#include<bits/stdc++.h>
using namespace std;
const int N = 35;
int n;
int f[N][N];//f[i][j]表示i位最高位是j的数字的个数
void init()
{
for(int i = 0; i <= 9; i ++ ){
if(i != 4) f[1][i] = 1;
}
for(int i = 2; i < N; i ++ ){
for(int j = 0; j <= 9; j ++ ){
for(int k = 0; k <= 9; k ++ ){
if(j == 4 || (j == 6 && k == 2)) continue;
f[i][j] += f[i - 1][k];
}
}
}
}
int dp(int n){
if(!n) return 1;
vector<int>num;
while(n) num.push_back(n % 10), n /= 10;
int res = 0;//记录方案数
int last = 0;//记录上一位
for(int i = num.size() - 1; i >= 0; i -- ){
int x = num[i];
for(int j = 0; j < x; j ++ ){
if(j == 4 || (last == 6 && j == 2)) continue;
res += f[i + 1][j];
}
if(x == 4 || (last == 6 && x == 2)) break;
last = x;
if(!i) res ++ ;
}
return res;
}
int main()
{
int l, r;
while(cin>>l>>r && l != 0 && r != 0){
memset(f, 0, sizeof f);
init();
cout<<dp(r) - dp(l - 1)<<endl;
}
return 0;
}
AcWing 1085 不要62 题解 (动态规划—DP—数位DP)
最新推荐文章于 2024-01-17 23:34:40 发布