QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#501490 | #7876. Cyclic Substrings | emt | WA | 1106ms | 991432kb | C++20 | 3.5kb | 2024-08-02 19:25:36 | 2024-08-02 19:25:37 |
Judging History
answer
// #pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using ld = long double;
using i128 = __int128_t;
using u128 = __uint128_t;
// #define int long long
#define mem(a, b) memset((a), (b), sizeof(a))
#define all(a) (a).begin(), (a).end()
#define inf 0x3f3f3f3f
#define lowbit(x) (-x) & x
#define debug cout<<"$"
#define ls u << 1
#define rs u << 1 | 1
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
using pii = pair<int,int>;
mt19937 rnd(time(0));
const int MOD = 998244353;
const ld eps = 1;
int qmi(int m, int k){int res = 1, t = m;while (k){if (k & 1) res = (res * t) % MOD;t = t * t % MOD;k >>= 1;}return res;}
const int N = 6e6 + 7;
const int P = 13331;
time_t now = time(0);
struct mana
{
char s[N*2];
int k = 0;
int d[N*2]; // 回文半径函数
void init(string t)
{
int n = t.size()-1;
s[0] = '$', s[++k] = '#';
for (int i = 1; i <= n; i++)
s[++k] = t[i], s[++k] = '#';
}
void get_d(){
d[1]=1;
for(int i=2,l,r=1;i<=k;i++){
if(i<=r)d[i]=min(d[r-i+l],r-i+1);
while(s[i-d[i]]==s[i+d[i]])d[i]++;
if(i+d[i]-1>r)l=i-d[i]+1,r=i+d[i]-1;
}
}
}man[2];
unordered_map<u_short,u_short> mb;
u64 h[N], p[N];
vector<unordered_map<u_short,int>> id(N);
u64 get(int l, int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}
void solve()
{
int n;
string s0;
cin>>n>>s0;
int t = n;
man[1].init(" "+s0);
s0 = " " + s0 + s0;
man[0].init(s0);
p[0] = 1;
for (int i = 1; i <= 2*n; i ++ )
{
h[i] = h[i - 1] * P + s0[i];
p[i] = p[i - 1] * P;
}
man[0].get_d();
man[1].get_d();
int cnt = 0, cnt1 = 0;
for(int q=0;q<2;q++)
for (int i = 2; i <= man[q].k; i++)
{
int len;
int l,r;
if(i&1)
{
len = min(t/2*2,man[q].d[i]-1);
l = i/2-(len/2-1);
r = i/2+(len/2-1)+1;
}
else
{
len = min(((t-1)/2*2+1),man[q].d[i]-1);
l = i/2-len/2;
r = i/2+len/2;
}
if(!len) continue;
auto z = get(l,r);
// debug<<i<<"\n";
// debug<<l<<" "<<r<<" "<<len<<" "<<q<<"\n";
id[len][z] += (q?-1:1);
while(!mb.count(z)&&l<=r)
{
l++,r--;
auto t = get(l,r);
mb[z] = t;
z = t;
}
}
// cout<<cnt<<" "<<cnt1<<"\n";
// std::cout << "当前时间戳(毫秒): " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() << std::endl;
int ans = 0;
for(int i=n;i;i--)
{
for(auto [c,d]:id[i])
{
ans += 1ll*d*d%MOD*i%MOD;
ans %= MOD;
if(i>2) id[i-2][mb[c]] += d;
}
}
// while(ma.size())
// {
// auto [c,d] = *ma.begin();
// ma.erase(ma.begin());
// auto [len,hash] = c;
// ans += (-len)*d%MOD*d%MOD;
// ans %= MOD;
// if(-len>=2)
// ma[{len+2,mb[hash]}]+=d;
// }
cout<<ans<<"\n";
}
signed main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin>>T;
while(T--)
solve();
}
/*
6677667766776677
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 59ms
memory: 339404kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 48ms
memory: 339256kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 43ms
memory: 339408kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 62ms
memory: 339456kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 63ms
memory: 339372kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 47ms
memory: 339336kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 48ms
memory: 339304kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 401ms
memory: 563004kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 1087ms
memory: 991432kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: -100
Wrong Answer
time: 1106ms
memory: 747396kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
205493395
result:
wrong answer 1st numbers differ - expected: '224009870', found: '205493395'