HDU 4722 Good Numbers 2013年成都邀请赛原题
2013-09-13 11:15
253 查看
题意很简单就是给你两个数字,A,B。让你求出A和B之间所有 Good Numbers 的个数。
Good Numbers的定义是:如果一个数各个位置上的数字的和是10的倍数那么这个数就是 Good Numbers。比如19。
因为1+9 = 10 所以19是 Good Numbers。
由于数据范围很大10^18所以一定不可能是搜索,应该是一个数学推理;一开始我跟ZP学的,比如9543,分为9000+500+40+3;9000之前有9*10*10 种情况,500有5*10种情况,40有四种情况,而3要看和之前的数字加起来的和是否为10的倍数,是就是1,否则是0;但是不知道为什的写的时候老是出错,wa了好几遍、、后来感觉不好写有漏洞、、就放弃了。
后来跟着崔老师学用dp来做这个题,因为我是dp初学者所以学的也比较慢,老长时间才做出来、、
用dp的思路和之前的差不多,就是枚举每一位之前的数字,记录每次的个数;最后在加起来,这样还比较节约时间、、
需要注意的是0也是10的倍数!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 739 Accepted Submission(s): 276
Problem Description
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 1018).
Output
For test case X, output "Case #X: " first, then output the number of good numbers in a single line.
Sample Input
2
1 10
1 20
Sample Output
Case #1: 0
Case #2: 1
Hint
The answer maybe very large, we recommend you to use long long instead of int.
Good Numbers的定义是:如果一个数各个位置上的数字的和是10的倍数那么这个数就是 Good Numbers。比如19。
因为1+9 = 10 所以19是 Good Numbers。
由于数据范围很大10^18所以一定不可能是搜索,应该是一个数学推理;一开始我跟ZP学的,比如9543,分为9000+500+40+3;9000之前有9*10*10 种情况,500有5*10种情况,40有四种情况,而3要看和之前的数字加起来的和是否为10的倍数,是就是1,否则是0;但是不知道为什的写的时候老是出错,wa了好几遍、、后来感觉不好写有漏洞、、就放弃了。
后来跟着崔老师学用dp来做这个题,因为我是dp初学者所以学的也比较慢,老长时间才做出来、、
用dp的思路和之前的差不多,就是枚举每一位之前的数字,记录每次的个数;最后在加起来,这样还比较节约时间、、
需要注意的是0也是10的倍数!
Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 739 Accepted Submission(s): 276
Problem Description
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 1018).
Output
For test case X, output "Case #X: " first, then output the number of good numbers in a single line.
Sample Input
2
1 10
1 20
Sample Output
Case #1: 0
Case #2: 1
Hint
The answer maybe very large, we recommend you to use long long instead of int.
#include <stdio.h> #include <string.h> #include <iostream> #include <string.h> #include <cmath> #define LL long long LL dp[22][22]; LL ff(LL x) { LL ans = 0; int sum, i, j, n = 0, num[23]; if(x < 0) return 0; if(x >= 0 && x <= 9) return 1; while(x){ num[n++] = x%10; x /= 10; } ans += dp[n-1][0]; for(i = 1; i < num[n-1]; i++) ans += dp[n-1][10-i]; sum = num[n-1]; for(i = n-2; i >= 0; i--){ if(i == 0){ for(j = 0; j <= num[i]; j++) if(!((j+sum)%10)) ans ++; } else{ for(j = 0; j < num[i]; j++) ans += dp[i][(20-sum-j)%10]; sum = (sum+j)%10; } } return ans; } int main() { LL x, y; int i, j, k, n, case1 = 1; for(i = 0 ; i < 10; i++) dp[1][i] = 1; for(i = 2; i <= 20; i++) for(j = 0; j < 10; j++) for(k = 0; k < 10; k++) dp[i][(j+k)%10] += dp[i-1][j]; scanf("%d",&n); while(n--) { scanf("%I64d %I64d",&x, &y); printf("Case #%d: %I64d\n",case1++,ff(y) - ff(x-1)); } }
相关文章推荐
- HDU 4727 The Number Off of FFF 2013年成都全国邀请赛原题
- HDU 4722 Good Numbers 2013年四川省赛题
- HDU 4727 The Number Off of FFF(2013年成都邀请赛)
- HDU 4565 - 2013年长沙邀请赛 A-so easy
- hdu 4722 Good Numbers 数位dp
- 2013年成都邀请赛解题报告
- HDU-4722-Good Numbers(找规律)
- HDU 4722 Good Numbers(找规律)
- hdu 4722 Good Numbers
- HDU 4722 Good Numbers
- 2013年成都邀请赛解题报告
- 13成都邀请赛 1007 Good Numbers
- HDU 4722 Good Numbers
- 13成都邀请赛 1007 Good Numbers
- 【数位DP】 HDU 4722 Good Numbers
- HDU 4722 Good Numbers(数位DP)
- [数位dp] hdu 4722 Good Numbers
- HDU 4588 Count The Carries (2013年南京邀请赛)
- Hdu 4722 Good Numbers
- 13成都邀请赛 1007 Good Numbers