QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#314989 | #7876. Cyclic Substrings | oql | ML | 135ms | 447884kb | C++17 | 3.1kb | 2024-01-26 16:45:44 | 2024-01-26 16:45:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++)
#define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++)
#define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--)
#define all(x) (x).begin(),(x).end()
#define cle(x) memset(x,0,sizeof(x))
#define lowbit(x) ((x)&-(x))
#define VI vector<int>
#define ll long long
#define ull unsigned ll
#define lll __int128
#define db double
#define lb long db
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define endl "\n"
template<class T>inline void read(T &x) {
x=0; char ch=getchar(); bool f=0;
for(;ch<'0'||ch>'9';ch=getchar()) f|=(ch=='-');
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+(ch^48);
if(f) x=-x;
}
template<class T, class... V>
inline void read(T &a, V&... b){read(a); read(b...);}
#ifndef ONLINE_JUDGE
#define debug(x) cerr<<#x<<"="<<x<<endl
#define debugv(x) cerr<<#x<<" : "; ff(i,0,(x).size()) cerr<<(x)[i]<<(i==(x).size()-1?'\n':' ')
#else
#define debug(x)
#define debugv(x)
#endif
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353;
inline ll Pow(ll x,ll y) {
ll ans = 1;
for(; y; y >>= 1, x = x * x % mod)
if(y & 1)
ans = ans * x % mod;
return ans;
}
inline ll Add(ll x,ll y) {
x += y;
return (x >= mod) ? x - mod : x;
}
inline ll Dec(ll x,ll y) {
x -= y;
return (x < 0) ? x + mod : x;
}
inline ll Mul(ll x,ll y) {
return 1ll * x * y % mod;
}
const int N = 6e6 + 5;
const int S = 10;
namespace PAM {
int n,s[N],num[N],cnt,las,nex[N][S],fail[N],len[N];
int quick[N][S];
void init() {
s[n=0]=len[cnt=1]=-1;
fail[las=0]=fail[1]=1;
for (int i = 0; i < S; i ++) {
quick[0][i] = quick[1][i] = 1;
}
}
void add(int c, bool flag) {
s[++n] = c;
int p = las;
if (s[n - len[p] - 1] != s[n]) p = quick[p][c];
if(!nex[p][c]) {
int np = ++cnt, q = fail[p];
len[np] = len[p]+2;
if (s[n - len[q] - 1] != s[n]) q = quick[q][c];
fail[np] = nex[q][c];
memcpy(quick[np], quick[fail[np]], sizeof(quick[np]));
quick[np][s[n - len[fail[np]]]] = fail[np];
nex[p][c] = np;
}
las = nex[p][c];
if (flag) num[las] ++;
}
vector<int> adj[N];
void dfs(int u) {
for (auto v : adj[u]) {
dfs(v);
num[u] += num[v];
}
}
ll work(int n) {
fo(i,0,cnt) if (i != 1) adj[fail[i]].pb(i);
dfs(1);
ll ans = 0;
fo(i,2,cnt)
if (len[i] <=n)
ans = Add(ans, Mul(len[i], Mul(num[i], num[i])));
return ans;
}
}
int n;
char s[N];
int main() {
read(n);
scanf("%s", s + 1);
PAM::init();
for (int i = 1; i <= n; i ++)
PAM::add(s[i] - '0', 0);
for (int i = 1; i <= n; i ++)
PAM::add(s[i] - '0', 1);
printf("%lld\n", PAM::work(n));
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 15ms
memory: 145428kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 11ms
memory: 146272kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 19ms
memory: 145052kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 22ms
memory: 145952kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 19ms
memory: 145608kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 16ms
memory: 144944kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 19ms
memory: 145212kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 135ms
memory: 447884kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: -100
Memory Limit Exceeded
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718