HDU 3555 Bomb (数位DP)
2013-05-14 19:15
543 查看
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3558 Accepted Submission(s): 1245
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
做的第一道数位DP,参考了别人的思路,又长见识了。
首先定义状态,dp[i][0]表示长度为i且不含49的数有多少个,dp[i][1]表示长度为i,不含49但开头为9的数有多少个,dp[i][2]表示含49的数有多少个。
状态转移为:
1)dp[i][0] = dp[i-1][0]*10 - dp[i-1][1];
2)dp[i][1] = dp[i-1][0];
3)dp[i][2] = dp[i-1][2]*10 + dp[i-1][1];
对于1),表示长度为i时只需从长度为i-1的开头加上0~9这10个数,故乘以10,但是要除去i-1中开头为9的情况,因为在i-1且开头为9时,开头加上4就成了含49的情况了(对于任意k,dp[k][0]是包含dp[k][1]的)。
对于2),意思是长度为i且开头为9,只需在长度为i-1且不含49的情况下,直接在开头添加9即可,故二者个数是相同的。
对于3),表示长度为i且含49时,只需在开头加上0~9,同时要加上长度为i-1且开头为9时的个数,因为这种情况只需在开头加个4即可。
所以,先对dp[][]打表。但是是不能直接求的,以上打出来的表都是在长度条件下的全部情况,而对于输入的N是可以任意的。既若输入500,dp[3][2]是999的情况,这里多计算了501~999这个区间上含49的值了。
处理方法是,用数组rec[1~len]存储N中从低到高的每个数位。对于要求的答案,从高位往下统计。对于当前位i,首先dp[i-1][2]肯定是符合条件的,比如49100,长度为5,第一位是4,那么长度为4的且含49的数都是符合情况的。再看第二位9,那么长度为3的含49的情况肯定是也统计范围内的,既dp[3][2]。此时注意到9是比4大的,那么449**都是符合题意的,故此时要加上dp[3][1]。最后,考虑第三位1,先统计dp[2][2],此时1前面已经包含了49,所以对于1**,只要小于1**的都是符合情况的,既1*dp[2][0].
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3558 Accepted Submission(s): 1245
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
做的第一道数位DP,参考了别人的思路,又长见识了。
首先定义状态,dp[i][0]表示长度为i且不含49的数有多少个,dp[i][1]表示长度为i,不含49但开头为9的数有多少个,dp[i][2]表示含49的数有多少个。
状态转移为:
1)dp[i][0] = dp[i-1][0]*10 - dp[i-1][1];
2)dp[i][1] = dp[i-1][0];
3)dp[i][2] = dp[i-1][2]*10 + dp[i-1][1];
对于1),表示长度为i时只需从长度为i-1的开头加上0~9这10个数,故乘以10,但是要除去i-1中开头为9的情况,因为在i-1且开头为9时,开头加上4就成了含49的情况了(对于任意k,dp[k][0]是包含dp[k][1]的)。
对于2),意思是长度为i且开头为9,只需在长度为i-1且不含49的情况下,直接在开头添加9即可,故二者个数是相同的。
对于3),表示长度为i且含49时,只需在开头加上0~9,同时要加上长度为i-1且开头为9时的个数,因为这种情况只需在开头加个4即可。
所以,先对dp[][]打表。但是是不能直接求的,以上打出来的表都是在长度条件下的全部情况,而对于输入的N是可以任意的。既若输入500,dp[3][2]是999的情况,这里多计算了501~999这个区间上含49的值了。
处理方法是,用数组rec[1~len]存储N中从低到高的每个数位。对于要求的答案,从高位往下统计。对于当前位i,首先dp[i-1][2]肯定是符合条件的,比如49100,长度为5,第一位是4,那么长度为4的且含49的数都是符合情况的。再看第二位9,那么长度为3的含49的情况肯定是也统计范围内的,既dp[3][2]。此时注意到9是比4大的,那么449**都是符合题意的,故此时要加上dp[3][1]。最后,考虑第三位1,先统计dp[2][2],此时1前面已经包含了49,所以对于1**,只要小于1**的都是符合情况的,既1*dp[2][0].
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define SIZE 32 using namespace std; typedef __int64 Int; Int dp[SIZE][4]; int rec[SIZE],len; int Case; Int N; int main() { memset(dp,0,sizeof(dp)); dp[0][0] = 1; for(int i=1; i<SIZE; 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]; } scanf("%d",&Case); while(Case--) { scanf("%I64d",&N); Int ans = 0; len = 0; N ++; while(N) { rec[++len] = N%10; N /= 10; } bool flag = false; int last = 0; for(int i=len; i>=1; i--) { ans += dp[i-1][2]*rec[i]; if(flag) ans += dp[i-1][0]*rec[i]; if(!flag && rec[i]>4) ans += dp[i-1][1]; if(last == 4 && rec[i] == 9) flag = true; last = rec[i]; } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- HDU - 3555 Bomb 数位DP
- hdu 3555 Bomb 【数位DP】
- HDU 3555 Bomb (简单数位DP)
- hdu 3555 Bomb 炸弹(数位DP,入门)
- (数位DP 1.2)hdu 3555 Bomb(统计1~n中,包含49的数的个数)
- [数位DP]Hdu 3555——Bomb
- hdu 3555 Bomb(数位dp)
- hdu 3555 Bomb(数位DP,4级)
- HDU 3555 Bomb(数位DP)
- HDU 3555 Bomb (数位DP)
- HDU 3555 Bomb 数位DP
- hdu 3555 Bomb ( 数位DP)
- HDU 3555 Bomb(数位DP)
- 【HDU 3555】Bomb 数位dp模板
- hdu 3555 Bomb (数位dp入门)
- hdu 3555 Bomb ( 数位DP)
- 数位DP入门之hdu 3555 Bomb
- HDU 3555 Bomb 数位DP
- HDU 3555 Bomb题解(数位dp)
- HDU 3555 Bomb(数位DP)