bomb hdu 3555

Problem Description
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.
 

Author
fatboy_cw@WHU
 

Source
 把不含62的减掉  数位dp

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:   3554  3556  3557  3558  3559 
 
#include <cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long int dp[30][10];
long long int a[30];
void init()
{
    long long int i , j, k ;
    memset( dp, 0 , sizeof(  dp ));
    dp[0][0] = 1;
    for( i = 1; i<=25; i++ )
    {
        for( j  = 0; j<10; j++)
        {
            for( k  =0; k<10; k++)
            {
                if( j != 4 || k != 9)
                {
                    dp[i][j] = dp[i][j] + dp[i-1][k];
                }
            }
        }
    }
}
long long int solve( long long int m )
{
    long long int i , j, k;
    long long int ans  = 0;
    int cnt = 1;
    while( m )
    {
        a[cnt++] = m%10;
        m = m/10;
    }
    a[cnt] = 0;
    for( i  = cnt-1; i>=1; i--)
    {
        for( j = 0; j<a[i]; j++)
        {
            if( a[i+1] != 4 || j != 9)
            {
                ans = ans + dp[i][j];
            }
        }
        if( a[i+1] == 4 && a[i] == 9)
            break;
    }
    //printf("%d\n", ans );
    return ans;
}
int main()
{
    long long int  t , n;
    long long int sum;
    init();
    scanf("%lld", &t);
    while( t-- )
    {
        scanf("%lld", &n) ;
        long long int t = solve( n + 1 ) - solve(1);
        sum = n - t;
        printf("%lld\n", sum);
    }

}

猜你喜欢

转载自blog.csdn.net/jianxingzhang/article/details/80085147
今日推荐