Prime Cryptarithm 牛式

版权声明:转载时 别忘了注明出处 https://blog.csdn.net/ZCY19990813/article/details/82713697

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 <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <map>
using namespace std;
typedef long long ll;
map <ll,ll> mp;
int main()
{
    ll i,j,k,p,q,a,b,c,d,e,n,A,B,C,ans=0;
    ll aa[1010];
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>aa[i];
        mp[aa[i]]=1;
    }
    for(i=0;i<n;i++)
    {
        a=aa[i];
        for(j=0;j<n;j++)
        {
            b=aa[j];
            for(k=0;k<n;k++)
            {
                c=aa[k];
                A=a*100+b*10+c;
                for(p=0;p<n;p++)
                {
                    d=aa[p];
                    for(q=0;q<n;q++)
                    {
                        e=aa[q];
                        B=d*10+e;
                        C=A*B;
                        if(A*e>=100&&A*e<=999)
                        {
                            if(A*d>=100&&A*d<=999)
                            {
                                 if(A*B>=1000&&A*B<=9999)
                                 {
                                     if(mp[A*e%10]&&mp[A*e/10%10]&&mp[A*e/100])
                                     {
                                         if(mp[A*d%10]&&mp[A*d/10%10]&&mp[A*d/100])
                                         {
                                             if(mp[A*B%10]&&mp[A*B/10%10]&&mp[A*B/100%10]&&mp[A*B/1000])
                                                ans++;
                                         }
                                     }
                                 }
                            }

                        }
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ZCY19990813/article/details/82713697