HDU4722Good Numbers熱身賽2 1007題(思維題 不用數位dp照樣可以做的)

果7發表於2013-09-11

Good Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 268    Accepted Submission(s): 90


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.
 

Source
 

題目大意:給你a,b兩個數(a<=b),問你a~b滿足各個數位相加整除10的個數,含邊界。

 解題思路:說下我的總體思想吧。先列舉一下0~200內滿足條件的值,0,19,28,37,46,55,64,73,82,91,109,118,127,136,145,154,163,172,181,190.規律應該很顯然了,0~10中有一個,10~20中有一個。。。一次的每十個中必有一個。可以自己在紙上畫一下,一個很大的數共有n位,前面n-1位的和是p而最後一位還沒確定,最後一位可以取q=0~9,可以自己想一下結果肯定是(p+q)%10,而模上10以後的結果必是從0~9所以每十個裡面有一個。 下面的就好辦了。問你a~b滿足條件的個數,就先求邊界c~d(c>=a且最小的滿足條件的數,d<=b且最小的滿足條件的數)。不過個數為0需要特殊判斷一下。詳見程式碼。

 題目地址:Good Numbers

AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
char a[12][12];

int is(__int64 x)
{
    int ans=0;
    while(x)
    {
        ans+=x%10;
        x/=10;
    }
    if(ans%10==0)
        return 1;
    return 0;
}

int main()
{
    int tes;
    __int64 res;
    __int64 a,b;
    scanf("%d",&tes);
    int cas=0;
    while(tes--)
    {
        scanf("%I64d%I64d",&a,&b);
        while(!is(a)) //先找到>=a的最小數位相加整除10的數
            a++;
        while(!is(b)) //先找到<=b的最大數位相加整除10的數
            b--;
        if(a>b)
            res=0;
        else //即為a<=b的時候再用相減的方法,保險起見
           res=b/10-a/10+1; //每10個裡面有一個
        printf("Case #%d: %I64d\n",++cas,res);
    }
    return 0;
}

/*
210
1 10
1 20
32 43
32 46
91 109
0 100
1 100
*/
//46MS 232K


相關文章