数位dp 模板

不能有49

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
#define N 1000+5
ll dp[N][2];
ll a[N];
ll dfs(int pos,int pre,bool lead,bool limit  )
{
    if(!pos)return 1; 

    if(!limit&&!lead&&dp[pos][pre]!=-1)return dp[pos][pre];
    ll ans=0;
    int up=limit?a[pos]:9;
    rep(i,0,up)
    {
        if(pre&&i==9)continue;
        ans+=dfs(pos-1,i==4,lead&&i==0,limit&&i==a[pos]);
    }
    if(!limit&&!lead)dp[pos][pre]=ans;
    return ans;
}
ll solve(ll x)
{
    int pos=0;
    while(x)
    {
        a[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,0,true,true);
}
int main()
{
    int cas;
    RI(cas);
    while(cas--)
    {
        CLR(dp,-1);
        ll x;
        cin>>x;
        printf("%lld
",x-solve(x)+1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/bxd123/p/10712994.html