AcWing 4868. 数字替换

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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int INF = 1000;
int n, ans = INF;
LL x;
void dfs(LL x, int d) {
bool st[10] = {0};
int cnt = 0;
for (LL y = x; y; y /= 10) {
++cnt;
st[y % 10] = 1;
}
if (d + n - cnt >= ans) return;
if (cnt == n) {
ans = d;
return;
}
for (int i = 9; i >= 2; --i)
if (st[i])
dfs(x * i, d + 1);
}
int main() {
scanf("%d%lld", &n, &x);
dfs(x, 0);
if (ans == INF) ans = -1;
printf("%d\n", ans);
return 0;
}