dp[dep][four]表示长度为dep的上一个是否为4的不含子串49的数的个数
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll dp[20][2]; 5 int dig[20]; 6 ll dfs(int dep,int four,int flag){ 7 if(!dep)return 1LL; 8 if(!flag&&dp[dep][four]!=-1)return dp[dep][four]; 9 int lim=flag?dig[dep]:9; 10 ll ans=0; 11 for(int i=0;i<=lim;i++){ 12 if(four&&i==9)continue; 13 ans+=dfs(dep-1,i==4?1:0,flag&(i==lim)); 14 } 15 if(!flag)dp[dep][four]=ans; 16 return ans; 17 } 18 ll solve(ll x){ 19 int dd=0; 20 while(x)dig[++dd]=x%10,x/=10; 21 return dfs(dd,0,1); 22 } 23 int main(){ 24 memset(dp,-1,sizeof(dp)); 25 int T; 26 scanf("%d",&T); 27 while(T--){ 28 ll A; 29 scanf("%lld",&A); 30 printf("%lld ",A-solve(A)+1); 31 } 32 return 0; 33 }