QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#339596#7876. Cyclic Substrings275307894a#TL 1619ms1041108kbC++142.3kb2024-02-27 16:33:432024-02-27 16:33:43

Judging History

你现在查看的是最新测评结果

  • [2024-02-27 16:33:43]
  • 评测
  • 测评结果:TL
  • 用时:1619ms
  • 内存:1041108kb
  • [2024-02-27 16:33:43]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=6e6+5,M=N*10+5,K=1000+5,mod=998244353,Mod=+-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int n;char s[N];vector<int> S[N];
int buf[N],*f[N],*head=buf,siz[N],pl[N];
namespace bit{
	int f[N];
	void add(int x){while(x<=2*n) f[x]++,x+=x&-x;}
	int qry(int x){int ans=0;while(x) ans+=f[x],x-=x&-x;return ans;}
}
namespace PAM{
	int son[N][10],link[N],cnt=1,len[N];
	int find(int x,int id){
		while(x^1&&s[id]^s[id-len[x]-1]) x=link[x];
		return x;
	}
	ll ans;
	void dfs(int x){
		int w=0;
		if(len[x]<=n&&len[x]>0){
			w=bit::qry(n+len[x]-1);
		}
		for(int i=0;i<siz[x];i++) bit::add(f[x][i]);
		for(int i:S[x]) dfs(i);
		if(len[x]<=n&&len[x]>0){
			w=bit::qry(n+len[x]-1)-w;
			// gdb(len[x],w);
			ans+=1ll*w*w%mod*len[x]%mod;
		}
	}
	void build(){
		link[0]=1;len[1]=-1;int x=0;
		for(int i=1;i<=2*n;i++){
			x=find(x,i);
			if(!son[x][s[i]-'0']){
				len[++cnt]=len[x]+2;
				link[cnt]=son[find(link[x],i)][s[i]-'0'];
				son[x][s[i]-'0']=cnt;
			}
			x=son[x][s[i]-'0'];
			pl[i]=x;
		}
		for(int i=1;i<=2*n;i++) siz[pl[i]]++;
		for(int i=1;i<=cnt;i++) f[i]=head,head+=siz[i],siz[i]=0;
		for(int i=1;i<=2*n;i++) f[pl[i]][siz[pl[i]]++]=i;
		for(int i=0;i<=cnt;i++) if(i^1) S[link[i]].emplace_back(i);
		dfs(1);
		printf("%lld\n",ans%mod);
	}
}
void Solve(){
	int i,j;scanf("%d",&n);
	scanf("%s",s+1);for(i=n+1;i<=2*n;i++) s[i]=s[i-n];
	PAM::build();
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 145436kb

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

score: 0
Accepted
time: 19ms
memory: 145504kb

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

score: 0
Accepted
time: 12ms
memory: 145432kb

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 12ms
memory: 146128kb

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

score: 0
Accepted
time: 22ms
memory: 146400kb

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

score: 0
Accepted
time: 19ms
memory: 145768kb

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 15ms
memory: 145620kb

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 157ms
memory: 445036kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 439ms
memory: 1041108kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 363ms
memory: 454244kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 452ms
memory: 900484kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 454ms
memory: 853648kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 421ms
memory: 674900kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 495ms
memory: 656284kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 1619ms
memory: 448412kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 731ms
memory: 287764kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: -100
Time Limit Exceeded

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:


result: