1.3.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.

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

********** Note About Cryptarithm‘s Multiplication ************

In USA, children are taught to perform multidigit multiplication as described here. Consider multiplying a three digit number whose digits are ‘a‘, ‘b‘, and ‘c‘ by a two digit number whose digits 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 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 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. 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

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 bool p[10];
 6 bool check(int c)
 7 {
 8     while(c){
 9         if(p[c%10]==false) return false;
10         c/=10;
11     }
12     return true;
13 }
14 int main()
15 {
16     freopen("crypt1.in","r",stdin);
17     freopen("crypt1.out","w",stdout);
18     int a[10];
19     int n;
20     cin>>n;
21     for(int i=0;i<n;i++){
22         cin>>a[i];
23         p[a[i]]=true;
24     }
25     long long cnt=0;
26     for(int i=0;i<n;i++){
27         for(int j=0;j<n;j++){
28             for(int m=0;m<n;m++){
29                 for(int k=0;k<n;k++){
30                     for(int x=0;x<n;x++){
31                         int b=a[i]*100+a[j]*10+a[m];
32                         int c=a[k]*10+a[x];
33                         int d=b*c;
34                         if(b*a[k]>1000||!check(b*a[k])) continue;
35                         if(b*a[x]>1000||!check(b*a[x])) continue;
36                         if(d>10000||!check(d)) continue;
37                         //cout<<b<<" "<<c<<endl;
38                         cnt++;
39                     }
40                 }
41             }
42         }
43     }
44     cout<<cnt<<endl;
45     return 0;
46 }
时间: 2024-10-12 21:45:26

1.3.3 Prime Cryptarithm的相关文章

洛谷P1211 [USACO1.3]牛式 Prime Cryptarithm

P1211 [USACO1.3]牛式 Prime Cryptarithm 187通过 234提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 题面错误 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x     ** ---------- *** *** ---------- **** (请复制到记事本) 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学

l洛谷——P1211 [USACO1.3]牛式 Prime Cryptarithm

P1211 [USACO1.3]牛式 Prime Cryptarithm 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ---------- *** *** ---------- **** (请复制到记事本) 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找

Prime Cryptarithm

链接 分析:对于三位数我们限定为[100,999],两位数我们限定为[10,99],然后我们依次判断是否满足乘法式且各个数位是否在数列中,若都满足+1 1 /* 2 PROB:crypt1 3 ID:wanghan 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 #incl

USACO Section1.3 Prime Cryptarithm 解题报告

crypt1解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 用给出的N个数字,替换以下竖式,能生成多少个正确的竖式? * * * x * * ------- * * * * * * ---

USACO 1.3 Prime Cryptarithm (枚举)

描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ---------- *** *** ---------- **** 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [编辑]格式 PROGRAM NAME: crypt1 INPUT FO

usaco Prime Cryptarithm

/* ID: modengd1 PROG: crypt1 LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> using namespace std; int input[10],N; bool tag[10000]; int counter() { int ans=0; for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { fo

【USACO题库】1.3.4 Prime Cryptarithm牛式

好久没有发题解了,今天发一个很久很久之前写过得题吧 题目其实莫名的难 但是理解后,原来就是一只纸老虎 题目加工中~~~~(缩短题目) 加工完成:已知数字1-9组成集合的一个子集,求满足题意乘法步骤的情况有多少,注意乘数.被乘数.结果都不能超出位数,且每个数字都在题目给出的子集中. 其实就是上面这样. 其实一波暴力即可 循环1-9位数,进行计算,可以就可以,不可以就跳过,但是要注意是个,十,百....的起点,如百的起点是100: 大水题 上代码吧 #include<iostream> #incl

Section 1.3

Mixing Milk http://www.wzoi.org/usaco/11%5C302.asp 水贪心,肯定优先选单价小的 #include <bits/stdc++.h> using namespace std; const int N = 5005; typedef long long ll; struct point{ int price, limit; void scan(){ scanf("%d%d", &price, &limit); }

USACO Chapter 1 解题总结

1.1.1 Your Ride Is Here 基本字符串操作,无压力. 1.1.2 Greedy Gift Givers 基础模拟题,弄明白题意,不怕麻烦,就OK了. 1.1.3 Friday the Thirteenth 自己的做法:三维数组代表年月日,400的数据范围不大,模拟走一下时间的流逝过程即可.时间复杂度O(N*12*31),多好玩. 官方标程好像用到了一个神奇的公式,好像是什么蔡勒公式. 1.1.4 Broken Necklace 2*N的拆环,枚举断点找即可.在找的过程中先要解