题目就是kuangbin的数位DP。
先讲C题,不要62,差不多就是一个模板题。要注意的是按位来的话,光一个pos是不够的,还需要一维来记录当前位置是什么数字,这样才能防止同一个pos不同数字的dp值混在一起。直接丢代码:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <math.h> 8 using namespace std; 9 typedef long long ll; 10 11 // pos , 记录当前位是什么 12 // 第二维是为了防止同一个pos的不同which的dp混淆在一起,因为是记忆化搜索的 13 int bit[12],dp[12][12]; 14 15 int dfs(int pos,int which,bool have_six,bool flag) 16 { 17 if(pos == -1) return 1; 18 int& ans = dp[pos][which]; 19 if(flag && ans!=-1) return ans; 20 int d = flag?9:bit[pos]; 21 22 int ret = 0; 23 for(int i=0;i<=d;i++) 24 { 25 if(i==4) continue; 26 if(have_six && i==2) continue; 27 ret += dfs(pos-1,i,i==6,flag||i<d); 28 } 29 if(flag) ans = ret; 30 return ret; 31 } 32 33 int solve(int x) 34 { 35 //if(x==0) return 0; 36 int pos = 0; 37 while(x) 38 { 39 bit[pos++] = x % 10; 40 x /= 10; 41 } 42 43 int ans = 0; 44 ans += dfs(pos-1,0,false,false); 45 return ans; 46 // 因为0也是一个值 47 // 所以solve(5)=5是因为0.1.2.3.5 48 } 49 50 int main() 51 { 52 int x,y; 53 memset(dp,-1,sizeof(dp)); 54 //printf("%d !!\n",solve(5)); 55 while(scanf("%d%d",&x,&y)==2) 56 { 57 if(x==0 && y==0) break; 58 else printf("%d\n",solve(y)-solve(x-1)); 59 } 60 }