QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#451509 | #8527. Power Divisions | HKOI0# | TL | 2542ms | 176968kb | C++20 | 2.9kb | 2024-06-23 15:42:58 | 2024-06-23 15:42:58 |
Judging History
answer
#include <bits/stdc++.h>
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#define int long long
const int N = 3e5 + 11;
const int M = 1.1e6 + 11;
const int MOD = 1e9 + 7;
const int MOD2 = 888888877777777LL;
int dp[N], pw[M];
int mul(int a, int b){
return (__int128_t) a * a % MOD2;
}
struct PartialSum{
vector<int> h;
unordered_map<int, int> mp;
PartialSum(int n, vector<int> a) : h(n + 1){
for (int i = 0; i < n; i++) {
h[i + 1] = (h[i] + pw[a[i]]) % MOD2;
}
for (int i = 0; i <= n; i++) {
mp[h[i]] = i;
}
}
int find(int x){
if (x >= MOD2) x -= MOD2;
if (x < 0) x += MOD2;
if (mp.count(x)) { return mp[x]; }
else return -1;
}
};
vector<int> nxt[N];
void add_transition(int l, int r){
#ifdef LOCAL
cout << "TRANSIT " << l << ' ' << r << endl;
#endif
nxt[l].push_back(r);
}
void find_transit(PartialSum& ps, vector<int>& a, int l, int r, int i){
// cout << "FIND" << ' ' << l << ' ' << r << ' ' << i << endl;
for (int j = a[i]; j <= a[i] + __lg(r - l + 1); j++) {
if (i - l <= r) {
for (int x = l; x <= i; x++) {
int idx = ps.find(ps.h[x] + pw[j]);
if (idx == -1) continue;
add_transition(x, idx);
}
} else {
for (int x = i + 1; x <= r + 1; x++) {
int idx = ps.find(ps.h[x] - pw[j]);
if (idx == -1) continue;
add_transition(idx, x);
}
}
}
}
bool used[N];
void solve() {
pw[0] = 1; for (int i = 1; i < M; i++) pw[i] = pw[i - 1] * 2 % MOD2;
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<pair<int, int>> V;
for (int i = 0; i < n; i++) {
V.push_back({a[i], i});
}
sort(all(V), [](auto x, auto y){
return x.first > y.first || x.first == y.first && x.second > y.second;
});
set<int> S; S.insert(-1); S.insert(n);
PartialSum PS(n, a);
for (auto [v, i] : V) {
auto ptr = S.lower_bound(i);
int l = *prev(ptr) + 1, r = *ptr - 1;
assert(l <= i && i <= r);
find_transit(PS, a, l, r, i);
S.insert(i);
}
dp[0] = 1;
for (int i = 0; i < n; i++) {
for (auto x : nxt[i]) {
if (used[x]) continue;
used[x] = true; dp[x] = (dp[x] + dp[i]) % MOD;
}
for (auto x : nxt[i]) {
used[x] = false;
}
}
cout << dp[n] << '\n';
}
signed main() {
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 14092kb
input:
5 2 0 0 1 1
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 7ms
memory: 13804kb
input:
1 0
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 3ms
memory: 13876kb
input:
2 1 1
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 6ms
memory: 15896kb
input:
3 2 1 1
output:
3
result:
ok 1 number(s): "3"
Test #5:
score: 0
Accepted
time: 3ms
memory: 13844kb
input:
4 3 2 2 3
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 3ms
memory: 14096kb
input:
5 3 4 4 2 4
output:
2
result:
ok 1 number(s): "2"
Test #7:
score: 0
Accepted
time: 6ms
memory: 14096kb
input:
7 3 4 3 5 6 3 4
output:
6
result:
ok 1 number(s): "6"
Test #8:
score: 0
Accepted
time: 3ms
memory: 13736kb
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: 3ms
memory: 14060kb
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: 8ms
memory: 14136kb
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: 94ms
memory: 22872kb
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: 2542ms
memory: 176968kb
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: -100
Time Limit Exceeded
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 ...