lightOJ 1068 - Investigation (数位dp)
2016-07-15 11:03
218 查看
1068 - Investigation
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible by 3 and 12 (3+7+0+2) is also divisible by 3. This property also holds for the integer 9.
In this problem, we will investigate this property for other integers.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains three positive integers A, B and K (1 ≤ A ≤ B < 231 and 0 < K < 10000).
Output
For each case, output the case number and the number of integers in the range [A, B] which are divisible by K and the sum of its digits is also divisible by K.
Sample Input
Output for Sample Input
3
1 20 1
1 20 2
1 1000 4
Case 1: 20
Case 2: 5
Case 3: 64
这种题目考验智商!!
n大于82的时候是没用的,因为数位之和小于82.
还有,k在变化
所以每次都要memset
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible by 3 and 12 (3+7+0+2) is also divisible by 3. This property also holds for the integer 9.
In this problem, we will investigate this property for other integers.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains three positive integers A, B and K (1 ≤ A ≤ B < 231 and 0 < K < 10000).
Output
For each case, output the case number and the number of integers in the range [A, B] which are divisible by K and the sum of its digits is also divisible by K.
Sample Input
Output for Sample Input
3
1 20 1
1 20 2
1 1000 4
Case 1: 20
Case 2: 5
Case 3: 64
这种题目考验智商!!
n大于82的时候是没用的,因为数位之和小于82.
还有,k在变化
所以每次都要memset
#include<iostream> using namespace std; #include<cstring> #include<cstdio> #include<algorithm> int digit[20]; int k; int dp[10][83][83]; int dfs(int pos,int lim,int dig,int sum) { if(pos<0)return dig==0&&sum==0; if(!lim&&(~dp[pos][dig][sum]))return dp[pos][dig][sum]; int ans=0; int len=lim?digit[pos]:9; for(int i=0; i<=len; ++i) { ans+=dfs(pos-1,lim&&(i==len),((dig*10+i)%k)%83,(sum+i)%k); } if(!lim)dp[pos][dig][sum]=ans; return ans; } int work(int val) { int len=0; while(val) { digit[len++]=val%10; val/=10; } return dfs(len-1,1,0,0); } int main() { int n,m,T; int coun=0; scanf("%d",&T); while(T--&&scanf("%d%d%d",&n,&m,&k)) { memset(dp,-1,sizeof(dp)); printf("Case %d: %d\n",++coun,(work(m)-work(n-1))); } return 0; }
相关文章推荐
- 二路归并算法
- AbstractList抽象类源码解析
- 安卓与“Proguard”——安卓的代码混淆
- 禁用myeclipse updating indexes与大幅优化myeclipse的速度&关闭自动更新
- 2016.7.12 去除mybatis-generator生成的class里的注释
- spring boot mybatis 快速构建微服务
- 七天学会ASP.NET MVC(七)——创建单页应用
- 湾区日报是如何运作的?
- android开发类似淘宝京东头条的垂直跑马灯效果
- HDU 4726 Kia's Calculation
- CodeForces 567B Berland National Library
- 无线循环的两种方式
- 二叉搜索树与双向链表
- 图像聚类-K均值聚类
- 用反射的方式获取父类中的所有属性和方法
- JAVA编程常见错误集锦(1)
- Android 数据存储相关
- computer vision一些术语-目标识别、目标检测、目标分割、语义分割等
- php中使用apcu实现共享数据
- Part2:vector和string(一)