USACO--Prime Cryptarithm

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

这道题的题意还真是无奈,后来才知道要求两个数的积必须小于10000,而且中间的积(上边的数分别乘以下边那个数的个位和十位)都要是三位数.感觉这不是贪心=_=  !

思路:我开了一个数组,输入后的n个数,在对应的hash_arr中这个下表的值为true,然后从100-999和10-99进行枚举,条件满足,就加一

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 105
using namespace std;

bool hash_arr[10];  //输入的n个数,将其作为数组下标,输入哪个数,对应数组中值为true

int is_exist(int n)  //判断组成这个数的几个数存是否存在
{
	bool ok=true;
	int trans[20],len=0;
	memset(trans,0,sizeof(trans));
	while(n)   //先把这个数分开,存在trans数组中
	{
		trans[len++]=(n%10);
		n/=10;
	}
	for(int i=0;i<len;i++)
	{
		if(!hash_arr[ trans[i] ]) {ok=false;break;}   //如果这个数为下表的数组中的值为0,就说明不存在,就结束
	}
	return ok;
}

int main()
{
    freopen("crypt1.in","r",stdin);
    freopen("crypt1.out","w",stdout);
	int n;
	scanf("%d",&n);
	memset(hash_arr,0,sizeof(hash_arr));
	for(int i=0;i<n;i++)
	{
		int tem;
		scanf("%d",&tem);
		hash_arr[tem]=true;//对应数组中的值为true
	}
	int a,b,c,d,e,sum=0;   //a是上边乘数,b是下边的乘数,c是a乘以b的十位数的积,d是a乘以b的个位上数的积,e是a*b
	for(int i=100;i<=999;i++)
	{
		for(int j=10;j<=99;j++)
		{
			a=i;b=j;c=i*(j/10);d=i*(j%10);e=i*j;
			if( is_exist(a) && is_exist(b) && is_exist(c) && is_exist(d) && is_exist(e) && e<=9999 && c<1000 && d<1000) sum++;
			//如果每个数都存在而且符合条件,sum的值+1
		}
	}
	printf("%d\n",sum);
	return 0;
}

发布了50 篇原创文章 · 获赞 2 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章