K - Bomb HDU - 3555

K - Bomb

HDU - 3555

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?

Input

The 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.

Output

For each test case, output an integer indicating the final points of the power.

Sample Input

3
1
50
500

Sample Output

0
1
15


       
 

Hint

From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499",
so the answer is 15.

题目描述:

问小于等于N的数中,数字里面包含49的有多少个。

分析:

数位dp。套模板,当数包含49的时候跳过,得到的就是不含49的数个数。最后用总数减去这个个数,等到的就是答案。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
int a[60];
ll dp[60][60];
ll dfs(int pos,int pre,bool limit)
{
    if(pos==0) return 1;
    if(!limit && dp[pos][pre]!=0) return dp[pos][pre];
    int up=limit?a[pos]:9;
    ll ans=0;
    //int statu=sta;
    for(int i=0;i<=up;i++)
    {
        if(pre==4&&i==9) continue;
        ans+=dfs(pos-1,i,limit&&i==a[pos]);
    }
    if(!limit) dp[pos][pre]=ans;
    return ans;
}
ll solve(ll x)
{
    int pos=0;
    while(x>0)
    {
        a[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,0,true);
}
int main()
{
    int T;
    cin>>T;
    memset(dp,0,sizeof dp);
    while(T--)
    {   
        ll r;
        scanf("%lld",&r);
        printf("%lld\n",r-solve(r)+1);
    }
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12322365.html
今日推荐