QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#354041#1287. Anti-hash TestDiuWA 4ms27404kbC++143.1kb2024-03-14 20:48:552024-03-14 20:48:57

Judging History

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

  • [2024-03-14 20:48:57]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:27404kb
  • [2024-03-14 20:48:55]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+10,mod=1e9+7,inv2=mod+1>>1,inv3=(mod+1)/3;
int T;
ll n;
int a[N],b[N];
char s[N];
int qpow(int x,ll y=mod-2){
	int m=1;y%=(mod-1);
	for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)m=1ll*m*x%mod;
	return m;
}
#define NO {puts("-1");return;}
namespace Baoli{
	int len[N],fa[N],nxt[N][2];
	int tot,lst,f[N];
	vector<int> G[N];
	void init(){
		for(int i=0;i<=tot;i++)len[i]=fa[i]=nxt[i][0]=nxt[i][1]=f[i]=0,G[i].clear();
		len[0]=tot=lst=0,fa[0]=-1;
	}
	void ins(int c){
		int cur=++tot;f[cur]=1;
		len[cur]=len[lst]+1;
		int p=lst;
		while(p!=-1&&!nxt[p][c])nxt[p][c]=cur,p=fa[p];
		if(p==-1)fa[cur]=0;
		else{
			int q=nxt[p][c];
			if(len[p]+1==len[q])fa[cur]=q;
			else{
				int cl=++tot;
				len[cl]=len[p]+1;
				for(int i=0;i<2;i++)nxt[cl][i]=nxt[q][i];
				fa[cl]=fa[q];
				while(p!=-1&&nxt[p][c]==q)nxt[p][c]=cl,p=fa[p];
				fa[q]=fa[cur]=cl;
			}
		}
		lst=cur;
	}
	void dfs(int u){
		for(int v:G[u])dfs(v),f[u]+=f[v];
	}
	void solve(int n,int m){
		init();
		for(int i=0;i<(1<<n);i++)ins(__builtin_popcount(i)&1);
		for(int i=1;i<=tot;i++)G[fa[i]].push_back(i);
		dfs(0);
		int p=0;
		for(int i=1;i<=m;i++){
			p=nxt[p][s[i]-'a'];
			if(!p)NO;
		}
		int ans1=f[p],ans2=0;
		for(int i=1;i<=tot;i++)if(f[i]==ans1)ans2+=len[i]-len[fa[i]];
		printf("%d %d\n",ans1,ans2);
	}
}
int pw2[30],f1[30],f2[30],g1[30],g2[30];
void init(int n){
	pw2[0]=1;
	for(int i=1;i<=n;i++)pw2[i]=(pw2[i-1]+pw2[i-1])%mod;
	f1[0]=f1[1]=1,f1[2]=2,f2[2]=1;
	for(int i=3;i<=n;i++){
		f1[i]=(f1[i-1]+2ll*f2[i-1])%mod;
		f2[i]=f1[i-1];
	}
	g1[0]=g1[1]=g2[0]=g2[1]=1;
	g1[2]=4,g2[2]=6;
	for(int i=3;i<=n;i++){
		g1[i]=(1ll*f1[i-1]*pw2[i-1]+7ll*f2[i-1]*pw2[i-3])%mod;
		g2[i]=(g1[i]+1ll*f1[i]*pw2[i-1])%mod;
	}
}
int calc(int c){
	return (1ll*f1[c-2]*(pw2[c-2]+pw2[c-1]+pw2[c-3]+pw2[c-2]+pw2[c-3]+pw2[c-2]+pw2[c-2])+1ll*f2[c-2]*(pw2[c-3]+pw2[c-4]+pw2[c-3]+pw2[c-1]+pw2[c-3]+pw2[c-4]+pw2[c-3]+pw2[c-1]))%mod;
}
void work(ll k,int n,bool x,int c){
	if(n>=5)NO;
	if(n==4)c+=3,x^=1;
	if(n==3)c+=2,x=1;
	if(n==2)c+=1;
	if(k<=c)NO;
	if(n==1){
		printf("%d %d\n",qpow(2,k-1),2);
		return;
	}
	if(c+1==k){
		printf("%d %d\n",1,calc(k));
		return;
	}
	if(k-c&1){
		int ans1=1ll*(qpow(2,k-c+1)-1)*inv3%mod;
		int ans2=(g1[c]+g2[c])%mod;
		printf("%d %d\n",ans1,ans2);
	}else{
		int ans1=1ll*(qpow(2,k-c+2)-1)*inv3%mod;
		int ans2;
		if(x)ans1=1ll*(ans1-1)*inv2%mod,ans2=g2[c];
		else ans1=1ll*(ans1+1)*inv2%mod,ans2=g1[c];
		printf("%d %d\n",ans1,ans2);
	}
}
void solve(ll k,int n){
	int c=0;
	while(1){
		int i=2;
		while(i<=n&&a[i]!=a[i-1])++i;
		if(i>n)return work(k,n,a[1],c);
		if(i&1){
			for(int j=1;j<=n;j+=2){
				if(a[j]==a[j+1]){puts("-1");return;}
				a[j+1>>1]=a[j];
			}
			n=n+1>>1;
		}else{
			a[0]=a[1]^1;
			for(int j=0;j<=n;j+=2){
				if(a[j]==a[j+1]){puts("-1");return;}
				a[j+2>>1]=a[j];
			}
			n=n+2>>1;
		}
		++c;
	}
}
signed main(){
	scanf("%d",&T);
	init(25);
	for(;T--;){
		scanf("%lld\n%s",&n,s+1);
		int len=strlen(s+1);
		for(int i=1;i<=len;i++)a[i]=s[i]=='b';
		if(n<=5)Baoli::solve(n,len);
		else solve(n,len);
	}
}

詳細信息

Test #1:

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

input:

4
10
a
10
abba
5
abbabaabbaababbabaababbaabbabaab
20
ababab

output:

512 2
171 4
1 344
-1

result:

ok 7 tokens

Test #2:

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

input:

3
1
a
1
ab
1
b

output:

1 3
1 3
1 3

result:

ok 6 tokens

Test #3:

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

input:

10
2
a
2
ab
2
abb
2
abba
2
b
2
bb
2
bba
2
b
2
ba
2
a

output:

2 2
1 6
1 6
1 6
2 2
1 6
1 6
2 2
1 6
2 2

result:

ok 20 tokens

Test #4:

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

input:

36
3
a
3
ab
3
abb
3
abba
3
abbab
3
abbaba
3
abbabaa
3
abbabaab
3
b
3
bb
3
bba
3
bbab
3
bbaba
3
bbabaa
3
bbabaab
3
b
3
ba
3
bab
3
baba
3
babaa
3
babaab
3
a
3
ab
3
aba
3
abaa
3
abaab
3
b
3
ba
3
baa
3
baab
3
a
3
aa
3
aab
3
a
3
ab
3
b

output:

4 2
3 1
1 23
1 23
1 23
1 23
1 23
1 23
4 2
1 23
1 23
1 23
1 23
1 23
1 23
4 2
2 1
1 23
1 23
1 23
1 23
4 2
3 1
1 23
1 23
1 23
4 2
2 1
1 23
1 23
4 2
1 23
1 23
4 2
3 1
4 2

result:

ok 72 tokens

Test #5:

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

input:

136
4
a
4
ab
4
abb
4
abba
4
abbab
4
abbaba
4
abbabaa
4
abbabaab
4
abbabaabb
4
abbabaabba
4
abbabaabbaa
4
abbabaabbaab
4
abbabaabbaaba
4
abbabaabbaabab
4
abbabaabbaababb
4
abbabaabbaababba
4
b
4
bb
4
bba
4
bbab
4
bbaba
4
bbabaa
4
bbabaab
4
bbabaabb
4
bbabaabba
4
bbabaabbaa
4
bbabaabbaab
4
bbabaabbaab...

output:

8 2
5 2
3 4
3 4
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
8 2
3 4
3 4
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
8 2
5 2
2 6
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
8 2
5 2
2 6
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
1 86
8 2
5 2
2 6
2 6
1 86
1 ...

result:

ok 272 tokens

Test #6:

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

input:

528
5
a
5
ab
5
abb
5
abba
5
abbab
5
abbaba
5
abbabaa
5
abbabaab
5
abbabaabb
5
abbabaabba
5
abbabaabbaa
5
abbabaabbaab
5
abbabaabbaaba
5
abbabaabbaabab
5
abbabaabbaababb
5
abbabaabbaababba
5
abbabaabbaababbab
5
abbabaabbaababbaba
5
abbabaabbaababbabaa
5
abbabaabbaababbabaab
5
abbabaabbaababbabaaba
5
...

output:

16 2
11 1
5 10
5 10
3 13
3 13
3 13
3 13
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
16 2
5 10
5 10
3 13
3 13
3 13
3 13
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 344
1 3...

result:

ok 1056 tokens

Test #7:

score: -100
Wrong Answer
time: 4ms
memory: 27204kb

input:

2080
6
a
6
ab
6
abb
6
abba
6
abbab
6
abbaba
6
abbabaa
6
abbabaab
6
abbabaabb
6
abbabaabba
6
abbabaabbaa
6
abbabaabbaab
6
abbabaabbaaba
6
abbabaabbaabab
6
abbabaabbaababb
6
abbabaabbaababba
6
abbabaabbaababbab
6
abbabaabbaababbaba
6
abbabaabbaababbabaa
6
abbabaabbaababbabaab
6
abbabaabbaababbabaaba
6...

output:

32 2
21 2
11 4
11 4
5 46
5 46
-1
5 46
3 60
3 60
-1
3 60
-1
3 60
3 60
3 60
1 1312
1 1312
-1
1 1312
-1
1 1312
1 1312
1 1312
-1
1 1312
1 1312
1 1312
1 1312
1 1312
-1
1 1312
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
32 2
-1
11 4
-1
-1
-1
5 46
-1
3 60...

result:

wrong answer 10th words differ - expected: '34', found: '46'