QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#181516 | #7219. The Mighty Spell | ucup-team288 | RE | 21ms | 16656kb | C++20 | 4.3kb | 2023-09-16 20:08:43 | 2023-09-16 20:08:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 200010, MAX_M = 51, p = 1e9 + 7;
using ap = array<int, MAX_M>;
int n, m;
int c[MAX_N];
ll cnt[MAX_M];
ll w[MAX_N], iw[MAX_N], p2[MAX_N];
ll wb[MAX_N], iwb[MAX_N];
ll pf[MAX_N];
ap lst[MAX_N];
ll inv[MAX_N];
ll g(ll x) {
vector<int> c = {2, 3, 3, 3};
ll s = 0;
for (ll z: c)
s = (s * x + z) % p;
return s;
return (2 * x * x % p * x % p
+ 3 * x * x % p
+ 3 * x + 3) % p;
}
void mul(ll &a, ll b) {
a = (a * b) % p;
}
void add(ll &a, ll b) {
a = (a + b) % p;
}
ll bin_pow(ll v, ll t) {
ll ret = 1;
for (;t;t>>=1, mul(v, v))
if (t&1) mul(ret, v);
return ret;
}
void init() {
inv[0] = inv[1] = 1;
p2[0] = 1;
p2[1] = 2;
for (int i = 2;i < MAX_N;++i) {
inv[i] = (p-p/i) * inv[p % i] % p;
p2[i] = p2[i-1] * 2 % p;
}
for (int i = 1;i < MAX_N;++i)
pf[i] = (pf[i-1] + g(i) * bin_pow(inv[2], i)) % p;
}
//ll get_g(ll l, ll r, ll clen) {
// ll ret = 0;
// for (int i = l;i <= r;++i) {
// add(ret, p2[clen - i] * f(i) % p);
// }
// return ret;
//}
ll cont(ll l, ll r) {
vector<int> has(m+1);
for (int i = l;i <= r;++i)
has[ c[i] ] = true;
vector<int> cnt(m+1);
for (int i = 1;i+1 < l;++i)
++cnt[ c[i] ];
for (int i = r+2;i <= n;++i)
++cnt[ c[i] ];
ll ret = g(r - l + 1);
for (int i = 1;i <= m;++i) {
if (has[i]) mul(ret, p2[cnt[i]]);
else mul(ret, p2[cnt[i]] - 1);
}
return ret;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
init();
DE(g(1), g(2), g(3), g(2) + g(3) + 2 * g(1));
cin >> n >> m;
for (int i = 1;i <= n;++i)
cin >> c[i];
for (int i = 1;i <= n;++i) {
lst[i] = lst[i-1];
lst[i][c[i]] = i;
++cnt[c[i]];
}
for (int i = 1;i <= m;++i) {
w[i] = p2[cnt[i]] - 1;
iw[i] = bin_pow(w[i], p-2);
if (cnt[i] > 0)
wb[i] = p2[cnt[i]-1] - 1;
else
wb[i] = 0;
iwb[i] = bin_pow(wb[i], p-2);
//DE(i, w[i]);
}
ll res = 0;
for (int r = 1;r <= n;++r) {
if (r+1 <= n && cnt[ c[r+1] ] == 1) continue;
int nxt_c = c[r+1];
if (nxt_c) {
--cnt[nxt_c];
swap(w[nxt_c], wb[nxt_c]);
swap(iw[nxt_c], iwb[nxt_c]);
}
ll cur_v = 1;
for (int i = 1;i <= m;++i) {
DE(i, cnt[i]);
//mul(cur_v, p2[ cnt[i] ]-1);
mul(cur_v, w[i]);
}
vector<int> sid(m+1); iota(AI(sid), 0);
sort(AI(sid), [&](int a, int b) {
return lst[r][a] > lst[r][b];
});
DE(r);
int clen = 0;
for (int j = 0;;++j) {
int p1 = lst[r][ sid[j] ];
clen += cnt[ sid[j] ];
DE(clen, sid[j], cnt[ sid[j] ]);
if (p1 == 0) {
// len = r
//ll sc = cur_v;
ll v = cur_v * g(r) % p * p2[clen-r];
assert(v == cont(1, r));
add(res, v);
break;
}
mul(cur_v, iw[ sid[j] ]);
if (cnt[ sid[j] ] > 1) {
clen -= cnt[ sid[j] ];
ll sc = cur_v;
mul(sc, p2[ cnt[sid[j]]-1 ] - 1);
int O = r - p1;
if (O > 0) {
ll v = sc * g(O) % p * p2[clen - O] % p;
assert(v == cont(p1+1, r));
add(res, v);
}
clen += cnt[ sid[j] ];
}
int p2 = lst[r][ sid[j+1] ];
int L = r-p1+1, R = r-p2-1;
DE(j, cur_v, L, R, r);
//upd(L, R, clen, cur_v);
if (L <= R) {
ll v = (pf[R] - pf[L-1] + p) % p
* ::p2[clen - 1] % p;
add(res, v * cur_v);
}
// for (int j = L;j <= R;++j) {
// ll v = g(j) * cur_v % p * ::p2[clen - 1 - j] % p;
//
//// int lb = r - j + 1;
//// DE(v, cont(r - j + 1, r), lb, cur_v, g(j), clen - 1 - j);
// assert(cont(r - j + 1, r) == v);
// }
//add(res, get_g(L, R, clen) * cur_v % p * );
}
if (nxt_c) {
++cnt[nxt_c];
swap(w[nxt_c], wb[nxt_c]);
swap(iw[nxt_c], iwb[nxt_c]);
}
}
if (res < 0) res += p;
cout << res << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 16ms
memory: 14244kb
input:
3 2 1 2 2
output:
152
result:
ok answer is '152'
Test #2:
score: 0
Accepted
time: 20ms
memory: 14048kb
input:
4 3 1 2 1 2
output:
0
result:
ok answer is '0'
Test #3:
score: 0
Accepted
time: 16ms
memory: 16252kb
input:
6 3 1 2 3 3 2 1
output:
3627
result:
ok answer is '3627'
Test #4:
score: 0
Accepted
time: 16ms
memory: 14744kb
input:
5 5 1 4 5 3 2
output:
343
result:
ok answer is '343'
Test #5:
score: 0
Accepted
time: 20ms
memory: 13772kb
input:
5 5 1 5 4 3 2
output:
343
result:
ok answer is '343'
Test #6:
score: 0
Accepted
time: 19ms
memory: 14796kb
input:
5 5 3 1 5 4 2
output:
343
result:
ok answer is '343'
Test #7:
score: 0
Accepted
time: 20ms
memory: 15148kb
input:
5 5 4 1 2 3 5
output:
343
result:
ok answer is '343'
Test #8:
score: 0
Accepted
time: 13ms
memory: 15072kb
input:
5 5 2 3 2 2 2
output:
0
result:
ok answer is '0'
Test #9:
score: 0
Accepted
time: 16ms
memory: 15876kb
input:
5 5 1 2 2 2 5
output:
0
result:
ok answer is '0'
Test #10:
score: 0
Accepted
time: 16ms
memory: 15688kb
input:
5 5 4 2 1 3 5
output:
343
result:
ok answer is '343'
Test #11:
score: 0
Accepted
time: 20ms
memory: 14660kb
input:
5 5 2 3 4 5 1
output:
343
result:
ok answer is '343'
Test #12:
score: 0
Accepted
time: 12ms
memory: 16504kb
input:
5 5 4 3 5 2 1
output:
343
result:
ok answer is '343'
Test #13:
score: 0
Accepted
time: 16ms
memory: 15696kb
input:
5 5 3 4 5 2 1
output:
343
result:
ok answer is '343'
Test #14:
score: 0
Accepted
time: 20ms
memory: 16636kb
input:
5 5 4 3 3 5 2
output:
0
result:
ok answer is '0'
Test #15:
score: 0
Accepted
time: 20ms
memory: 15192kb
input:
5 5 1 4 4 1 1
output:
0
result:
ok answer is '0'
Test #16:
score: 0
Accepted
time: 20ms
memory: 14856kb
input:
5 5 1 5 2 4 3
output:
343
result:
ok answer is '343'
Test #17:
score: 0
Accepted
time: 17ms
memory: 14952kb
input:
5 5 4 2 5 3 1
output:
343
result:
ok answer is '343'
Test #18:
score: 0
Accepted
time: 13ms
memory: 14540kb
input:
5 5 3 1 4 5 2
output:
343
result:
ok answer is '343'
Test #19:
score: 0
Accepted
time: 16ms
memory: 15564kb
input:
5 5 5 1 3 4 2
output:
343
result:
ok answer is '343'
Test #20:
score: 0
Accepted
time: 20ms
memory: 15836kb
input:
5 5 4 5 3 5 5
output:
0
result:
ok answer is '0'
Test #21:
score: 0
Accepted
time: 12ms
memory: 13732kb
input:
5 5 2 2 3 4 2
output:
0
result:
ok answer is '0'
Test #22:
score: 0
Accepted
time: 16ms
memory: 14504kb
input:
5 5 4 5 1 2 3
output:
343
result:
ok answer is '343'
Test #23:
score: 0
Accepted
time: 20ms
memory: 14696kb
input:
5 5 3 5 1 2 4
output:
343
result:
ok answer is '343'
Test #24:
score: 0
Accepted
time: 21ms
memory: 16392kb
input:
5 5 5 4 1 2 3
output:
343
result:
ok answer is '343'
Test #25:
score: 0
Accepted
time: 16ms
memory: 15124kb
input:
5 5 5 3 4 1 2
output:
343
result:
ok answer is '343'
Test #26:
score: 0
Accepted
time: 16ms
memory: 15520kb
input:
5 5 3 1 2 1 5
output:
0
result:
ok answer is '0'
Test #27:
score: 0
Accepted
time: 20ms
memory: 14812kb
input:
5 5 3 1 4 2 5
output:
343
result:
ok answer is '343'
Test #28:
score: 0
Accepted
time: 16ms
memory: 16320kb
input:
5 5 1 2 4 5 3
output:
343
result:
ok answer is '343'
Test #29:
score: 0
Accepted
time: 16ms
memory: 15608kb
input:
5 5 4 3 1 5 2
output:
343
result:
ok answer is '343'
Test #30:
score: 0
Accepted
time: 16ms
memory: 16396kb
input:
5 5 2 1 3 4 5
output:
343
result:
ok answer is '343'
Test #31:
score: 0
Accepted
time: 21ms
memory: 15536kb
input:
5 5 4 2 1 3 5
output:
343
result:
ok answer is '343'
Test #32:
score: 0
Accepted
time: 16ms
memory: 14812kb
input:
5 5 4 3 1 4 3
output:
0
result:
ok answer is '0'
Test #33:
score: 0
Accepted
time: 16ms
memory: 13840kb
input:
5 5 3 4 1 1 3
output:
0
result:
ok answer is '0'
Test #34:
score: 0
Accepted
time: 16ms
memory: 15672kb
input:
20 5 5 2 5 1 5 5 2 4 5 5 2 5 5 5 5 4 2 5 3 4
output:
102882880
result:
ok answer is '102882880'
Test #35:
score: 0
Accepted
time: 16ms
memory: 15256kb
input:
20 5 3 2 1 2 2 2 2 2 4 3 2 2 3 3 5 2 2 1 2 5
output:
134653185
result:
ok answer is '134653185'
Test #36:
score: 0
Accepted
time: 14ms
memory: 16324kb
input:
20 5 1 2 3 2 1 3 5 1 2 4 5 2 3 4 5 1 4 3 4 5
output:
315505338
result:
ok answer is '315505338'
Test #37:
score: 0
Accepted
time: 17ms
memory: 16656kb
input:
20 5 5 2 2 4 2 3 5 1 1 3 1 5 2 4 4 3 1 4 3 5
output:
312062382
result:
ok answer is '312062382'
Test #38:
score: 0
Accepted
time: 6ms
memory: 15660kb
input:
20 5 3 4 2 5 4 5 5 4 1 4 3 3 4 3 4 2 3 2 5 3
output:
188515821
result:
ok answer is '188515821'
Test #39:
score: 0
Accepted
time: 20ms
memory: 15216kb
input:
20 5 3 5 1 3 3 4 5 2 1 1 3 1 2 5 2 1 1 2 5 2
output:
197857329
result:
ok answer is '197857329'
Test #40:
score: 0
Accepted
time: 20ms
memory: 15660kb
input:
20 10 3 8 6 8 9 2 1 5 8 6 7 8 4 8 6 8 10 8 8 8
output:
4905343
result:
ok answer is '4905343'
Test #41:
score: 0
Accepted
time: 16ms
memory: 15832kb
input:
20 10 10 5 1 8 7 2 7 2 6 2 2 2 2 2 4 2 3 7 9 7
output:
3724041
result:
ok answer is '3724041'
Test #42:
score: 0
Accepted
time: 20ms
memory: 14500kb
input:
20 10 5 1 9 6 10 4 5 3 2 4 8 3 7 1 8 6 2 9 10 7
output:
52978806
result:
ok answer is '52978806'
Test #43:
score: 0
Accepted
time: 16ms
memory: 14544kb
input:
20 10 5 8 6 2 1 10 3 8 9 7 6 5 10 9 1 7 3 4 4 2
output:
53309955
result:
ok answer is '53309955'
Test #44:
score: 0
Accepted
time: 13ms
memory: 15964kb
input:
20 10 1 8 1 7 9 7 9 9 7 4 1 6 2 7 8 6 6 9 6 7
output:
0
result:
ok answer is '0'
Test #45:
score: 0
Accepted
time: 13ms
memory: 15516kb
input:
20 10 1 10 10 10 2 9 1 1 7 2 3 9 5 10 8 4 1 4 2 5
output:
0
result:
ok answer is '0'
Test #46:
score: 0
Accepted
time: 21ms
memory: 14736kb
input:
20 20 9 16 3 18 8 19 6 4 2 17 1 15 10 11 5 13 12 7 14 20
output:
17263
result:
ok answer is '17263'
Test #47:
score: 0
Accepted
time: 19ms
memory: 14948kb
input:
20 20 2 17 18 12 15 20 11 9 10 5 6 16 7 8 4 13 3 1 19 14
output:
17263
result:
ok answer is '17263'
Test #48:
score: 0
Accepted
time: 16ms
memory: 14500kb
input:
20 20 14 15 19 8 3 20 9 12 18 7 5 11 4 2 16 6 1 17 10 13
output:
17263
result:
ok answer is '17263'
Test #49:
score: 0
Accepted
time: 16ms
memory: 15660kb
input:
20 20 18 9 3 4 13 12 15 11 2 16 19 7 10 20 17 8 6 1 14 5
output:
17263
result:
ok answer is '17263'
Test #50:
score: 0
Accepted
time: 20ms
memory: 14852kb
input:
20 20 7 6 4 14 20 13 1 15 5 18 16 10 1 16 12 14 5 13 1 3
output:
0
result:
ok answer is '0'
Test #51:
score: 0
Accepted
time: 16ms
memory: 15404kb
input:
20 20 17 17 5 16 9 14 14 1 2 4 19 8 9 5 9 20 5 16 20 9
output:
0
result:
ok answer is '0'
Test #52:
score: 0
Accepted
time: 15ms
memory: 14560kb
input:
20 20 20 1 16 5 19 11 8 7 2 3 12 6 17 14 13 18 4 10 15 9
output:
17263
result:
ok answer is '17263'
Test #53:
score: 0
Accepted
time: 10ms
memory: 14956kb
input:
20 20 2 13 6 1 17 3 11 9 8 10 7 5 16 14 4 15 18 19 20 12
output:
17263
result:
ok answer is '17263'
Test #54:
score: 0
Accepted
time: 16ms
memory: 15500kb
input:
20 20 17 11 13 5 2 14 18 7 6 9 10 3 16 15 8 1 19 4 12 20
output:
17263
result:
ok answer is '17263'
Test #55:
score: 0
Accepted
time: 20ms
memory: 16468kb
input:
20 20 7 13 1 15 9 2 8 20 4 5 12 3 11 14 10 18 6 17 16 19
output:
17263
result:
ok answer is '17263'
Test #56:
score: 0
Accepted
time: 16ms
memory: 14760kb
input:
20 20 11 8 17 7 10 20 20 12 7 3 7 14 15 4 14 7 11 1 12 20
output:
0
result:
ok answer is '0'
Test #57:
score: 0
Accepted
time: 20ms
memory: 14536kb
input:
20 20 20 18 17 14 11 2 13 3 10 1 16 3 1 16 10 8 4 8 13 3
output:
0
result:
ok answer is '0'
Test #58:
score: 0
Accepted
time: 16ms
memory: 14876kb
input:
20 20 1 20 7 4 18 11 9 8 3 6 16 13 19 2 12 14 15 17 10 5
output:
17263
result:
ok answer is '17263'
Test #59:
score: 0
Accepted
time: 17ms
memory: 14844kb
input:
20 20 8 4 5 18 16 15 19 2 13 1 14 7 10 12 17 3 6 9 20 11
output:
17263
result:
ok answer is '17263'
Test #60:
score: 0
Accepted
time: 20ms
memory: 15084kb
input:
20 20 13 14 8 9 18 17 20 6 15 3 2 16 12 11 1 10 19 4 7 5
output:
17263
result:
ok answer is '17263'
Test #61:
score: 0
Accepted
time: 17ms
memory: 15932kb
input:
20 20 4 10 17 8 19 1 16 3 14 6 7 11 13 15 20 9 12 5 2 18
output:
17263
result:
ok answer is '17263'
Test #62:
score: 0
Accepted
time: 13ms
memory: 15476kb
input:
20 20 19 17 19 8 7 1 18 13 16 16 20 11 5 8 17 19 11 14 4 8
output:
0
result:
ok answer is '0'
Test #63:
score: 0
Accepted
time: 16ms
memory: 14724kb
input:
20 20 7 1 14 13 20 16 10 18 16 12 5 7 16 14 6 12 11 20 10 19
output:
0
result:
ok answer is '0'
Test #64:
score: -100
Runtime Error
input:
500 5 3 5 5 3 5 3 5 5 3 3 3 3 3 3 1 3 3 3 3 3 3 3 5 3 3 3 1 3 3 3 3 5 3 3 3 3 3 3 3 3 3 3 5 3 3 3 5 3 3 5 2 3 3 3 5 3 3 3 3 1 3 3 5 3 3 5 3 3 5 3 5 3 3 3 3 3 3 5 3 5 3 3 5 3 5 3 5 3 3 3 3 3 3 3 3 3 3 3 3 3 5 3 3 3 3 3 3 3 3 3 3 3 5 3 3 3 3 3 3 3 5 3 3 5 5 3 3 3 3 3 5 3 5 3 3 3 3 5 3 3 3 3 3 3 3 3 3 ...