Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 23853 Accepted Submission(s): 8990
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
ll n;
int bit[20];
ll dp[20][2];
ll dfs(int pos,int pre,int sta,bool limit)
{
if(pos==-1)return 1;
if(!limit&&dp[pos][sta]!=-1)return dp[pos][sta];
int up=limit?bit[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++)
{
if(pre==4&&i==9)
continue;
ans+=dfs(pos-1,i,i==4,limit&&i==bit[pos]);
}
if(!limit)dp[pos][sta]=ans;
return ans;
}
ll solve(ll x)
{
int len=0;
while(x)
{
bit[len++]=x%10;
x/=10;
}
return dfs(len-1,-1,0,true);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%lld",&n);
memset(dp,-1,sizeof(dp));
printf("%lld\n",n+1-solve(n));
}
return 0;
}
原文:https://www.cnblogs.com/zhgyki/p/9750663.html