Algorithm, Data structure/Solved Algorithmic Problem

USACO 1.3 - Prime Cryptarithm

JaykayChoi 2016. 6. 11. 10:30


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


출처: http://train.usaco.org/



주어진 정수들을 주어진 각 칸에 넣어 곱셈을 한다고 할 때 곱셈을 만족시키는 모든 해의 개수를 구하는 문제입니다.

100~999 와 10~99 의 모든 숫자를 대상으로 검사를 하는 방법을 사용했습니다. 빠르게 검사를 하기 위해 find 를 지원하는 std container set 을 사용했습니다.




my solving

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <fstream>
#include <iostream>
#include <string>
#include <set>
using namespace std;
 
set<int> numbers;
 
bool isAllowed(int num, int allowedDigit)
{
    string strNum = to_string(num);
    
    if (strNum.size() != allowedDigit)
        return false;
 
    for (int i = 0; i < strNum.size(); i++)
    {
        set<int>::iterator it = numbers.find(strNum[i] - '0');
        if (it == numbers.end())
            return false;
    }
    return true;
}
 
int main() 
{
    ofstream fout("crypt1.out");
    ifstream fin("crypt1.in");
 
    int n;
    
    fin >> n;
 
    for (int i = 0; i < n; i++)
    {
        int num;
        fin >> num;
        numbers.insert(num);
    }
 
    int ret = 0;
    for (int i = 100; i < 1000; i++)
    {
        for (int j = 10; j < 100; j++)
        {
            if (isAllowed(i, 3&& isAllowed(j, 2&& isAllowed(i * (j % 10), 3&& isAllowed(i * (j / 10), 3&& isAllowed(i * j, 4))
                ret++;
        }
    }
    
    fout << ret << endl;
 
fin.close();
fout.close();
    return 0;
}
cs