题意
24 8
+20 8
=4416
248 + 208的解被错误的算成了4416,
给你一个整数n, 求出在1 - n 中有多少种
(a, b)对, 让 a + b 被错误的计算成n的排列
输入
112
输出
50
样例理解
f[0] = 1;
f[i] = f[i - 1] * (a[i] + 1);
f[i] = f[i - 1] * (a[i] + 1) + f[i - 2] * (9 - a[i]);
12
f[i] = f[i - 1] * (a[i] + 1);
0 / 1 | 0 / 1 / 2
1 / 0 | 2 / 1 / 0
f[i] += f[i - 2] * (9 - a[i]);
凑出12的方案数是 3 -> 9;
f[2] = 6 + 7 = 13
112
f[1] = 2;
f[2] = 4 + 8 = 12;
f[3] = 12 * 3 + 2 * 7 = 36 + 14 = 50;
上代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 30;
int len, a[N];
LL f[N];
string s;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> s;
len = s.size();
for (int i = 1; i <= len; i ++ )
a[i] = s[i - 1] - '0';
f[0] = 1;
for (int i = 1; i <= len; i ++ ) {
f[i] = f[i - 1] * (a[i] + 1);
if (i >= 2 && a[i - 1] == 1) {
// 9 - a[i] 是10 + a[i]的所有构造
f[i] += f[i - 2] * (9 - a[i]);
}
}
cout << f[len] << endl;
return 0;
}