`
SwordAndFlame
  • 浏览: 6601 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

数位dp_HDOJ_3555_bomb

阅读更多

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 876    Accepted Submission(s): 319

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
 

 

Recommend
zhouzeyong
题目大意:
在小于等于N的自然数中统计有多少含有49序列;比较简单的数位dp:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
__int64 dp[20][3];
int digit[20];
int len;
void num_input()
{
    __int64 n;
    len =0;
    scanf("%I64d",&n);
    n++;
    while(n){digit[++len]=n%10;n/=10;}
}
void Init()
{
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int k=1;k<20;k++)
    {
        dp[k][0]=dp[k-1][0]*10-dp[k-1][1];//not include 49
        dp[k][1]=dp[k-1][0];//not include 49 with a head 9
        dp[k][2]=dp[k-1][1]+dp[k-1][2]*10;//include 49
        //cout<<dp[k][0]<<' '<<dp[k][1]<<' '<<dp[k][2]<<endl;
    }
}
void solve()
{
    __int64 ans=0;
    bool flag=false;
    int last=digit[len];
    for(int i=len;i>=0;i--)
    {
        //cout<<i<<' '<<flag<<' '<<digit[i]<<' '<<dp[i-1][0]<<endl;
        ans=ans+digit[i]*dp[i-1][2];
        if(flag)
            ans=ans+dp[i-1][0]*digit[i];
        if(!flag&&digit[i]>4)
            ans=ans+dp[i-1][1];
        if(last==4&&digit[i]==9)
            flag=1;
        last=digit[i];
    }
    printf("%I64d\n",ans);
}
int main()
{
    int t;

    scanf("%d",&t);
    while(t--)
    {
        num_input();
        Init();
        solve();
    }
    return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics