USACO 1.3.5 Prime Cryptarithm

题目分析:暴力

/*
ID:wconvey
PROG:crypt1
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int cmp(int x,int y)
{
	return x<y;
}
int main()
{
	freopen("crypt1.in","r",stdin);
	freopen("crypt1.out","w",stdout);

	int n;
	bool flag[10];
	scanf("%d",&n);
	int f[4],g[4],h[5];
	int x,y,z,a,b,arr[10];

	memset(flag,0,sizeof(flag));
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	memset(h,0,sizeof(h));

	for(int i=1;i<=n;i++)
	{
		scanf("%d",&arr[i]);
		flag[arr[i]]=1;
	}

	sort(arr+1,arr+1+n,cmp);

	int ans=0,temp;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			for(int k=1;k<=n;k++)
				for(int p=1;p<=n;p++)
					for(int q=1;q<=n;q++)
					{
						x=arr[i],y=arr[j],z=arr[k];
						a=arr[p],b=arr[q];

						f[3]=(b*z)%10;
						if(flag[f[3]]==0)
							continue;
						temp=b*z/10;
						temp+=b*y;
						f[2]=temp%10;
						temp=temp/10+b*x;
						if(temp>arr[n])
							continue;
						f[1]=temp;

						g[3]=(a*z)%10;
						temp=(a*z)/10;
						temp+=a*y;
						g[2]=temp%10;
						temp=temp/10+a*x;
						if(temp>arr[n])
							continue;
						g[1]=temp;

                        h[3]=(f[2]+g[3])%10;
						temp=(f[2]+g[3])/10;
						temp+=f[1]+g[2];
						h[2]=temp%10;
						temp=temp/10+g[1];
						if(temp>arr[n])
							continue;
						h[1]=temp;

						if(flag[f[1]]+flag[f[2]]+flag[f[3]]+flag[g[1]]+flag[g[2]]+flag[g[3]]+
							flag[h[1]]+flag[h[2]]+flag[h[3]]==9)
							ans++;

					}
	printf("%d\n",ans);
	//system("pause");
	return 0;
}


你可能感兴趣的:(USACO 1.3.5 Prime Cryptarithm)