usaco 1.3.4 Prime Cryptarithm

来源:互联网 发布:python 如何读入文件 编辑:程序博客网 时间:2024/06/05 16:09
Prime Cryptarithm

The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.

      * * *   x    * *    -------      * * *         <-- partial product 1    * * *           <-- partial product 2    -------    * * * *
Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.

Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.

Write a program that will find all solutions to the cryptarithm above for any subset of digits from the set {1,2,3,4,5,6,7,8,9}.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1:N, the number of digits that will be usedLine 2:N space separated digits with which to solve the cryptarithm

SAMPLE INPUT (file crypt1.in)

52 3 4 6 8

OUTPUT FORMAT

A single line with the total number of unique solutions. Here is the single solution for the sample input:

      2 2 2    x   2 2     ------      4 4 4    4 4 4  ---------    4 8 8 4

SAMPLE OUTPUT (file crypt1.out)

1


又开始无节操的看题解了。发现原来那个想法太2了。为什么自己就是想不到用数组设置10来判断是否是合法数这么简单的方法呢怒!还有枚举的时候不是枚举每个digit,而是枚举整个大数,这样乘法什么的也方便啊你个呆瓜!

/*ID: wtff0411PROG: crypt1LANG: C++*/#include <iostream>#include <fstream>#include <string>#include <cstring>#include <vector>int n;int c[10];using namespace std;bool isgood(int a,int m){     while(m--)     {         if(!c[a%10]||a==0)         return false;         a/=10;     }     if(a!=0)return false;     return true;}bool check(int i,int j){     if(!isgood(i,3)||!isgood(j,2)||!isgood(i*j,4))     return false;     if(!isgood(i*(j%10),3)||!isgood(i*(j/10),3))     return false;     return true;}     int main(){    freopen("crypt1.in","r",stdin);    freopen("crypt1.out","w",stdout);        cin>>n;    int i;    int j;    int res=0;    int fir[16];    int input;    for(i=0;i<10;i++)    {        c[i]=0;    }    for(i=0;i<n;i++)    {        cin>>input;        c[input]=1;//why i can't think of that!    }        for(i=100;i<1000;i++)        for(j=10;j<100;j++)        {            if(check(i,j))            res++;        }    cout<<res<<endl;    //system("pause");    return 0;}                                    


今天翻墙成功,可以在寝室里上usaco了,也算一大收获吧。不过不知道是配置问题还是怎样,提交的时候总是说有问题,但是回主页刷新一下发现done了。唔,幸好是一遍过吧……其实这题想到方法真不难。。。想不到啊T^T……

。。法律好难……


原创粉丝点击