CUC-SUMMER-11-F

作者: Nioge | 来源:发表于2017-08-18 23:29 被阅读17次
F - 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.


题意:从1到n有多少个不含49的数

解法:数位dp,dp[i][2]表示i位不含49的数的个数,dp[i][0]表示长度为i但是不以9开头的数,dp[i][1]表示长度为i以9开头的数

代码:

#include<iostream>
#include<cstring>
using namespace std;
long long dp[25][3];
long long count(long long x){
    int digit[25],k=0,flag=0;
    while(x){
        digit[++k]=x%10;
        x/=10;
    }
    digit[k+1]=0;
    long long ans=0;
    for(int i=k;i>0;i--){
        ans+=digit[i]*dp[i-1][2];
        if(flag)
            ans+=dp[i-1][0]*digit[i];
        else if(digit[i]>4)
            ans+=dp[i-1][1];
        if(digit[i+1]==4&&digit[i]==9)
            flag=1;
    }
    return ans;
}
int main()
{
    dp[0][0]=1;
    for(int i=1;i<=20;i++){
        dp[i][0]=dp[i-1][0]*10-dp[i-1][1];
        dp[i][1]=dp[i-1][0];
        dp[i][2]=dp[i-1][2]*10+dp[i-1][1];
    }
    int n;
    cin>>n;
    while(n--){
        long long num;
        cin>>num;
        cout<<count(num+1)<<endl;
    }
}

相关文章

  • CUC-SUMMER-11-F

    F - Bomb HDU - 3555 The counter-terrorists found a time b...

网友评论

    本文标题:CUC-SUMMER-11-F

    本文链接:https://www.haomeiwen.com/subject/geuzrxtx.html