QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#649658 | #7876. Cyclic Substrings | Tmonkey | AC ✓ | 893ms | 770856kb | C++20 | 4.8kb | 2024-10-18 08:33:40 | 2024-10-18 08:33:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define YES "YES"
#define NO "NO"
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
const double eps = 1e-9;
const int N = 3e6 + 100;
const int M = 6e6 + 100;
const ll mod = 998244353;
const int base = 23333;
mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int n, cnt, flag = 1;
ll ans;
string s;
int d[N + N / 2];
int nxt[M][24];
int lef[N + N / 2];
struct node
{
int son[10], h = 0, fa = 0, deg = 0;
ll num = 0, add = 0;
} trie[M];
void topu()
{
queue<int> q;
for (int i = 0; i <= cnt; i++)
{
if (trie[i].deg == 0)
q.push(i);
}
while (!q.empty())
{
int u = q.front();
q.pop();
int v = trie[u].fa;
trie[v].add = (trie[v].add + trie[u].add) % mod;
trie[v].deg--;
if (trie[v].deg == 0)
q.push(v);
trie[u].num = (trie[u].num + trie[u].add) % mod;
if (flag)
ans = (ans + trie[u].num * trie[u].num % mod * (max(0, 2 * trie[u].h - 1)) % mod) % mod;
else
ans = (ans + trie[u].num * trie[u].num % mod * (2 * trie[u].h) % mod) % mod;
}
}
void clear()
{
for (int i = 0; i <= cnt; i++)
{
for (int j = 0; j < 10; j++)
{
trie[i].son[j] = 0;
}
trie[i].add = trie[i].deg = trie[i].fa = trie[i].h = trie[i].num = 0;
}
}
int get(int u, int k)
{
int i = 0;
while (k)
{
if (k & 1)
u = nxt[u][i];
k = k >> 1;
i++;
}
return u;
}
void solve()
{
cin >> n;
cin >> s;
s = s + s;
// 奇数
// n = 1000000;
// s = "";
// for (int i = 1; i <= n; i++)
// {
// s += '2';
// }
// s = s + s;
// printf("YES\n");
for (int i = 0, l = 0, r = -1; i < n + n / 2; i++)
{
int k = 0, pos = 0;
if (i <= r)
{
k = min(d[l + r - i], r - i + 1);
pos = lef[l + r - i];
if (d[l + r - i] > r - i + 1)
pos = get(pos, trie[pos].h - (r - i + 1));
}
if (pos)
trie[pos].add++;
while (0 <= i - k && i + k < 2 * n && k < (n + 1) / 2 && s[i - k] == s[i + k])
{
int v = s[i + k] - '0';
if (!trie[pos].son[v])
{
trie[pos].son[v] = ++cnt;
trie[pos].deg++;
}
int x = trie[pos].son[v];
trie[x].fa = pos;
trie[x].num++;
trie[x].h = trie[pos].h + 1;
// update
nxt[x][0] = pos;
for (int j = 1; j < 24; j++)
{
nxt[x][j] = nxt[nxt[x][j - 1]][j - 1];
}
pos = trie[pos].son[v];
k++;
}
d[i] = k--;
lef[i] = pos;
if (i + k > r)
{
l = i - k;
r = i + k;
}
}
for (int i = 0; i < n / 2; i++)
{
trie[lef[i]].add = (trie[lef[i]].add + mod - 1) % mod;
}
// printf("YES\n");
topu();
clear();
flag = 0;
// printf("YES\n");
// 偶数
cnt = 0;
for (int i = 0, l = 0, r = -1; i < n + n / 2; i++)
{
int k = 0, pos = 0;
if (i <= r)
{
k = min(d[l + r - i + 1], r - i + 1);
pos = lef[l + r - i + 1];
if (d[l + r - i + 1] > r - i + 1)
pos = get(pos, trie[pos].h - (r - i + 1));
}
if (pos)
trie[pos].add++;
while (0 <= i - k - 1 && i + k < 2 * n && k < n / 2 && s[i - k - 1] == s[i + k])
{
int v = s[i + k] - '0';
if (!trie[pos].son[v])
{
trie[pos].son[v] = ++cnt;
trie[pos].deg++;
}
int x = trie[pos].son[v];
trie[x].fa = pos;
trie[x].num++;
trie[x].h = trie[pos].h + 1;
// update
nxt[x][0] = pos;
for (int j = 1; j < 24; j++)
{
nxt[x][j] = nxt[nxt[x][j - 1]][j - 1];
}
pos = trie[pos].son[v];
k++;
}
d[i] = k--;
lef[i] = pos;
if (i + k > r)
{
l = i - k - 1;
r = i + k;
}
}
for (int i = 0; i < n / 2; i++)
{
trie[lef[i]].add = (trie[lef[i]].add + mod - 1) % mod;
}
topu();
cout << ans << '\n';
// printf("%lld\n", ans);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 15ms
memory: 430688kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 15ms
memory: 430624kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 11ms
memory: 431744kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 15ms
memory: 431308kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 19ms
memory: 429824kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 30ms
memory: 431484kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 19ms
memory: 429980kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 257ms
memory: 490672kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 893ms
memory: 609152kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 643ms
memory: 680452kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 583ms
memory: 750352kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 777ms
memory: 608804kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 721ms
memory: 740040kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 737ms
memory: 733980kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 720ms
memory: 633312kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 417ms
memory: 520980kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 592ms
memory: 770856kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 641ms
memory: 766860kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed