HDU 4722 Good Numbers 2013年成都邀请赛原题

126 篇文章 2 订阅

题意很简单就是给你两个数字,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 <= 10 18).
 

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));
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值