USACO 1.4 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.

以下的密码是乘法问题,可以通过将指定的N个数字组中的数字替换为标有*的位置来解决。如果选择了素数组{2,3,5,7},则将密码称为PRIME CRYPTARITHM。

      * * *
   X * *
    -------
      * * * < - 部分产品1
    * * * < - 部分产品2
    -------
    * * * *
数字只能出现在标有“*”的地方。当然,不允许使用前导零。

部分产品必须是三位数长,即使一般情况(见下文)可能有四位数的部分产品。 

**********关于Cryptarithm乘法的注释************ 
在美国,教孩子们按照这里的描述进行多位乘法。考虑将数字为'a','b'和'c'的三位数乘以一个两位数字,其数字为'd'和'e':

[请注意,此图表显示的结果中的数字远远超过
上面所需的图表有三位数的部分产品!]

          abc < -  number'abc'
        xde < -  number'de'; 'x'表示'倍增'
     -----------
p1 * * * * < -  e * abc的乘积; 第一颗星可能是0(缺席)
p2 * * * * < -  d * abc的乘积; 第一颗星可能是0(缺席)
     -----------
      * * * * * < -  p1和p2之和(e * abc + 10 * d * abc)== de * abc

请注意,“部分产品”与美国学校一样。第一个部分产品是第二个数字和最高数字的最后一位数的乘积。第二部分产品是第二个数字的第一个数字和最高数字的乘积。

编写一个程序,可以找到上述密码的所有解决方案,用于所提供的非零个位数的任何子集。


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
题解:
这个问题的限制足够小,我们可以尝试所有可能的3位* 2位数字的产品,并查看是否使用了所有正确的数字。

代码如下:
/*
ID: zhaoxia13
LANG: C
TASK: crypt1
*/

#include <stdio.h>
#include <stdlib.h>

int n, a[20], p[10];
int f[10], sum, x1, y1, x2, y2, ans;

void dfs(int k)
{
	int i;
	if(k == 5)
    {
        x1 = f[1]*100 + f[2]*10 + f[3];
        x2 = f[5]*x1;
		y2 = f[4]*x1;
        if(x2 >= 1000 || y2 >= 1000)
			return;
        while(x2)
		{
			if(p[x2%10] == 0)
				return;
			x2=x2/10;
		}
        while(y2)
		{
			if(p[y2%10] == 0)
				return;
			y2 = y2/10;
		}
        x2 = f[5]*x1;
		y2 = f[4]*x1;
        sum = x2 + y2*10;
        if(sum >= 10000)
			return;
        while(sum)
		{
			if(p[sum%10] == 0)
				return;
			sum = sum/10;
		}
        ans++;
        return;
    }
    for(i = 1; i <= n; i++)
	{f[k + 1] =a [i];dfs(k+1);}
}

int main()
{
	int i;
	freopen("crypt1.in", "r", stdin);
	freopen("crypt1.out", "w", stdout);
	scanf("%d", &n);
    for(i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		p[a[i]]	=	1;
	}
    dfs(0);
    printf("%d\n", ans);

	fclose(stdin);
	fclose(stdout);
	return 0;
}
/*
ID: zhaoxia13
LANG: C
TASK: crypt1
*/

#include <stdio.h>
#include <stdlib.h>

int n, a[20], p[10];
int f[10], sum, x1, y1, x2, y2, ans;

void dfs(int k)
{
	int i;
	if(k == 5)
    {
        x1 = f[1]*100 + f[2]*10 + f[3];
        x2 = f[5]*x1;
		y2 = f[4]*x1;
        if(x2 >= 1000 || y2 >= 1000)
			return;
        while(x2)
		{
			if(p[x2%10] == 0)
				return;
			x2=x2/10;
		}
        while(y2)
		{
			if(p[y2%10] == 0)
				return;
			y2 = y2/10;
		}
        x2 = f[5]*x1;
		y2 = f[4]*x1;
        sum = x2 + y2*10;
        if(sum >= 10000)
			return;
        while(sum)
		{
			if(p[sum%10] == 0)
				return;
			sum = sum/10;
		}
        ans++;
        return;
    }
    for(i = 1; i <= n; i++)
	{f[k + 1] =a [i];dfs(k+1);}
}

int main()
{
	int i;
	freopen("crypt1.in", "r", stdin);
	freopen("crypt1.out", "w", stdout);
	scanf("%d", &n);
    for(i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		p[a[i]]	=	1;
	}
    dfs(0);
    printf("%d\n", ans);

	fclose(stdin);
	fclose(stdout);
	return 0;
}



 




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值