水题,注意一定要用__int64
#include <iostream> #include <cstdio> #include <cstring> #define ll __int64 using namespace std; int t; ll n; ll dp[21][3]; void init(){ memset(dp,0,sizeof dp); dp[0][1]=1; for(int i=1;i<=20;i++){ dp[i][0]=dp[i-1][0]+dp[i-1][1]; dp[i][1]=dp[i-1][0]*8+dp[i-1][1]*9; dp[i][2]=dp[i-1][0]+dp[i-1][2]*10; } } ll solve(ll n){ int c[22],ct=0; memset(c,0,sizeof c); ll nn=n; while(n) c[ct++]=n%10,n/=10; ll ret=0; for(int i=ct-1;i>=0;i--){ if(c[i]>=5) ret+=(ll)(c[i]-1)*(dp[i][0]+dp[i][1])+dp[i][1]; else ret+=(ll)c[i]*(dp[i][0]+dp[i][1]); if(i+1<ct&&c[i]==9&&c[i+1]==4) break; } return nn-ret; } int main(){ scanf("%d",&t); init(); while(t--){ scanf("%I64d",&n); cout<<solve(n+1)-solve(1)<<endl; } return 0; }