QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#847679#9970. Looping RPSIvanZhang2009WA 6ms55092kbC++206.7kb2025-01-08 09:45:232025-01-08 09:45:24

Judging History

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

  • [2025-01-08 09:45:24]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:55092kb
  • [2025-01-08 09:45:23]
  • 提交

answer

/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */

/*
 * @Description: I want to be the weakest vegetable in the world!
 * @Author: I.Z.
*/
#include<bits/stdc++.h>
using namespace std;
#define MOD         998244353
#define speMOD      2933256077ll
#define int         long long
#define pii         pair<int,int>
#define all(v)      v.begin(),v.end()
#define pb          push_back
#define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
#define over(x)     {cout<<(x)<<endl;return;}
#define lowbit(x)   ((x)&(-(x)))
#define cntbit(x)   __builtin_popcount(x)
#define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
#define lbound(v,x) lower_bound(all(v),x)-v.begin()
mt19937 sd(random_device{}());
int qpow(int a,int b,int m=MOD,int res=1){
	a%=m;
	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
	return res;
}
int exgcd(int x,int y,int &a,int &b){
	if(y==0){
		a=1;b=0;
		return x;
	}
	int d=exgcd(y,x%y,a,b);
	int z=b;
	b=a-b*(x/y);
	a=z;
	return d;
}
int _x_,_y_;
inline int inverse(int x,int y=MOD){
	exgcd(x,y,_x_,_y_);
	return (_x_+y)%y;
}
int fac[2000005],inv[2000005];
void init(int n){
	fac[0]=inv[0]=1;
	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
	inv[n]=qpow(fac[n],MOD-2);
	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
}
int binom(int x,int y){
	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
struct SA{
    string s;
    int n;
    int sa[2000005],rk[2000005],id[2000005],cnt[2000005],old[2000005],ht[2000005],st[25][2000005];
    bool same(int x,int y,int w){return x+w<n&&y+w<n&&old[x]==old[y]&&old[x+w]==old[y+w];}
    int query(int l,int r){
        if(rk[l]>rk[r])swap(l,r);
        l=rk[l]+1,r=rk[r];
        int S=__lg(r-l+1);
        return min(st[S][l],st[S][r-(1<<S)+1]);
    }
    void build(string S){
        s=S;n=s.size();int m=200;
        REP(i,0,m)cnt[i]=0;
        REP(i,0,n)++cnt[rk[i]=s[i]];
        REP(i,1,m)cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;--i)sa[--cnt[rk[i]]]=i;
        for(int w=1;w<n;w<<=1){
            int cur=0;
            for(int i=n-1;i>=n-w;--i)id[cur++]=i;
            REP(i,0,n)if(sa[i]>=w)id[cur++]=sa[i]-w;
            REP(i,0,m)cnt[i]=0;
            REP(i,0,n)old[i]=rk[i],++cnt[rk[id[i]]];
            REP(i,1,m)cnt[i]+=cnt[i-1];
            for(int i=n-1;i>=0;--i)sa[--cnt[rk[id[i]]]]=id[i];
            cur=1;rk[sa[0]]=0;
            REP(i,1,n)if(same(sa[i],sa[i-1],w))rk[sa[i]]=cur-1;else rk[sa[i]]=cur++;
            if(cur==n)break;else m=cur;
        }
        ht[rk[0]]=0;
        REP(i,0,n)if(rk[i]){
        	int x=max(0ll,ht[rk[i-1]]-1),y=sa[rk[i]-1];
		    while(i+x<n&&y+x<n&&s[i+x]==s[y+x])++x;
		    ht[rk[i]]=x;
        }
        REP(i,1,n)st[0][i]=ht[i];
        REP(j,0,__lg(n-1)){
            REP(i,1,n-(1<<(j+1))+1)st[j+1][i]=min(st[j][i],st[j][i+(1<<j)]);
        }
    }
}sa;
int n;
string s[100005];
int N[100005];
int ps[100005];
int son[1000005][3];
int cnt[1000005];
int tot=1;
int id[200];
int d[100005];
int wh[100005];
vector<int>v[1000005],v2[1000005];
int lcp(int x,int l,int y,int r){
	return min({N[x]-l,N[y]-r,sa.query(ps[x]+l,ps[y]+r)});
}
int addstring(string t,int tt=-1){
	int c=0;
	for(auto i:t){
		int &p=son[c][id[i]];
		if(p==-1){
			p=tot++;cnt[p]=0;
			REP(j,0,3)son[p][j]=-1;
		}
		c=p;
	}
	++cnt[c];
	v[c].pb(tt);
	return c;
}
int sum[1000005],sum2[1000005],cnt2[1000005];
int totadd[1000005];
int fa[1000005];
void dfs(int x,int val,int val2){
	for(auto i:v[x])sum[i]=val,sum2[i]=val2;
	REP(j,0,3)if(son[x][j]!=-1){
		int s=0,J=(j+2)%3,s2=0;
		if(son[x][J]!=-1)s+=cnt[son[x][J]],s2+=cnt2[son[x][J]];
		dfs(son[x][j],val+s,val2+s2);
	}
}
bool win(int x,int y){
	int t1=0,t2=0;
	while(1){
		if(s[x][t1]!=s[y][t2]){
			return (id[s[x][t1]]+2)%3==id[s[y][t2]];
		}
		int l=lcp(x,t1,y,t2);
		(t1+=l)%=N[x];
		(t2+=l)%=N[y];
	}
}
void Main() {
	id['P']=0;id['N']=1;id['K']=2;
	cin>>n;
	string T="";
	REP(i,0,n)ps[i]=T.size(),cin>>s[i],T+=s[i],N[i]=s[i].size();
	sa.build(T);
	REP(i,0,3)son[0][i]=-1;
	REP(i,0,n){
		d[i]=N[i];
		REP(j,1,N[i]/2+1)if(N[i]%j==0){
			if(lcp(i,0,i,j)>=N[i]-j){
				d[i]=j;
				break;
			}
		}
		s[i]=s[i].substr(0,d[i]);
	}
	REP(i,0,n)wh[i]=addstring(s[i],i);
	int ans=n*(n-1)*(n-2)/6;
	REP(i,0,tot)if(cnt[i]>1){
		int x=cnt[i];
		ans-=x*(x-1)*(x-2)/6;
		ans-=x*(x-1)/2*(n-x);
	}
	REP(i,0,tot){
		REP(j,0,3)if(son[i][j]!=-1)fa[son[i][j]]=i;
	}
	REP(i,0,n){
		int x=fa[wh[i]];
		while(x)++cnt[x],x=fa[x];
	}
	REP(i,0,tot)if(v[i].size()>1){
		int x=i,y=v[i].size();y=y*(y-1)/2;
		while(x)cnt2[x]+=y,x=fa[x];
	}
	dfs(0,0,0);
	REP(i,0,n)ans+=sum2[i];
	REP(i,0,n){
		int x=wh[i];x=fa[x];
		while(x){
			if(v[x].size()){
				if(win(v[x][0],i))++totadd[x],v2[x].pb(wh[i]);
				else sum[i]+=v[x].size(),ans+=v[x].size()*(v[x].size()-1)/2;
			}
			x=fa[x];
		}
	}
	REP(i,0,tot)if(totadd[i]){
		sort(all(v2[i]));
		int lst=0;
		REP(j,1,v2[i].size()+1)if(j==v2[i].size()||v2[i][j]!=v2[i][j-1]){
			int len=j-lst;lst=j;
//			cout<<len<<endl;
			ans+=len*(len-1)/2*v[i].size();
		}
		for(auto j:v[i])sum[j]+=totadd[i];
	}
//	REP(i,0,n)cout<<sum[i]<<' ';
//	cout<<endl;
	REP(i,0,n)ans-=sum[i]*(sum[i]-1)/2;
	over(ans)
}
void TC() {
    int tc=1;
	while(tc--){
		Main();
		cout.flush();
	}
}
signed main() {
	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
}
/*
1. CLEAR the arrays (ESPECIALLY multitests)
2. DELETE useless output
 */

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 40372kb

input:

6
P
PN
KK
N
PKK
PN

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 0ms
memory: 44576kb

input:

10
KKKNP
KNKPPKNK
KNKPP
KNKPPKN
KKKN
NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN
NNKN
NPPN
NNKNNNKNNNKNNNKNNNKNNNKNNNK
KKKNN

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 0ms
memory: 46788kb

input:

10
NNNPNNNPNNNPNNNK
KKN
NNNP
KKP
NNNPNNNPNNNPN
KKNKKNKKPN
KNNPNPNKKKNPPKNKKKNKNKKNKPPPNKKPKP
KKPK
KKNKKNK
KKPKKN

output:

3

result:

ok 1 number(s): "3"

Test #4:

score: 0
Accepted
time: 3ms
memory: 46852kb

input:

10
K
PPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNP
PPKP
PPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPK
P
K
N
P
PPPNN
N

output:

25

result:

ok 1 number(s): "25"

Test #5:

score: 0
Accepted
time: 6ms
memory: 46656kb

input:

10
NPNKP
NNNNKNNNNPP
PPKPNNNNPNKKKN
NPNKPNP
NNNNKN
NNNNK
NKNPKKPNPKKNPNKN
NKNPKKPNPKKNPNK
NKNPKKPNPKKNP
NPNKPNPN

output:

30

result:

ok 1 number(s): "30"

Test #6:

score: 0
Accepted
time: 4ms
memory: 44800kb

input:

10
KPKKPKKPKKPKKP
KPKKPKKPKKPKKPKNK
PNPNP
KPK
PN
NPNPNNPNPNK
NKKPKKPKPPKKPKKKKPKNKPPKPPNKNP
NPNPNNP
PNPNPK
NPNPN

output:

39

result:

ok 1 number(s): "39"

Test #7:

score: 0
Accepted
time: 0ms
memory: 44520kb

input:

4
KKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPK
NN
KKP
KKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKNK

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 5ms
memory: 48684kb

input:

7
KPKN
KPKNKPKNKPKNKPKK
NKPPNNNPKKNN
KPPKPKPPKPKPPKPKPPKPKPP
KPKNKPKNKPKNKP
KPPKP
KPPKPKPPKPKPPKPKPPKPKPPKPN

output:

2

result:

ok 1 number(s): "2"

Test #9:

score: 0
Accepted
time: 3ms
memory: 48608kb

input:

10
NKNNKNKN
KPKN
PKPN
PNNNNNNKKNNPNNKNPPKPPNPNPPKKKPNNNPNPKKNK
PKPNPKP
PKPNPK
KPKNKP
NKNNKNKNNKNPN
KPKNKPK
NKNNK

output:

39

result:

ok 1 number(s): "39"

Test #10:

score: -100
Wrong Answer
time: 0ms
memory: 55092kb

input:

300
NKNPNK
NKKNKK
KPPNPN
KKPNKNK
PKKNPKP
KPKPPPN
NNKPPNN
NPKPPKN
KNNKKPK
PPPNPKK
NKPKNP
KPKNNPP
NNPKNP
PNPPPKN
PKKPNP
PPNNKK
PKNKNK
PKNPNK
NKNPNPP
KNKNNPN
NKPPPPK
NNPPKKN
KNKKNPK
KKNNPKN
PPPKNK
NPPPPPP
NKKPKPP
KNKNPPK
KPKPNNK
NPNNKN
PNPNKP
PNPKKP
KKKKPKN
NNNKNPK
NPNKPNK
NNNKNK
PPKKNKP
NNNKPPK
KPNKPP...

output:

1103124

result:

wrong answer 1st numbers differ - expected: '1102940', found: '1103124'