USACO Section 1.3 : Prime Cryptarithm
# include <stdio.h>
# include <math.h>
# include <string.h>
int i,j,k,N,t,s1,s2,num;
int f[11]={0};
check(int a)
{
while (a>0)
{
if (!f[a%10])
return 0;
a/=10;
}
return 1;
}
main ()
{
FILE *in=fopen("crypt1.in","r");
FILE *out=fopen("crypt1.out","w");
fscanf (in,"%d",&N);
for (i=1;i<=N;i++)
{
fscanf (in,"%d",&t);
f[t]=1;
}
num=0;
for (i=100;i<1000;i++)
{
if (check(i))
{
for (j=10; j<99; j++)
{
if (check(j))
{
s1=(j%10)*i;
s2=(j/10)*i;
if ((s1>999)||(!check(s1))) continue;
if ((s2>999)||(!check(s2))) continue;
if (!check(s2*10+s1)) continue;
num++;
}
}
}
}
fprintf (out,"%d\n",num);
fclose(in);
fclose(out);
exit (0);
}
# include <math.h>
# include <string.h>
int i,j,k,N,t,s1,s2,num;
int f[11]={0};
check(int a)
{
while (a>0)
{
if (!f[a%10])
return 0;
a/=10;
}
return 1;
}
main ()
{
FILE *in=fopen("crypt1.in","r");
FILE *out=fopen("crypt1.out","w");
fscanf (in,"%d",&N);
for (i=1;i<=N;i++)
{
fscanf (in,"%d",&t);
f[t]=1;
}
num=0;
for (i=100;i<1000;i++)
{
if (check(i))
{
for (j=10; j<99; j++)
{
if (check(j))
{
s1=(j%10)*i;
s2=(j/10)*i;
if ((s1>999)||(!check(s1))) continue;
if ((s2>999)||(!check(s2))) continue;
if (!check(s2*10+s1)) continue;
num++;
}
}
}
}
fprintf (out,"%d\n",num);
fclose(in);
fclose(out);
exit (0);
}