属于数位DP中的模版题
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; #define MOD 2520 int dp[20][2];//dp[数字的长度][数字的第一位数字]; int bit[20], p = 0; int dfs(int pos,int first,int flag) { if(pos == -1) { return 1; } if(!flag && dp[pos][first] != -1) return dp[pos][first]; int ans = 0, end; end = flag?bit[pos]:9; for(int i=0; i<=end; i++) { if(i == 4)continue; if(i == 2 && first)continue; ans += dfs(pos-1, i == 6, flag && end == i); } if( !flag ) dp[pos][first] = ans; return ans; } int solve(int n) { int len = 0; while(n) { bit[len ++] = n%10; n /= 10; } return dfs(len-1, 0, 1); } int main() { int a, b; memset(dp, -1, sizeof(dp)); while(scanf("%d%d",&a,&b), a+b) { printf("%d\n", solve(b) - solve(a-1) ); } return 0; }
时间: 2024-11-05 18:26:59