Hdu 3555-Bomb 数位dp
题目: http://acm.hdu.edu.cn/showproblem.php?pid=3555
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 12696 Accepted Submission(s): 4533
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?
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.
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
题解:
数位dp
和“不要62”那道题很类似。套模版即可。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 LL digit[26],f[26][12][2]; 5 LL read() 6 { 7 LL s=0,fh=1;char ch=getchar(); 8 while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();} 9 while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();} 10 return s*fh; 11 } 12 LL Dfs(LL len,LL last,LL flag,LL Judge) 13 { 14 if(len==0)return Judge; 15 if(flag==0&&f[len][last][Judge]!=-1)return f[len][last][Judge]; 16 LL end=flag?digit[len]:9,i,ret=0; 17 for(i=0;i<=end;i++)ret+=Dfs(len-1,i,flag&&(i==end),Judge||(last==4&&i==9)); 18 if(flag==0)f[len][last][Judge]=ret; 19 return ret; 20 } 21 LL Calc(LL N) 22 { 23 LL len=0; 24 memset(digit,0,sizeof(digit)); 25 while(N>0){digit[++len]=N%10;N/=10;} 26 return Dfs(len,0,1,0); 27 } 28 int main() 29 { 30 LL T,N; 31 T=read(); 32 while(T--) 33 { 34 N=read(); 35 memset(f,-1,sizeof(f)); 36 printf("%lld\n",Calc(N)); 37 } 38 fclose(stdin); 39 fclose(stdout); 40 return 0; 41 }