您的位置:首页 > 编程语言 > Go语言

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的倍数!


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));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM dp