#include <iostream> #include<cstdio> typedef long long ll; using namespace std; ll dp[15][2]; ll digit[15]; ll dfs(ll len,bool last_4,bool limit){ if(len==0) return 1; if(!limit && dp[len][last_4]) return dp[len][last_4]; ll sum=0; for(ll i=0;i<=(limit?digit[len]:9);i++){ if(!(last_4&&i==9)) sum+=dfs(len-1,i==4,limit&&i==digit[len]); } if(!limit) dp[len][last_4]=sum; return sum; } ll solve(ll x){ digit[0]=0; while(x){ digit[++digit[0]]=x%10; x/=10; } return dfs(digit[0],false,true); } int main() { ll t;scanf("%lld",&t); while(t--){ ll n;scanf("%lld",&n); printf("%lld\n",n-(solve(n)-1)); } return 0; }
原文:https://www.cnblogs.com/lllxq/p/9428677.html