USACO 1.3 - Prime Cryptarithm(暴力枚举)

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

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

The partial products must be three digits long, even though thegeneral case (see below) might have four digit partial products.

********** Note About Cryptarithm's Multiplication ************
In USA, children are taught to perform multidigit multiplicationas described here. Consider multiplying a three digit numberwhose digits are 'a', 'b', and 'c' by a two digit number whosedigits are 'd' and 'e':

[Note that this diagram shows far more digits in its results than
the required diagram above which has three digit partial products!]

          a b c     <-- number 'abc'
        x   d e     <-- number 'de'; the 'x' means 'multiply'
     -----------
p1      * * * *     <-- product of e * abc; first star might be 0 (absent)
p2    * * * *       <-- product of d * abc; first star might be 0 (absent)
     -----------
      * * * * *     <-- sum of p1 and p2 (e*abc + 10*d*abc) == de*abc

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

Write a program that will find all solutions to the cryptarithmabove for any subset of supplied non-zero single-digits.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1:N, the number of digits that will be used
Line 2:N space separated non-zero 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 solutions. Hereis 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

                                                         

水题,暴力=。=

枚举所有三位数,两位数符合情况数。

CODE:

/*
ID: sotifis3
LANG: C++
TASK: crypt1
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int vis[15];

bool judge(int num)
{
    while(num > 0){
        if(vis[num%10] != 1) return false;
        num /= 10;
    }
    return true;
}
int main()
{
    //freopen("in", "r", stdin);
    freopen("crypt1.in","r",stdin);
    freopen("crypt1.out","w",stdout);
    int n;
    while(~scanf("%d", &n)){
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; ++i){
            int a;
            scanf("%d", &a);
            vis[a] = 1;
        }
        int ans = 0;
        for(int i = 100; i <= 999; ++i){
            if(judge(i)){
                for(int j = 0; j <= 9; ++j){
                    int jj = j * i;
                    if(judge(j) && judge(jj) && jj < 1000){
                        for(int k = 1; k <= 9; ++k){
                            int kk = k * i;
                            if(judge(k) && judge(kk) && kk < 1000){
                                if(judge(kk * 10 + jj) && (kk * 10 + jj) < 10000 && (kk * 10 + jj) >= 1000)
                                    ans ++;
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值