QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#543821 | #7876. Cyclic Substrings | afy | AC ✓ | 168ms | 549072kb | C++20 | 3.0kb | 2024-09-01 21:03:35 | 2024-09-01 21:03:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ(x) ((int)((x).size()))
#define lb(x) ((x) & (-(x)))
#define bp(x) __builtin_popcount(x)
#define bpll(x) __builtin_popcountll(x)
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
const int Mod = 998244353;
struct PAM {
string s;
int lst, tot, n;
vector<int> fa, len, sz, cnt, pos;
vector<array<int, 10>> ch;
/*
- len[i] : the length of the longest palindrome string end with i
- sz[i] : the number of the palindrome string end with i
- cnt[i] : the occurrence number of the longest palindrome string end with i
- fa[i] : i 的回文后缀链接
- pos[i] : 下标为 i 的字符在回文树上对应的结点
- st : fa 的倍增数组
*/
PAM(){}
PAM(string s): s(s), n(s.length() + 2) { // String s (Index from 0)
fa.resize(n); len = sz = cnt = pos = fa;
ch = vector(n, array<int, 10>());
len[0] = 0; len[1] = -1;
fa[0] = 1; fa[1] = 0;
lst = 0; tot = 1;
}
int get_fail(int x, int now) {
while (s[now - len[x] - 1] != s[now]) {
x = fa[x];
}
return x;
}
void insert(int c, int now) {
int p = get_fail(lst, now);
if (!ch[p][c]) {
len[++tot] = len[p] + 2;
int tmp = get_fail(fa[p], now);
fa[tot] = ch[tmp][c];
sz[tot] = sz[fa[tot]] + 1;
ch[p][c] = tot;
}
lst = ch[p][c];
cnt[lst] += 1;
pos[now] = ch[p][c];
}
void init() {
for (int i = 0; i < SZ(s); i++) {
insert(s[i] - '0', i);
}
for (int i = tot; i >= 0; i--) {
cnt[fa[i]] += cnt[i];
}
}
};
void solve() {
int n;
string s;
cin >> n >> s;
PAM pam1(s);
PAM pam2(s + s);
pam1.init();
pam2.init();
ll ans = 0;
vector mp(pam2.tot + 1, false);
for (int i = 0; i < n; i++) {
int u = pam2.pos[i];
int len = pam2.len[u];
int cnt = pam2.cnt[u];
if (mp[u]) {
continue;
}
mp[u] = true;
int c = cnt - pam1.cnt[pam1.pos[i]];
(ans += 1ll * c * c % Mod * len) %= Mod;
}
for (int i = n; i < 2 * n; i++) {
int u = pam2.pos[i];
int len = pam2.len[u];
int cnt = pam2.cnt[u];
if (mp[u]) {
continue;
}
mp[u] = true;
if (len > n) {
continue;
}
int c = cnt;
(ans += 1ll * c * c % Mod * len) %= Mod;
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
while (T--) solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 44ms
memory: 184888kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 168ms
memory: 548128kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 123ms
memory: 548404kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 132ms
memory: 548112kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 122ms
memory: 548192kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 157ms
memory: 548480kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 165ms
memory: 549072kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 115ms
memory: 548160kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 81ms
memory: 548856kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 123ms
memory: 548136kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 123ms
memory: 548292kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed