USACO1.3.4 Prime Cryptarithm 牛式 解题报告(模拟)

版权声明:本文为博主原创文章,转载注明出自CSDN bestsort。 https://blog.csdn.net/bestsort/article/details/82708304

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

让找出一个3位数abc和一个2位数de,使得abc*d和abc*e均为4位数,且abc*de为5位数(且均由给出的数组成)

直接从100-1000和从10-100循环找出满足题意的数即可

#include <map>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
#define lowbit(a) (a&(-a))
#define _mid(a,b) ((a+b)/2)
#define _mem(a,b) memset(a,0,(b+3)<<2)
#define fori(a) for(int i=0;i<a;i++)
#define forj(a) for(int j=0;j<a;j++)
#define ifor(a) for(int i=1;i<=a;i++)
#define jfor(a) for(int j=1;j<=a;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)
#define IO do{\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);}while(0)
#define mp(a,b) make_pair(a,b);
using namespace std;
typedef long long ll;
const int maxn =  210;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f;
const double EPS = 1e-7;
const double Pi = acos(-1);
const int MOD = 1e9+7;
bool v[100];
bool allused(int a,int b) {
 
    int buf[50]= {a,b,a*b,(b%10)*a,(b/10)*a};
    fori(5){
        if(i!=2&&buf[i] > 1000)
            return false;
        while(buf[i]) {
            if(!v[buf[i]%10])
                return false;
            buf[i] /= 10;
        }
    }
    return true;
}
int main() {
    int n;
    cin >> n;
        int buf;
        fori(n)
        cin >> buf,v[buf] = true;
        int res = 0;
        for(int i=111; i<1000; i++)
            for(int j=11; j<100; j++){
                if(i*j<10000&&allused(i,j))
                    res++;
            }
        cout << res << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/bestsort/article/details/82708304