QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#239194#7518. GCD of Pattern MatchingchaoticWA 0ms3800kbC++141.8kb2023-11-04 19:08:242023-11-04 19:08:25

Judging History

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

  • [2023-11-04 19:08:25]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3800kb
  • [2023-11-04 19:08:24]
  • 提交

answer

/*
think:30min
maybe it's really trivial
maybe I need 30min to solve it
coding:12:05
failure
*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN=20;
int t,m,n,ret,ans,l;
char p[MAXN];
bool vis[MAXN],flag;
int w[210];
long long v[MAXN],s;
int num,now;
long long fans,pw[MAXN];
long long gcd(long long x,long long y)
{
	return y==0?x:gcd(y,x%y);
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %s",&m,p+1);
		pw[0]=1;
		for(int i=1;i<=16;i++) pw[i]=pw[i-1]*m;
		l=strlen(p+1);
		for(int i=1;i<=l/2;i++) swap(p[i],p[l-i+1]);
		ret=0;
		for(int i=0;i<l;i++)
		{
			for(int j=1;j<=l;j++)
			{
				if((i+1)*(j-1)+1>l) continue;
				memset(vis,0,sizeof(vis));
				flag=0;
				for(int k=1;k<=l;k++)
				{
					if(flag) break;
					if(!vis[k])
					{
						vis[k]=1;
						for(int o=1;o<=j-1;o++)
						{
							if(k+o*(i+1)>l||vis[k+o*(i+1)]||p[k+o*(i+1)]!=p[k])
							{
								flag=1;
								break;
							}
							vis[k+o*(i+1)]=1;
						}
					}
				}
				if(!flag)
				{
					now=0;
					for(int k=1;k<=j;k++)
						now|=1<<((k-1)*(i+1));
					ret=max(ret,now);
				}
			}
		}
		if(__builtin_popcount(ret)!=n)
		{
			ans=m-1;
			memset(vis,0,sizeof(vis));
			now=0;
			num=0;
			for(int i=1;i<=l;i++)
			{
				if(!((1<<i-1)&now))
				{
					if(!w[p[i]]) w[p[i]]=++num;
					v[w[p[i]]]+=pw[i-1];
					now|=ret<<i-1;
				}
			}
			for(int i=1;i<=l;i++) w[p[i]]=0;
			if(num==m)
			{
				for(int i=1;i<num;i++)
					ans=gcd(ans,abs(v[i]-v[i+1]));
			}
			else
			{
				for(int i=1;i<=num;i++)
					ans=gcd(ans,v[i]);
			}
			s=0;
			for(int i=1;i<=num;i++) s+=v[i]*(i-1);
			for(int i=1;i<=num;i++) v[i]=0;
			ans=gcd(ans,s);
		}
		fans=0;
		for(int i=1;i<=l;i++)
			if(ret&(1<<i-1)) fans+=pw[i-1];
		fans*=ans;
		printf("%lld\n",fans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
10 ccpcccpc
10 cpcpcp
10 cpc
4 cpccpc
4 dhcp

output:

10001
10101
1
65
3

result:

ok 5 number(s): "10001 10101 1 65 3"

Test #2:

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

input:

30
2 ab
3 abc
4 abcd
5 abcde
6 abcdef
7 abcdefg
8 abcdefgh
9 abcdefghi
10 abcdefghij
11 abcdefghijk
12 abcdefghijkl
13 abcdefghijklm
14 abcdefghijklmn
15 abcdefghijklmno
16 abcdefghijklmnop
16 a
16 ab
16 abc
16 abcd
16 abcde
16 abcdef
16 abcdefg
16 abcdefgh
16 abcdefghi
16 abcdefghij
16 abcdefghijk
...

output:

1
1
3
2
5
3
7
4
9
5
11
6
13
7
-1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

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