首页 > 代码库 > hdu3555 Bomb(数位dp)

hdu3555 Bomb(数位dp)

                                                   Bomb

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

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
2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU
数位dp入门 睡觉  明天解释
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cctype> 5 #include<cmath> 6 #include<cstring> 7 #include<map> 8 #include<stack> 9 #include<set>10 #include<vector>11 #include<algorithm>12 #include<string.h>13 typedef long long ll;14 typedef unsigned long long LL;15 using namespace std;16 const int INF=0x3f3f3f3f;17 const double eps=0.0000000001;18 ll N,dp[33][3],sum;19 int bit[33];20 int main()21 {22     int i,j,T,len;23     memset(dp,0,sizeof(dp));24     dp[0][0]=1;25     for(i=1;i<=20;i++){26         dp[i][0]=dp[i-1][0]*10-dp[i-1][1];27         dp[i][1]=dp[i-1][0];28         dp[i][2]=dp[i-1][1]+dp[i-1][2]*10;29     }30     scanf("%d",&T);31     while(T--){32         scanf("%I64d",&N);33         N++;34         len=sum=0;35         while(N){36             bit[++len]=N%10;37             N/=10;38         }39         bit[len+1]=0;40         bool flag=0;41         for(i=len;i>=1;i--){42             sum+=dp[i-1][2]*bit[i];43             if(!flag&&bit[i]>4)  sum+=dp[i-1][1];44             if(flag)    sum+=dp[i-1][0]*bit[i];45             if(bit[i+1]==4&&bit[i]==9)  flag=1;46         }47         cout<<sum<<endl;48     }49     return 0;50 }

 

 

hdu3555 Bomb(数位dp)