hdu 4722 Good Numbers 数位dp
来源:互联网 发布:mac菜单栏透明 编辑:程序博客网 时间:2024/05/22 12:42
Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 602 Accepted Submission(s): 220
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.
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).
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: 1HintThe answer maybe very large, we recommend you to use long long instead of int.
求区间[l,r]之间有多少个数,各位数和能被10整除。
转化为get(x),区间[0,x]之间有多少个数满足条件。
则答案即为get(r)-get(l-1),问题转化为数位dp。
将x的各位数字储存在bit[]中。
考虑区间[0~5132]
可以转化成[0~999] 、[1000~1999]、[2000~2999]、[3000~3999]、[4000~4999]、[5000~5099]、[5100~5109]、[5110~5119]、[5120~5129]、[5130~5132]
令f[i][j]表示前i位和模10得j的方案数。若i为最后一位,若j=0则f[i][j]=1,否则f[i][j]=0。
用记忆化搜索实现数位dp
dp(int p,int m,bool flag),flag=true 表示当前位p无论取多少都小于数x 。
则dp(i,m) += dp(i+1,(m+k)%10) 0=<k<= (9 or bit[i])
-------------------------
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;typedef long long LL;const int maxn=111;LL f[maxn][11];int bit[maxn];int n;LL dp(int p,int m,bool flag){ if (p==0) return (m==0); if (flag&&f[p][m]!=-1) return f[p][m]; int k=flag?9:bit[p]; LL res=0; for (int i=0;i<=k;i++){ res+=dp(p-1,(m+i)%10,flag||k!=i); } if (flag) f[p][m]=res; return res;}LL get(LL x){ memset(f,-1,sizeof(f)); if (x<0) return 0; n=0; while (x!=0){ bit[++n]=x%10; x/=10; } return dp(n,0,0);}int main(){ int T; scanf("%d",&T); for (int cas=1;cas<=T;cas++){ LL x,y; scanf("%I64d%I64d",&x,&y); printf("Case #%d: %I64d\n",cas,get(y)-get(x-1)); } return 0;}
- hdu 4722 Good Numbers 数位dp
- 【数位DP】 HDU 4722 Good Numbers
- hdu 4722 Good Numbers 数位dp
- HDU 4722 Good Numbers (数位dp)
- [数位dp] hdu 4722 Good Numbers
- hdu 4722 Good Numbers(数位dp)
- hdu 4722 Good numbers(数位DP)
- HDU 4722-Good Numbers-数位dp
- 【HDU 4722】Good Numbers(数位dp)
- HDU 4722 Good Numbers(数位DP)
- HDU 4722 Good Numbers(数位DP)
- HDU 4722Good Numbers(数位dp)
- HDU 4722 Good Numbers(找规律+数位DP)
- hdu 4722 Good Numbers(初涉数位dp)
- Good Numbers (数位DP)
- HDU 4722 Good Numbers 数位dp或找规律枚举 数位dp感悟
- HDOJ 4722 - Good Numbers 数位dp
- HDOJ 4722Good Numbers 数位DP
- Android项目怎么使用外部工程作为jar包
- MapReduce: 提高MapReduce性能的七点建议
- GIS空间索引(2)--格网索引与四叉树索引
- 设置Android应用程序全屏显示的两种方法
- js 正则表达式
- hdu 4722 Good Numbers 数位dp
- MiniGUI源码分析——DC分析
- nltk一些函数意思
- 两个相连的div
- session超时跳出iframe并跳到登陆页面
- hibernate延迟加载
- div怎样在object上面显示
- 何避免网站优化过度?
- Ice 读书笔记--Slice语言