f[i][j]表示当前有i位 第i位数字是j的合法windy数有几种
统计1~x的答案时先考虑位数比x少的,然后枚举1~{x的某一位-1} 加入答案
注意个位上的数要加1 不然取不到闭区间
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
#define ll long long
int f[12][10], A, B;
int stk[12], top;
ll g(int x){
ll ans = 0, t = x;
top = 0;
while (t) {
stk[++top] = t % 10;
t /= 10;
}
for (int i = 1; i < top; i++)
for (int j = 1; j <= 9; j++)
ans += f[i][j];
for (int i = 1; i < stk[top]; i++) ans += f[top][i];
stk[1]++;
for (int i = top - 1; i >= 1; i--) {
for (int j = 0; j < stk[i]; j++) { //个位可以取到等
if (abs(stk[i + 1] - j) >= 2) {
ans += f[i][j];
}
}
if (abs(stk[i + 1] - stk[i]) < 2) break;
}
return ans;
}
int main(){
cin >> A >> B;
for (int i = 0; i <= 9; i++) f[1][i] = 1;
for (int i = 2; i <= 11; i++) {
for (int j = 0; j <= 9; j++){
for (int k = 0; k <= 9; k++){
if (abs(j - k) >= 2){
f[i][j] += f[i - 1][k];
}
// puts("forf\n");
}
}
//cout << "yyy" << endl;
}
cout << g(B) - g(A - 1) << endl;
}