2021-01-02 USACO Prime Cryptarithm

本文介绍了加密算术的概念,通常是一种填字游戏,要求玩家用特定的数字替换星号以形成有效的数学表达式。文章提供了一个使用集合 {2,3,5,7} 的经典乘法问题示例,并要求编写程序找到所有符合要求的解决方案。程序名为 crypt1,输入和输出格式均有说明。" 137984981,14586329,快速搭建微信小程序开发环境指南,"['微信小程序', '小程序开发', '开发工具', '环境配置', '编程入门']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(This poorly named task has nothing to do with prime numbers or even, really, prime digits. Sorry 'bout that.)

A cryptarithm is usually presented as a pencil-and-paper task in which the solver is required to substitute a digit for each of the asterisks (or, often, letters) in the manual evaluation of an arithmetic term or expression so that the consistent application of the digits results in a proper expression. A classic example is this cryptarithm, shown with its unique solution:

    SEND            9567       S->9  E->5  N->6  D->7
  + MORE          + 1085       M->1  O->0  R->8
  -------        -------
   MONEY           10652       Y->2

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 *. Since the asterisks are generic, any digit from the input set can be used for any of the asterisks; any digit may be duplicated as many times as desired.

Consider using the set {2,3,5,7} for the cryptarithm below:

      * * *
   x    * *
    -------
      * * *         <-- partial product 1 -- MUST BE 3 DIGITS LONG
    * * *           <-- partial product 2 -- MUST BE 3 DIGITS LONG
    -------
    * * * *

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 first cryptarithm above (with three digit partial-products) for any subset of supplied non-zero single-digits. Note that the multiplicands, partial products, and answers must all conform to the cryptarithm's framework.

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 one and only 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

OUTPUT DETAILS

Here's why 222x22 works: 3 digits times 2 digits yields two (equal!) partial products, each of three digits (as required). The answer has four digits, as required. Each digit used {2, 4, 8} is in the supplied set {2, 3, 4, 6, 8}.

Why 222x23 doesn't work:

      2 2 2   <-- OK:  three digits, all members of {2, 3, 4, 6, 8}
        2 3   <-- OK:  two digits, all members of {2, 3, 4, 6, 8}
     ------
      6 6 6   <-- OK:  three digits, all members of {2, 3, 4, 6, 8}
    4 4 4     <-- OK:  three digits, all members of {2, 3, 4, 6, 8}
  ---------
    5 1 0 6   <-- NOT OK: four digits (good), but 5, 1, and 0 are not in
                                                    {2, 3, 4, 6, 8}
/*
ID: traysen1
TASK: crypt1
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
int N, lst[15];
bool usable[15];

bool check_product(int prod) {
	if (prod >= 1000) return false;
	
	while (prod) {
		int digit = prod % 10; 
		prod /= 10;
		
		if (!usable[digit]) return false;
	}
	
	return true;
}

bool check_answer(int ans) {
	if (ans >= 10000) return false;
	
	while (ans) {
		int digit = ans % 10; 
		ans /= 10;
		
		if (!usable[digit]) return false;
	}
	
	return true;
}

bool check_conformation(int a, int b, int c, int d, int e) {
	int abc = a * 100 + b * 10 + c, de = d * 10 + e;
	int product_1 = e * abc, product_2 = d * abc;
	int ans = product_1 + 10 * product_2;
	
	return check_product(product_1) && check_product(product_2) && check_answer(ans);
}

int main() {
	freopen("crypt1.in", "r", stdin);
	freopen("crypt1.out", "w", stdout);
	
	cin >> N;
	for (int i = 0; i < N; i++) {
		int number;
		cin >> number;
		lst[i] = number;
		usable[number] = true;
	}
	
	int total_answer = 0;
	for (int a = 0; a < N; a++)
		for (int b = 0; b < N; b++)
			for (int c = 0; c < N; c++)
				for (int d = 0; d < N; d++)
					for (int e = 0; e < N; e++) 
						if (check_conformation(lst[a], lst[b], lst[c], lst[d], lst[e])) total_answer++;
	
	cout << total_answer << endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值