USACO Prime Cryptarithm

Code

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
55
56
57
58
59
60
61
62
63
64
/*
ID: wcr19961
PROG: crypt1
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 12;
bool vis[maxn];
int a[maxn], b, ans;
int check(int x) {
int cnt = 0;
while (x) {
if (!vis[x % 10]) {
return -1;
}
x /= 10;
++cnt;
}
return cnt;
}
void dfs(int d, int n) {
if (d == n) {
if (check(b * (a[3] * 10 + a[4])) == 4) {
++ans;
}
return;
}
if (d == 3) {
b = a[0] * 100 + a[1] * 10 + a[2];
}
for (int i = 1; i < 10; ++i) {
if (vis[i]) {
if (d >= 3 && check(b * i) != 3) {
continue;
}
a[d] = i;
dfs(d + 1, n);
}
}
}
int main() {
freopen("crypt1.in", "r", stdin);
freopen("crypt1.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
vis[x] = true;
}
dfs(0, 5);
printf("%d\n", ans);
return 0;
}

支付宝扫码领红包