HDU4722Good Numbers热身赛2 1007题(思维题 不用数位dp照样可以做的)

来源:互联网 发布:西门子plc的编程技巧 编辑:程序博客网 时间:2024/05/22 21:19

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
21 101 20
 

Sample Output
Case #1: 0Case #2: 1
Hint
The answer maybe very large, we recommend you to use long long instead of int.
 

Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
 

题目大意:给你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;}/*2101 101 2032 4332 4691 1090 1001 100*///46MS 232K


原创粉丝点击