QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#445630 | #8527. Power Divisions | ucup-team3099# | WA | 4096ms | 165004kb | C++23 | 8.7kb | 2024-06-16 06:46:51 | 2024-06-16 06:46:52 |
Judging History
answer
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <set>
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
template <class T, class E>
constexpr T fexp(T x, E e) {
T ans(1);
for(; e > 0; e >>= 1) {
if(e & 1) ans = ans * x;
x = x * x;
}
return ans;
}
template <class LOW, class HIGH, const LOW mod>
struct modBase {
using mint = modBase<LOW, HIGH, mod>;
constexpr modBase() : val(0) {}
// be careful of negative numbers!
constexpr modBase(const LOW v) : val((v % mod + mod) % mod) {}
LOW val;
#define add(a, b) a + b >= mod ? a + b - mod : a + b
#define sub(a, b) a < b ? a + mod - b : a - b
constexpr mint &operator += (const mint &o) { return val = add(val, o.val), *this; }
constexpr mint &operator -= (const mint &o) { return val = sub(val, o.val), *this; }
constexpr mint &operator *= (const mint &o) { return val = (LOW) ((HIGH) val * o.val % mod), *this; }
constexpr mint &operator /= (const mint &o) { return *this *= o.inverse(); }
constexpr mint operator + (const mint &b) const { return mint(*this) += b; }
constexpr mint operator - (const mint &b) const { return mint(*this) -= b; }
constexpr mint operator * (const mint &b) const { return mint(*this) *= b; }
constexpr mint operator / (const mint &b) const { return mint(*this) /= b; }
constexpr mint operator - () const { return mint() - mint(*this); }
constexpr bool operator == (const mint &b) const { return val == b.val; }
constexpr bool operator != (const mint &b) const { return val != b.val; }
template<class E> constexpr mint pow (E e) const { return fexp(*this, e); }
constexpr mint inverse() const { return pow(mod - 2); }
constexpr LOW get() const { return val; }
static constexpr LOW getMod() { return mod; }
friend std::ostream& operator << (std::ostream &os, const mint &p) { return os << p.val; }
friend std::istream& operator >> (std::istream &is, mint &p) { return is >> p.val; }
};
struct Info {
int pos;
int potL, potR;
int L, R;
};
std::vector<Info> genInfo(int n, const std::vector<int> &a) {
std::set<int> st;
st.insert(-1);
st.insert(n);
std::vector<int> p(n);
for(int i = 0; i < n; i++) {
p[i] = i;
}
std::sort(p.begin(), p.end(), [&](int v0, int v1) { return a[v0] > a[v1]; });
std::vector<Info> ans;
for(auto x : p) {
auto itL = st.lower_bound(x);
auto itR = itL;
itL--;
int l = *itL + 1, r = *itR - 1;
if(x - l < r - x) {
for(int i = l; i <= x; i++) {
ans.push_back({i, a[x], a[x] + (int) log2(r - l + 1) + 1, x+1, r+1});
}
} else {
for(int i = x; i <= r; i++) {
ans.push_back({i+1, a[x], a[x] + 20, l, x});
}
}
st.insert(x);
}
return ans;
}
struct Triple {
int l, r, pot;
};
template<const int MOD>
std::vector<Triple> solve(int n, const std::vector<int> &a, const std::vector<Info> &info) {
using mint = modBase<int, long long, MOD>;
std::vector<Triple> ans;
const int ms = 1001000;
std::vector<mint> b(n+1, 0), pot(ms, 1);
for(int i = 1; i < ms; i++) {
pot[i] = pot[i-1] * 2;
}
for(int i = 0; i < n; i++) {
b[i+1] = b[i] + pot[a[i]];
}
std::vector<std::pair<int, int>> st;
for(int i = 0; i <= n; i++) {
//std::cout << b[i] << (i == n ? '\n' : ' ');
st.push_back({b[i].val, i});
}
std::sort(st.begin(), st.end());
for(auto [pos, potL, potR, l, r] : info) {
for(int i = potL; i <= potR; i++) {
int target = (pos <= l ? (b[pos] + pot[i]).val : (b[pos] - pot[i]).val);
//std::cout << "pos " << pos << ", pot " << i << " range [" << l << ", " << r << ") got target " << target << '\n';
int first = (int)(std::lower_bound(st.begin(), st.end(), std::pair<int, int>(target, l)) - st.begin());
for(int j = first; j <= n && st[j].first == target && st[j].second <= r; j++) {
ans.push_back({std::min(pos, st[j].second), std::max(pos, st[j].second), i});
//std::cout << "found " << ans.back().first << ", " << ans.back().second << '\n';
}
}
}
//std::sort(ans.begin(), ans.end());
return ans;
}
template<const int MOD>
std::vector<Triple> solve(int n, const std::vector<int> &a, const std::vector<Triple> &info) {
using mint = modBase<int, long long, MOD>;
std::vector<Triple> ans;
const int ms = 1001000;
std::vector<mint> b(n+1, 0), pot(ms, 1);
for(int i = 1; i < ms; i++) {
pot[i] = pot[i-1] * 2;
}
for(int i = 0; i < n; i++) {
b[i+1] = b[i] + pot[a[i]];
}
for(auto [l, r, i] : info) {
if(b[r] - b[r] == pot[i]) {
ans.push_back({l, r, i});
}
}
//std::sort(ans.begin(), ans.end());
return ans;
}
constexpr int MOD[] = {(int) 1e9 + 7, (int) 1e9 + 9, 998244353};
int main() {
std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n, 0);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
auto info = genInfo(n, a);
std::vector<Triple> transitions = solve<MOD[0]>(n, a, info);
solve<MOD[2]>(n, a, transitions);
// {
// auto cur = solve<MOD[1]>(n, a, info);
// int sz = 0;
// for(int i = 0, j = 0; i < (int) transitions.size(); i++) {
// while(j+1 < (int) cur.size() && cur[j] < transitions[i]) j++;
// if(cur[j] == transitions[i]) {
// transitions[sz++] = transitions[i];
// }
// }
// transitions.resize(sz);
// }
// {
// auto cur = solve<MOD[2]>(n, a, info);
// int sz = 0;
// for(int i = 0, j = 0; i < (int) transitions.size(); i++) {
// while(j+1 < (int) cur.size() && cur[j] < transitions[i]) j++;
// if(cur[j] == transitions[i]) {
// transitions[sz++] = transitions[i];
// }
// }
// transitions.resize(sz);
// }
using mint = modBase<int, long long, MOD[0]>;
std::vector<mint> dp(n+1, 0);
dp[0] = 1;
std::vector<std::vector<int>> trans(n+1);
for(auto [l, r, _] : transitions) {
//std::cout << "[" << l << ", " << r << ")" << std::endl;
trans[l].push_back(r);
}
for(int i = 0; i < n; i++) for(auto j : trans[i]) {
dp[j] += dp[i];
}
std::cout << dp.back() << '\n';
}
/*
NEVER FORGET TO:
Look at the problem's constraints before coding.
How to cheese cf:
Find a lower bound or upper bound for the problem. Have faith that it is the answer of the problem.
If it isn't the answer, have more faith or change to another bound god by looking for a better bound.
Trust guesses. Who has time to think? If people in div2 AC the problem it requires no proof since people don't prove things.
You must draw cases. Thinking gets you nowhere, so draw cases and reach illogical conclusions from them.
Sometimes drawing cases is bad because it takes too much time. Faster is to not think at all and just code a bruteforce solution.
This is called "law of small numbers". If something works for small numbers, surely it works for big numbers.
https://en.wikipedia.org/wiki/Faulty_generalization#Hasty_generalization don't mind the "faulty" part of it, in competitive programming mistakes are lightly punished
Don't think about them being right or not, cf is a battle of intuition only.
Be as stupid as possible in implementation. Trying to be smart is an easy way to get WA.
Think about 2x2 cases for matrix problems and hope that everything works for the general case.
Find a necessary condition and trust it to be sufficient. They're basically the same thing.
Heuristics might speed up your code. Forget about complexity, it's only about ACing and not proving that your solution is good.
For paths in a grid starting at (1, i) or something like that, assume that they never cross and do D&C
Consider doing problems in reverse order of queries/updates
For combinatorics problems, consider symmetry
General strategy (MUST DO):
Try to solve the problem with more restricted constraints.
About testing:
Test n=1, a[i]=1, a[i]=n, etc. Basically, test low values. No need to test if pretests are strong, but if you get WA it's good.
This isn't a joke. Do it if you get stuck. It's shit practice in my opinion, but do it if you want AC.
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 10ms
memory: 7536kb
input:
5 2 0 0 1 1
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 6ms
memory: 7684kb
input:
1 0
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 10ms
memory: 7664kb
input:
2 1 1
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 10ms
memory: 7736kb
input:
3 2 1 1
output:
3
result:
ok 1 number(s): "3"
Test #5:
score: 0
Accepted
time: 11ms
memory: 7736kb
input:
4 3 2 2 3
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 7ms
memory: 7636kb
input:
5 3 4 4 2 4
output:
2
result:
ok 1 number(s): "2"
Test #7:
score: 0
Accepted
time: 10ms
memory: 7500kb
input:
7 3 4 3 5 6 3 4
output:
6
result:
ok 1 number(s): "6"
Test #8:
score: 0
Accepted
time: 7ms
memory: 7752kb
input:
10 8 6 5 6 7 8 6 8 9 9
output:
4
result:
ok 1 number(s): "4"
Test #9:
score: 0
Accepted
time: 10ms
memory: 7680kb
input:
96 5 1 0 2 5 5 2 4 2 4 4 2 3 4 0 2 1 4 3 1 2 0 2 2 3 2 4 5 3 5 2 0 2 2 5 3 0 4 5 3 5 4 4 3 1 2 0 5 4 5 0 2 3 2 4 0 0 4 2 0 2 5 3 3 1 5 5 1 1 1 0 5 0 3 0 2 1 1 0 5 0 3 3 4 4 5 3 0 2 2 0 5 4 5 0 5
output:
11332014
result:
ok 1 number(s): "11332014"
Test #10:
score: 0
Accepted
time: 7ms
memory: 7592kb
input:
480 2 0 4 4 1 0 0 3 1 1 4 2 5 5 4 2 1 2 4 4 1 3 4 3 0 5 2 0 2 5 1 0 5 0 0 5 5 0 2 5 2 2 3 1 4 3 5 4 5 2 4 4 4 4 1 4 0 3 4 3 4 1 0 4 3 4 5 4 3 5 0 2 2 0 1 5 4 4 2 0 3 3 3 4 3 0 5 5 3 1 5 1 0 1 0 4 3 0 5 1 4 1 4 3 0 1 3 5 0 3 3 1 0 4 1 1 2 0 1 2 0 3 5 2 0 5 5 5 5 3 5 1 0 2 5 2 2 0 2 0 2 3 5 1 2 1 5 4 ...
output:
506782981
result:
ok 1 number(s): "506782981"
Test #11:
score: 0
Accepted
time: 11ms
memory: 7752kb
input:
2400 0 2 2 0 5 4 3 2 3 2 5 4 5 4 4 5 2 2 4 2 2 0 1 0 5 0 4 4 0 0 5 0 4 0 1 3 4 5 0 3 1 0 4 0 2 5 0 3 3 3 3 1 0 5 5 3 1 3 5 2 4 0 5 0 4 5 4 2 2 1 5 2 2 4 1 0 5 1 5 0 1 2 0 0 3 5 4 0 0 1 1 1 4 2 0 5 1 3 3 5 0 4 4 1 5 5 3 4 4 4 0 2 4 0 5 1 3 1 5 0 5 5 1 3 0 3 1 2 0 1 1 3 5 2 3 4 0 3 0 5 4 0 4 3 5 0 5 2...
output:
586570528
result:
ok 1 number(s): "586570528"
Test #12:
score: 0
Accepted
time: 45ms
memory: 9428kb
input:
12000 2 2 1 2 0 2 5 3 2 0 1 3 2 5 4 0 0 5 3 2 0 2 3 4 3 2 1 4 3 0 3 5 4 1 0 2 4 1 3 2 3 5 0 3 0 0 4 0 4 5 1 0 4 1 1 1 5 4 3 0 3 5 4 5 2 5 0 1 2 3 5 5 2 5 4 2 0 4 4 3 0 0 2 5 0 3 4 2 5 4 2 1 4 5 1 1 2 3 0 3 3 3 3 4 0 5 3 4 0 3 0 2 0 0 2 0 3 4 2 2 0 1 0 5 3 0 2 0 2 2 1 0 5 3 5 4 5 5 0 4 0 4 1 4 4 3 2 ...
output:
201653965
result:
ok 1 number(s): "201653965"
Test #13:
score: 0
Accepted
time: 251ms
memory: 19784kb
input:
60000 2 5 0 3 2 3 5 3 5 5 4 1 1 5 3 0 1 1 2 5 5 5 0 3 2 0 3 2 3 3 0 0 1 4 3 1 4 2 3 3 0 5 1 0 1 1 5 5 4 0 5 4 1 3 1 3 5 3 2 4 4 4 5 4 3 2 3 2 4 5 2 0 4 5 1 2 0 4 0 5 1 3 4 1 2 4 1 1 3 3 0 1 1 3 0 0 2 3 3 2 1 4 1 2 4 3 3 5 2 5 3 4 3 0 2 1 1 1 5 1 2 4 2 3 1 2 1 0 2 0 1 1 5 5 3 4 2 5 2 4 5 3 0 5 1 4 2 ...
output:
592751350
result:
ok 1 number(s): "592751350"
Test #14:
score: 0
Accepted
time: 1458ms
memory: 68988kb
input:
300000 0 5 1 5 5 4 5 3 0 5 0 5 1 4 1 2 2 2 3 0 1 5 4 0 3 1 4 5 2 1 0 3 2 1 2 5 0 2 4 5 0 1 2 1 1 0 0 5 3 0 0 3 4 5 0 2 1 1 1 2 5 1 4 3 1 0 2 0 0 4 3 3 2 5 3 3 1 5 2 0 2 4 3 1 0 3 4 1 3 3 1 0 0 1 1 1 3 1 2 3 5 3 3 2 0 3 0 0 5 5 0 0 0 0 1 4 3 3 4 3 4 5 3 3 5 1 1 4 2 2 1 3 2 1 1 0 0 5 5 0 0 3 2 4 5 5 2...
output:
842503795
result:
ok 1 number(s): "842503795"
Test #15:
score: 0
Accepted
time: 1560ms
memory: 164984kb
input:
300000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
432100269
result:
ok 1 number(s): "432100269"
Test #16:
score: 0
Accepted
time: 3176ms
memory: 165004kb
input:
300000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 10000...
output:
432100269
result:
ok 1 number(s): "432100269"
Test #17:
score: 0
Accepted
time: 1625ms
memory: 117128kb
input:
299995 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 0 1 1 1 0 1 1 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 1 1 1 1 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 1 0 0 0...
output:
261818019
result:
ok 1 number(s): "261818019"
Test #18:
score: 0
Accepted
time: 1351ms
memory: 64848kb
input:
299997 2 2 0 9 4 4 2 3 8 9 3 9 1 6 4 0 1 5 1 0 7 9 3 3 8 9 3 8 3 6 9 3 9 5 9 1 4 4 7 5 9 0 7 3 7 2 0 3 3 8 2 1 7 6 8 1 6 1 8 4 7 6 3 6 1 6 8 9 3 8 1 5 0 8 1 10 0 3 4 5 8 5 6 9 2 4 5 0 9 0 9 5 1 0 3 7 5 8 8 10 10 3 3 10 5 8 9 9 7 4 4 1 1 6 5 7 2 5 8 3 3 9 6 4 1 0 2 6 2 8 7 7 10 5 7 8 3 8 5 1 6 6 6 1 ...
output:
999738318
result:
ok 1 number(s): "999738318"
Test #19:
score: -100
Wrong Answer
time: 4096ms
memory: 63196kb
input:
299999 97 34 33 30 15 73 31 69 60 63 79 87 78 13 49 58 23 38 91 28 70 70 14 98 56 59 81 66 29 21 10 51 94 32 41 98 16 48 67 62 55 5 17 81 30 91 39 93 73 74 46 74 41 99 19 10 0 16 72 95 84 40 97 17 76 10 42 50 66 97 4 30 71 74 46 5 75 87 55 82 38 94 14 82 49 10 23 21 19 99 52 100 71 29 64 73 54 88 2 ...
output:
535793678
result:
wrong answer 1st numbers differ - expected: '799664563', found: '535793678'