QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#664755 | #7876. Cyclic Substrings | 333zhan | AC ✓ | 180ms | 480620kb | C++20 | 3.7kb | 2024-10-21 22:10:17 | 2024-10-21 22:10:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 998244353;
struct PAM {
static constexpr int N = 10;
struct Node {
int len;
int link;
array <int, N> nxt;
Node () : len {}, link {}, nxt {} {};
};
vector <Node> t;
int suff;
string s;
PAM () {
init ();
}
void init () {
t.assign (2, Node ());
t[0].len = -1;
suff = 1;
s.clear ();
}
int newNode () {
t.emplace_back ();
return t.size () - 1;
}
bool add (char c) {
int pos = s.size ();
s += c;
int let = c - '0';
int cur = suff, curlen = 0;
while (true) {
curlen = t[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) {
break;
}
cur = t[cur].link;
}
if (t[cur].nxt[let]) {
suff = t[cur].nxt[let];
return false;
}
int num = newNode ();
suff = num;
t[num].len = t[cur].len + 2;
t[cur].nxt[let] = num;
if (t[num].len == 1) {
t[num].link = 1;
return true;
}
while (true) {
cur = t[cur].link;
curlen = t[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) {
t[num].link = t[cur].nxt[let];
break;
}
}
return true;
}
int nxt (int p, int x) {
return t[p].nxt[x];
}
int link (int p) {
return t[p].link;
}
int len (int p) {
return t[p].len;
}
int size () {
return t.size ();
}
};
using i64 = long long;
constexpr int N = 6E6 + 10;
// vector <vector <int>> e (N);
int f[N], f2[N];
// void dfs (int x) {
// for (auto y : e[x]) {
// dfs (y);
// f[x] += f[y];
// }
// };
// void dfs2 (int x) {
// for (auto y : e[x]) {
// dfs2 (y);
// f2[x] += f2[y];
// }
// };
void solve () {
int n;
cin >> n;
string s;
// s = string (n, '1');
// for (int i = 0; i < n / 3; i ++) {
// s += "118";
// }
// for (int i = 0; i < n; i ++) {
// s += "2";
// }
cin >> s;
PAM pam;
vector <int> v;
for (auto c : s) {
pam.add (c);
v.push_back (pam.suff);
}
int N = pam.size ();
// vector <int> f (N);
for (auto x : v) {
f[x] ++;
}
// for (int i = 2; i < N; i ++) {
// e[pam.link (i)].push_back (i);
// }
// for (int i = 1; i <= 10; i ++) {
// cerr << f[i] << " ";
// }
for (int i = N - 1; i >= 2; i --) {
f[pam.link (i)] += f[i];
}
// dfs (1);
for (auto &x : f) {
x = -x;
}
for (auto c : s) {
pam.add (c);
v.push_back (pam.suff);
}
int M = N;
N = pam.size ();
// cout << N << '\n';
// vector <int> f2 (N);
for (auto x : v) {
f2[x] ++;
}
// e.resize (N);
// for (int i = M; i < N; i ++) {
// e[pam.link (i)].push_back (i);
// }
for (int i = N - 1; i >= 2; i --) {
f2[pam.link (i)] += f2[i];
}
// dfs2 (1);
i64 ans = 0;
for (int i = 2; i < N; i ++) {
if (pam.len (i) > n) {
continue;
}
if (i < M) {
f2[i] += f[i];
}
ans = (ans + 1LL * f2[i] * f2[i] % mod * pam.len (i) % mod) % mod;
}
cout << ans << '\n';
}
signed main () {
ios::sync_with_stdio (false);
cin.tie (nullptr);
int T = 1;
// cin >> T;
while (T --) {
solve ();
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 28076kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 4ms
memory: 28624kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 0ms
memory: 27948kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 4ms
memory: 28544kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 4ms
memory: 28848kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 8ms
memory: 28132kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 4ms
memory: 27704kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 59ms
memory: 148020kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 173ms
memory: 457192kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 142ms
memory: 229580kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 148ms
memory: 457472kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 140ms
memory: 458060kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 172ms
memory: 477752kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 180ms
memory: 478788kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 128ms
memory: 241956kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 48ms
memory: 122436kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 139ms
memory: 480620kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 152ms
memory: 479800kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed