【HDU 4722】Good Numbers

Good Numbers

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3944 Accepted Submission(s): 1255

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.

[题意][计算区间里有多少个数各位上的数加起来是10的倍数]

【题解】【数位dp】

【dp预处理,f[i][j]表示i位数模10余数为j的有多少个(当从前一位递推到当前位时,不管前面几位加起来模10余数是几,都可以找到相应的数使它是10的倍数,所以可以直接继承)】

【在查找的时候要用last记录前面几位的和(因为当枚举到当前位时,低位上的数依然会影响答案)】

【依然用前缀和来确定答案】

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
ll f[20][50],h[20],tot;
ll n,m,T;
inline void dp()
{
    int i,j,k;
    f[0][0]=1;
    for(k=1;k<20;++k)
     for(i=0;i<10;++i)
      for(j=0;j<10;++j)
       f[k][i]+=f[k-1][(i+j)%10];
    return;
}
inline ll math(ll n)
{
    ll i,j,sum=0,last=0;
    memset(h,0,sizeof(h)); tot=0;
    while(n) h[++tot]=n%10,n/=10;
    for(i=tot;i>0;--i)
     {
        for(j=0;j<h[i];++j)
         sum+=f[i-1][((0-(last+j))%10+10)%10];
        last+=h[i];
     }
    return sum;
}
int main()
{
    dp();
    scanf("%I64d",&T);
    for(int i=1;i<=T;++i)
     {
        scanf("%I64d%I64d",&n,&m);
        printf("Case #%d: %I64d\n",i,math(m+1)-math(n));
     }
    return 0;
}
时间: 2024-10-20 06:28:28

【HDU 4722】Good Numbers的相关文章

【HDU 1887】Weird Numbers(负进制转换)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1887 题意:有两种操作,from-r n代表把一个-r进制下的数字n转换成10进制,to-r n代表把一个10进制下的数字n转化成-r进制. 分析:两种操作中from操作很容易实现,和转换成正进制一样,没什么好说的,而to操作我们首先可以像正常转化一样进行短除法,举个例子: 把15转化为-3进制.短除法并且倒序之后结果为1,-2,0.而我们的进制中是不能出现-2的,所以我们可以这样考虑,-2这位代表的

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

[HDU 1839] Delay Constrained Maximum Capacity Path(二分+最短路) Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 481 Problem

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 1009】FatMouse&#39; Trade

题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of c

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

不容易啊,终于可以补第二个题了!! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵,每一个格子里都将有一个数.给你每一行数字之和和每一列数字之和.求每一个位置能填0~k之间的哪个数.如果有多种可能输出"Not Unique",如果没有解输出"Impossible",如果一组解则将其输出. 解题思路: 最大流: 不可能的条件:是行之和和列之和不想等或者建图后的最大流与他们不想等. 多组的条件是:在最大流

【HDU 5811】Colosseo(拓扑+输入优化)

[HDU 5811]Colosseo(拓扑+输入优化) Colosseo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 98 Problem Description Mr. Chopsticks keeps N monsters, numbered from 1 to N.