QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#485314 | #7876. Cyclic Substrings | ucup-team1198# | AC ✓ | 1444ms | 908632kb | C++20 | 3.1kb | 2024-07-20 16:16:09 | 2024-07-20 16:16:09 |
Judging History
answer
#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int MOD = 998244353;
int add(int a, int b) {
if (a + b < MOD)
return a + b;
return a + b - MOD;
}
int sub(int a, int b) {
if (a >= b)
return a - b;
return a + MOD - b;
}
int mul(int a, int b) {
return a * 1ll * b % MOD;
}
const int MAXSZ = 6'000'600;
const int ALPHA = 10;
int go[MAXSZ][ALPHA];
int len[MAXSZ];
int suff[MAXSZ];
int nodes_cnt = 0;
int node() {
fill(go[nodes_cnt], go[nodes_cnt] + ALPHA, -1);
len[nodes_cnt] = 0;
suff[nodes_cnt] = -1;
++nodes_cnt;
return nodes_cnt - 1;
}
int get_char_id(char c) {
return c - '0';
}
string cur_s;
int push_back(int last, char c) {
cur_s += c;
while ((int)cur_s.size() < len[last] + 2 || cur_s[cur_s.size() - len[last] - 2] != c)
last = suff[last];
if (go[last][get_char_id(c)] == -1) {
int new_v = node();
go[last][get_char_id(c)] = new_v;
len[new_v] = len[last] + 2;
if (len[last] == -1) {
suff[new_v] = 1; // even root
} else {
int new_suff = suff[last];
while (new_suff != -1 && cur_s[cur_s.size() - len[new_suff] - 2] != c)
new_suff = suff[new_suff];
suff[new_v] = go[new_suff][get_char_id(c)];
}
}
last = go[last][get_char_id(c)];
return last;
}
const int K = 23;
int up[MAXSZ][K];
int vals[MAXSZ];
int small_suff[MAXSZ];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
node(); // 0 - odd root
node(); // 1 - even root
suff[0] = -1;
len[0] = -1;
suff[1] = 0;
len[1] = 0;
int n;
cin >> n;
string s;
cin >> s;
//for (int i = 0; i < n; ++i)
// s += '0';
//cin >> s;
vector<int> states(2 * n);
int last = 1;
for (int i = 0; i < 2 * n; ++i) {
last = push_back(last, s[i < n ? i : i - n]);
states[i] = last;
}
for (int i = 0; i < nodes_cnt; ++i)
up[i][0] = suff[i];
up[0][0] = 0;
for (int j = 1; j < K; ++j) {
for (int i = 0; i < nodes_cnt; ++i)
up[i][j] = up[up[i][j - 1]][j - 1];
}
for (int i = 2; i < nodes_cnt; ++i) {
if (len[i] <= n)
small_suff[i] = i;
else
small_suff[i] = small_suff[suff[i]];
}
auto add_from_len = [&](int v, int from) {
v = small_suff[v]; // len <= n
if (len[v] < from)
return;
++vals[v];
if (from == 1) {
v = 1;
} else {
for (int j = K - 1; j >= 0; --j) {
if (len[up[v][j]] >= from)
v = up[v][j];
}
v = up[v][0];
}
--vals[v];
};
for (int i = 0; i < 2 * n; ++i) {
add_from_len(states[i], i < n ? 1 : i - n + 2);
}
for (int v = nodes_cnt - 1; v > 0; --v) {
vals[suff[v]] += vals[v];
}
int ans = 0;
for (int v = 2; v < nodes_cnt; ++v) {
ans = add(ans, mul(mul(vals[v], vals[v]), len[v]));
}
cout << ans << '\n';
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11920kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 2ms
memory: 11740kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 2ms
memory: 11728kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 11900kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 11976kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 11924kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 2ms
memory: 11936kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 416ms
memory: 312900kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 1444ms
memory: 907104kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 617ms
memory: 441584kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 1285ms
memory: 907900kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 1266ms
memory: 908632kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 1242ms
memory: 804220kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 1289ms
memory: 837580kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 531ms
memory: 485024kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 155ms
memory: 176784kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 897ms
memory: 775008kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 881ms
memory: 761428kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed