QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#642627 | #7876. Cyclic Substrings | ucup-team1001 | AC ✓ | 304ms | 555228kb | C++20 | 2.4kb | 2024-10-15 15:22:46 | 2024-10-15 15:22:46 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
i64 mod = 998244353;
// 回文自动机
// T 字符集大小 , base 字符集基数
template<size_t T = 26, size_t base = 'a'>
class PAM {
private:
class Node {
public:
int next[T];
int fail;
int len;
i64 cnt;
int x;
Node() {
memset(next, 0, sizeof(next));
fail = 0;
len = 0;
cnt = 0;
}
};
string s;
vector<Node> tree;
int last;
int lim;
// 获取节点 , 节点指针指向 x , 字符串长度指针指向i
int getFail(int x, int i) {
while (i - tree[x].len + 1 >= 2 && s[i] != s[i - tree[x].len - 1]) {
x = tree[x].fail;
}
return x;
}
int newNode(int len, int p) {
// cerr << len << " " << p << endl;
tree.push_back(Node());
tree.back().len = len;
tree.back().x = p;
return tree.size() - 1;
}
void init() {
tree.clear();
newNode(0, -1);
newNode(-1, -1);
tree[0].fail = 1;
last = 1;
}
void insert(int x, int i) {
int p = getFail(last, i);
// cerr << x << " " << i << endl;
if (!tree[p].next[x]) {
int np = newNode(tree[p].len + 2, x);
tree[np].fail = tree[getFail(tree[p].fail, i)].next[x];
tree[p].next[x] = np;
}
last = tree[p].next[x];
if (i > lim) {
// cerr << i << endl;
tree[last].cnt++;
}
}
public:
PAM(string _s, int lim) : s(_s), lim(lim) {
init();
s = "#" + s;
for (int i = 1; i < s.size(); i++) {
insert(s[i] - base, i);
}
}
i64 solve() {
i64 ans = 0;
for (int i = tree.size() - 1; i >= 2; i--) {
// 出现的次数
tree[tree[i].fail].cnt += tree[i].cnt;
tree[tree[i].fail].cnt %= mod;
if (tree[i].len <= lim)
ans += 1ll * tree[i].cnt * tree[i].cnt % mod * tree[i].len % mod;
ans %= mod;
}
return ans;
}
};
int main() {
int n;
cin >> n;
string s;
cin >> s;
s = s + s;
PAM<10, '0'> pam(s, n);
cout << pam.solve() << endl;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3796kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 55ms
memory: 143000kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 223ms
memory: 555228kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 204ms
memory: 291776kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 223ms
memory: 553300kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 236ms
memory: 553888kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 281ms
memory: 554956kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 304ms
memory: 553924kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 193ms
memory: 292564kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 118ms
memory: 95248kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 239ms
memory: 554712kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 244ms
memory: 554600kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed