usaco 1.3.4(crypt1)

这是1.3的最后一题了。

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 used
Line 2: N space separated digits with which to solve the cryptarithm

SAMPLE INPUT (file crypt1.in)

5
2 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


应该是英文水平问题,看英文看了好几遍没看懂。后来看了下翻译,然后就懂了。题目给定了个竖式,再给个集合,在集合中取数填入竖式使之成立(每个数可以重复取多次),求有几种取法。因为集合最多之后9个数,所以直接枚举了。
代码:
/*
ID:614433244
PROG: crypt1
LANG: C++
*/

#include"iostream"
#include"cstdio"
#include"algorithm"

using namespace std;

int a[10];
int b[3],c[2];
int n,ans=0;
bool isin( int p )
{
    int i;
    bool flag=false;
    for( i=0;i<n;i++ )
        if( a[i]==p )
        {
            flag=1;break;
        }
    return flag;
}

int main()
{
    freopen("crypt1.in","r",stdin);
    freopen("crypt1.out","w",stdout);
    scanf("%d",&n);
    int i,j,k,p,q;
    for( i=0;i<n;i++ )
        scanf("%d",&a[i]);
    sort(a,a+n);
    int g,s,ans;
    int m=0;
    for( i=0;i<n;i++ )
    {
        for( j=0;j<n;j++ )
        {
            for( k=0;k<n;k++ )
            {
                for( p=0;p<n;p++ )
                {
                    for( q=0;q<n;q++ )
                    {
                        g=(a[i]*100+a[j]*10+a[k])*a[p];
                        s=(a[i]*100+a[j]*10+a[k])*a[q];
                        ans=(a[i]*100+a[j]*10+a[k])*( a[p]*10+a[q] );
                        if( g>999||g<100 )
                            break;
                        if( s>999||s<100 )
                            break;
                        int temp;
                        int flag=1;
                        while( g )
                        {
                            temp=g%10;
                            if( isin( temp ) )
                            {
                                g=g/10;
                            }
                            else
                            {
                                flag=0;break;
                            }
                        }
                        if( flag==0 )
                            continue;

                        while( s )
                        {
                            temp=s%10;
                            if( isin( temp ) )
                            {
                                s=s/10;
                            }
                            else
                            {
                                flag=0;break;
                            }
                        }
                        if( flag==0 )
                            continue;
                        while( ans )
                        {
                            temp=ans%10;
                            if( isin( temp ) )
                            {
                                ans=ans/10;
                            }
                            else
                            {
                                flag=0;break;
                            }
                        }
                        if( flag==0 )
                            continue;
                        if( flag )
                            m++;
                    }
                }
            }
        }
    }
    printf("%d\n",m);
    return 0;
}
posted @ 2012-07-07 20:06  萧若离  阅读(225)  评论(0)    收藏  举报