HDU 4722 数位DP
2015-11-10 00:00
288 查看
Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3620 Accepted Submission(s): 1147
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For test case X, output "Case #X: " first, then output the number of good numbers in a single line.
[align=left]Sample Input[/align]
2
1 10
1 20
[align=left]Sample Output[/align]
Case #1: 0
Case #2: 1
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int MAX=22;
__int64 dp[MAX][10];//分别代表长度为i位数和mod 10为j的个数
int digit[MAX];
void digit_dp(){//计算每长度为i为的数mod 10 == 0的个数
dp[0][0]=1;
for(int i=1;i<MAX;++i){
for(int j=0;j<10;++j){
for(int k=0;k<10;++k){
dp[i][j]+=dp[i-1][(j-k+10)%10];
}
}
}
}
__int64 calculate(__int64 n){
int size=0,last=0;
__int64 sum=0;
while(n)digit[++size]=n%10,n/=10;
for(int i=size;i>=1;--i){
for(int j=0;j<digit[i];++j){
sum+=dp[i-1][((j+last)%10+10)%10];
}
last=(last+digit[i])%10;
}
return sum;
}
int main(){
digit_dp();
int t,num=0;
__int64 a,b;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d",&a,&b);
printf("Case #%d: %I64d\n",++num,calculate(b+1)-calculate(a));
}
return 0;
}
相关文章推荐
- OC中方法与函数的区别
- 《深入理解java虚拟机》学习笔记4——Java虚拟机垃圾收集器
- java webservice
- windows server 2008
- ssl协议讲解
- [非凡程序员]归档 解档 appkit
- 插入数据
- /etc/skel/ 家目录模板
- 如何美化MindMapper中文破解版中的主题
- CentOS7下安装python包管理工具pip
- Data truncated for column 'expected_return' at row
- 通过RS232发送和接收短信(一)
- extjs6入门:用sencha cmd搭建简单的extjs6项目
- iOS程序进入后台后仍运行定时器NSTimer
- asp.net web常用控件FileUpload(文件上传控件)
- Centos linux 下配置 apache ,创建虚拟网站访问路径
- 通过redis命令实现性能监控
- 教你ZBrush中的Z球使用方法
- 蜂窝教育iOS培训 助你成就高薪梦想
- 单选按钮和复选按钮