QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#392245 | #7876. Cyclic Substrings | iwew | AC ✓ | 1566ms | 596804kb | C++20 | 1.8kb | 2024-04-17 12:39:14 | 2024-04-17 12:39:15 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 6e6 + 10, M = 23, mod = 998244353;
int n;
namespace PAM {
struct Node {
int len, fail, ch[10];
}pam[N];
string s;
int cnt = 2, last = 1, pos = 0;
void init() {
s = "$" + s;
pam[1].len = -1, pam[2].len = 0, pam[2].fail = 1;
}
int up(int p) {
while(pam[p].len + 2 > n || s[pos - pam[p].len - 1] != s[pos]) {
p = pam[p].fail;
}
return p;
}
int fa[N][M], f[N], dep[N];
void insert(int ch, int id) {
pos += 1;
int p = up(last);
int &q = pam[p].ch[ch];
if(q == 0) {
q = ++ cnt;
pam[q].len = pam[p].len + 2;
pam[q].fail = (p == 1 ? 2 : pam[up(pam[p].fail)].ch[ch]);
}
last = q;
// cout << "ooo " << id << ' ' << pam[q].fail << ' ' << q << ' ' << pam[q].len << '\n';
p = q;
dep[p] = dep[pam[p].fail] + 1;
fa[p][0] = pam[p].fail;
for(int i = 1; (1 << i) <= dep[p]; i ++ ) {
fa[p][i] = fa[fa[p][i - 1]][i - 1];
}
if(id - pam[p].len + 1 <= n) {
f[p] += 1;
int len = log(dep[p]) / log(2);
for(int j = len; j >= 0; j -- ) {
if(id - pam[fa[p][j]].len + (pam[fa[p][j]].len > 0) <= n) {
p = fa[p][j];
}
}
f[fa[p][0]] -= 1;
}
}
ll get_ans() {
for(int i = cnt; i >= 1; i -- ) {
f[pam[i].fail] += f[i];
}
ll ans = 0;
for(int i = 1; i <= cnt; i ++ ) {
if(pam[i].len > 0 && pam[i].len <= n) {
ll res = 1ll * f[i] * f[i] % mod * pam[i].len % mod;
ans = (ans + res) % mod;
}
}
return ans;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> n >> s;
s = s + s;
PAM::s = s;
PAM::init();
for(int i = 0; i < s.length(); i ++ ) {
PAM::insert(PAM::s[i + 1] - '0', i + 1);
}
cout << PAM::get_ans() << '\n';
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 9800kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 0ms
memory: 10104kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 1ms
memory: 9852kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 10172kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 1ms
memory: 10052kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 1ms
memory: 9884kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 1ms
memory: 10076kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 479ms
memory: 160512kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 1566ms
memory: 455892kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 955ms
memory: 414296kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 1564ms
memory: 456516kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 1413ms
memory: 456172kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 1142ms
memory: 564600kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 1042ms
memory: 596804kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 251ms
memory: 455088kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 201ms
memory: 150004kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 300ms
memory: 534684kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 232ms
memory: 518244kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed