Prime Cryptarithm 牛式
Time Limit: 1 Sec Memory Limit: 64 MB
Description
下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。
* * * x * * ------- * * * * * * ------- * * * *
数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。
注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.
写一个程序找出所有的牛式。
Input
Line 1:数字的个数n。 Line 2:N个用空格分开的数字(每个数字都∈ {1,2,3,4,5,6,7,8,9})。
Output
共一行,一个数字。表示牛式的总数。
下面是样例的那个牛式。 2 2 2 x 2 2 --------- 4 4 4 4 4 4 --------- 4 8 8 4
Sample Input
5 2 3 4 6 8
Sample Output
1
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<int,int> m;
int a,b,c,d,e,x,y;
int ans=0;
int n,s[100];
cin>>n;
int i,j,k;
for(i=1;i<=n;i++)
{
cin>>s[i];
m[s[i]]=1;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=n;k++)
{
a=s[i]*100+s[j]*10+s[k];
for(x=1;x<=n;x++)
for(y=1;y<=n;y++)
{
b=s[x]*10+s[y];
c=a*b;
d=s[x]*a;
e=s[y]*a;
if(c<=9999&&c>=1000)
if(m[c%10]&&m[c/10%10]&&m[c/100%10]&&m[c/1000])
if(d>=100&&d<=999&&e>=100&&e<=999)
if(m[d%10]&&m[d/10%10]&&m[d/100])
if(m[e%10]&&m[e/10%10]&&m[e/100])
ans++;
}
}
}
}
cout<<ans<<endl;
return 0;
}