USACO-Section1.3 Prime Cryptarithm [其他]

18 篇文章 0 订阅

2017-6-1

题目大意

下面是一个乘法竖式,如果用我们给定的那n个数字来替代*,可以使式子成立的话,我们就叫这个式子牛式。

          ***
    x      **
   ———————————————
          ***
         ***
   ———————————————
         ****

数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。
注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.
写一个程序求出所有牛式的总数。
(copy from nocow)

题解

          abc
    x      de
   ———————————————
          ***
         ***
   ———————————————
         ****

我们只需要枚举 abc*d,存储在partialProduct里 ,然后在两重循环枚举partialProduct,abc*d, abc*e,看它是否满足上面的竖式。

代码

/*
ID: zachery1
PROG: crypt1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("crypt1.in");
ofstream fout("crypt1.out");

int n, ans = 0;
bool f[10];
vector<int> digits, partialProduct;

int main() {
    cin >> n;
    memset(f, false, sizeof(f));
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        f[t] = true;
        digits.push_back(t);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {

                partialProduct.clear();

                for (int l = 0; l < n; l++) {
                    int m = digits[l];
                    int a = digits[i], b = digits[j], c = digits[k];
                    int pp = (a*100 + b*10 + c) * m;
                    if (pp < 100 || pp > 999) continue;
                    bool flag = true;
                    int t = pp;
                    while (t) {
                        if (!f[t%10]) {
                            flag = false;
                            break;
                        }
                        t /= 10;
                    }
                    if (flag) {
                        partialProduct.push_back(pp);
                    }
                }

                for (int u = 0; u < partialProduct.size(); u++) {
                    for (int v = 0; v < partialProduct.size(); v++) {
                        int p = partialProduct[u] + partialProduct[v]*10;
                         if (p < 1000 || p > 9999) continue;
                         bool flag = true;
                         int t = p;
                         while (t) {
                             if (!f[t%10]) {
                                 flag = false;
                                 break;
                             }
                             t /= 10;
                         }
                         if (flag) {
                             ans++;
                         }
                    }
                }

            }
        }
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值