题意:问一个区间内的数字中还有49的数字的个数
思路:简单数位DP模版题,模版有大神已解释的很清楚了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=5010;
int dig[20];
ll dp[20][10][2];
ll dfs(int pos,int pre,int flag,int lim){
if(pos<0) return flag;
if(!lim&&dp[pos][pre][flag]!=-1) return dp[pos][pre][flag];
int las=lim?dig[pos]:9;
ll ret=0;
for(int i=0;i<=las;i++) ret+=dfs(pos-1,i,flag||(pre==4&&i==9),lim&&(i==las));
if(!lim) dp[pos][pre][flag]=ret;
return ret;
}
ll slove(ll n){
int len=0;
while(n){
dig[len++]=n%10;
n/=10;
}
return dfs(len-1,0,0,1);
}
int main(){
memset(dp,-1,sizeof(dp));
int T;
ll n;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
printf("%I64d\n",slove(n));
}
return 0;
}