usaco 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


题意: 三位数乘两位数,格式题目已给出,问你,给你一些数字,从中挑选数字,满足等式的条件有多少!!!



/**
   PROG:crypt1
   LANG:C++
   ID:zpc19951 
**/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int a[11],b[11],c[11],d[11],e[11],vis[11],num[11],flag;  //flag标记数的长度,这个是wrong点!!! 

bool judge(int num)
{
	int t=num,count=0;
	while(t) {
		if(!vis[t%10]) return true;
		t=t/10;
		count++;
	}
	if(count==flag)return false;
	else return true;
}


int main()
{
	freopen("crypt1.in","r",stdin);
	freopen("crypt1.out","w",stdout); 
	int i,j,k,z,p,n,ans;
	int num1,num2,num3,num4,num5;
	scanf("%d",&n);
	ans=0;
	for(i=0;i<10;i++) vis[i]=0;
	for(i=1;i<=n;i++) {
		scanf("%d",&num[i]);
		vis[num[i]]=1;
	} 
	
	
	
	//暴力枚举一发,在判断一下就好。 
	for(i=1;i<=n;i++) {
		for(j=1;j<=n;j++) {
			for(k=1;k<=n;k++) {
				num1=num[i]*100+num[j]*10+num[k];
				
				for(z=1;z<=n;z++) {
					for(p=1;p<=n;p++) {
						num2=num1*num[p];
						num3=num1*num[z];
//						printf("%d %d\n",num1,num[p]+num[z]*10);
						flag=3;
						if(judge(num2)||judge(num3)) continue;
						num4=num2+num3*10;
						flag=4;
						if(!judge(num4)) ans++; 
					}
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值