您的位置:首页 > 其它

USACO 1.3 Prime Cryptarithm

2015-09-04 16:05 441 查看
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
ab4d
. 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


题解:给出N个数字,求出这N个数字可以组成多少个牛式
即组成下列数字 被乘数 a(一个三位数,每位数字属于上面给出的N个数字组成的集合); 乘数 b(一个两位数每位数字属于上面给出的N个数字组成的集合);
a与b个位数的成绩 c(一个三位数,每位数字属于上面给出的N个数字组成的集合);a与b十位数的成绩 d(一个三位数,每位数字属于上面给出的N个数字组成的集合);
a与b的成绩 e(一个四位数,每位数字属于上面给出的N个数字组成的集合);

搞懂题意,一个简单的搜多即可得出答案
ps:本人大三狗一枚,正在持续更新博客,文章里有任何问题,希望各位网友可以指出。若有疑问也可在评论区留言,我会尽快回复。希望能与各位网友互相学习,谢谢!

/*
ID: cxq_xia1
PROG: crypt1
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool flag[10];

bool check(int a,int b)
{
int tmp;
if(a==0)
return false;

while(b--)
{
tmp=a%10;
if(!flag[tmp])
return false;
a/=10;
}
if(a!=0)
return false;

return true;
}

bool isPriCry(int n,int m)
{
if(!check(n,3)||!check(m,2)||!check(m*n,4)||!check(n*(m%10),3)||!check(n*(m/10),3))
return false;
else
return true;
}

int main()
{
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
memset(flag,false,sizeof(flag));
int N,tmp,ans=0;
cin >> N;
for(int i=0;i<N;i++)
{
cin >> tmp;
flag[tmp]=true;
}
for(int i=100;i<1000;i++)
{
for(int j=10;j<100;j++)
{
if(isPriCry(i,j))
{
ans++;
}

}
}
cout <<ans <<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  USACO