题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3555
先算出1~n 含有49的有几个,再减一减就好了
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
LL d[25][10],digit[20];
void init()
{
d[0][0]=1;
for(LL i=1;i<=20;i++)
for(LL j=0;j<=9;j++)
for(LL k=0;k<=9;k++)
if(!(j==4&&k==9)) d[i][j]+=d[i-1][k];
}
LL solve(LL x) //[0,x)
{
LL len=0;
while(x){
digit[++len]=x%10;
x/=10;
}
digit[len+1]=0;
LL ans=0;
for(LL i=len;i>=1;i--){
for(LL j=0;j<digit[i];j++)
if(!(j==9&&digit[i+1]==4))
ans+=d[i][j];
if(digit[i]==9&&digit[i+1]==4)
break;
}
return ans-1; //因为是从0开始的,所以要减去1
}
int main(int argc, char const *argv[])
{
LL n,T; cin>>T;
init();
while(T--){
cin>>n;
cout<<n-solve(n+1)<<endl;
}
return 0;
}