QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#847917#9970. Looping RPSas_lyrWA 7ms47320kbC++142.3kb2025-01-08 13:16:282025-01-08 13:16:28

Judging History

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

  • [2025-01-08 13:16:28]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:47320kb
  • [2025-01-08 13:16:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=110000,M=1100000,B=128,L=3;
const int mask=114,base=19260817,mod=998244853;
inline int pw(int x,int y){
	int z=1;
	while(y){
		if(y&1)
			z=1ll*z*x%mod;
		x=1ll*x*x%mod;
		y>>=1; 
	}
	return z;
}
int n,m;
int c[B];
int len[N];
string s[N];
vector <int> id[M];
vector <int> v[N];
int p[M],pt[M],inv[M];
int nw=1,son[M][L];
int cnt[M],siz[M],hsh[M];
int f[M],g[M];
int d[N];
inline int calc(int i,int j,ll l,ll r){
	int x=l-(l/(j+1))*(j+1);
	int res=hsh[v[i][j]];
	if(x)
		res=(res-1ll*hsh[v[i][x-1]]*p[j-x+1])%mod;
	int sum=(r/(j+1))-(l/(j+1));
	res=1ll*res*pt[sum*(j+1)/len[i]]%mod*p[sum*(j+1)%len[i]]%mod;
	res=(res+1ll*hsh[v[i][j]]*(1ll*pt[sum*(j+1)/len[i]]*p[sum*(j+1)%len[i]]%mod-1)%mod*inv[j+1])%mod;
	int y=r-(r/(j+1))*(j+1);
	res=1ll*res*p[y]%mod;
	res=(res+hsh[v[i][y]])%mod;
	res=(res+mod)%mod;
	return res;
}
int main(){
	c['P']=0,c['N']=1,c['K']=2;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cin>>s[i];
		len[i]=s[i].size();
		int x=1;
		siz[x]++;
		p[0]=1;
		for(int j=1;j<=len[i];j++)
			p[j]=1ll*p[j-1]*base%mod;
		pt[0]=1;
		for(int j=1;j<=len[i];j++)
			pt[j]=1ll*pt[j-1]*p[len[i]]%mod;
		inv[0]=0;
		for(int j=1;j<=len[i];j++)
			inv[j]=pw(p[j]-1,mod-2);
		for(int j=0;j<len[i];j++){
			if(son[x][c[s[i][j]]]==0){
				son[x][c[s[i][j]]]=++nw;
				hsh[nw]=(1ll*hsh[x]*base+c[s[i][j]]+mask)%mod;
			}
			x=son[x][c[s[i][j]]];
			v[i].push_back(x);
			siz[x]++;
		}
		id[x].push_back(i);
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<len[i]-1;j++){
			int y=v[i][j];
			d[i]+=siz[son[y][(c[s[i][j]]+1)%L]];
			f[son[y][(c[s[i][j]]+2)%L]]++;
			if(cnt[y]==0)
				continue;
			int l=0,r=1ll*(j+1)*len[i];
			while(l!=r){
				int mid=(l+r)>>1;
				if(calc(i,j,0,mid)==calc(i,len[i]-1,0,mid))
					l=mid+1;
				else
					r=mid;
			}
			if(r==1ll*(j+1)*len[i])
				g[y]++;
			else{
				int a=calc(i,len[i]-1,l,r)-mask,b=calc(i,j,l,r)-mask;
				if((a+1)%L==b)
					d[i]+=cnt[y];
				else
					g[y]++;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<len[i]-1;j++)
			d[i]+=f[v[i][j]];
		d[i]+=g[v[i][len[i]-1]];
	}
	for(int i=1;i<=nw;i++)
		for(int j=0;j<(int)id[i].size();j++)
			d[id[i][j]]+=j;
	ll ans=1ll*n*(n-1)*(n-2)/6;
	for(int i=1;i<=n;i++)
		ans-=1ll*d[i]*(d[i]-1)/2;
	printf("%lld",ans);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 47320kb

input:

6
P
PN
KK
N
PKK
PN

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: -100
Wrong Answer
time: 7ms
memory: 46968kb

input:

10
KKKNP
KNKPPKNK
KNKPP
KNKPPKN
KKKN
NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN
NNKN
NPPN
NNKNNNKNNNKNNNKNNNKNNNKNNNK
KKKNN

output:

-1035

result:

wrong answer 1st numbers differ - expected: '3', found: '-1035'