HDU3555 Bomb 数位DP第一题

时间:2022-09-04 03:46:57
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point. 
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them? 

InputThe first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker. 
OutputFor each test case, output an integer indicating the final points of the power.Sample Input

3
1
50
500

Sample Output

0
1
15
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define LL long long
const int N=;
LL dp[N][][][],n,ans;
int a[N],cnt;
void _divide(LL v){
cnt=;
while(v){
a[++cnt]=v%;
v/=;
}return ;
}
LL _dfs(int pos,bool limit,bool pre,bool stat)
{
if(pos==) return stat;
LL tmp=;
if(!limit&&dp[pos][limit][pre][stat]) return dp[pos][limit][pre][stat];
int Up=limit?a[pos]:;
for(int i=;i<=Up;i++)
tmp+=_dfs(pos-,limit&&i==Up,i==,stat||(pre&&i==));
dp[pos][limit][pre][stat]=tmp;
if(tmp>ans) ans=tmp;
return tmp;
}
int main()
{
int i,T;
scanf("%d",&T);
while(T--){
memset(dp,,sizeof(dp));
scanf("%lld",&n);
_divide(n);
printf("lld\n",_dfs(cnt,true,false,false));
}
return ;
}