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.

题意:给你3个数A,B,K,求A~B之间有几个数能被K整除,而且各位数之和也能被K整除。

这题类似hdu3652,方法类似,就是k看起来有点大有10000这么大,但是这么大并没什么用啊,总共不超过10位位数之和最多才90。

所以当k大于90时,直接是0了。

dp[len][mod][count],len表示当前位数,mod表示上一位数 mod k 的余数,count表示位数之和。

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
ll dp[20][100][100];
ll a[20] , b[20];
int k;
ll dfs(int len , int mod , int flag , ll s[] , int count) {
if(len == 0) {
return mod == 0 && (count % k) == 0;
}
if(!flag && dp[len][mod][count] != -1) {
return dp[len][mod][count];
}
int t = flag ? s[len] : 9;
ll sum = 0;
for(int i = 0 ; i <= t ; i++) {
sum += dfs(len - 1 , (mod * 10 + i) % k , flag && i == t , s , count + i);
}
if(!flag)
dp[len][mod][count] = sum;
return sum;
}
ll Get(int x , int y) {
int len1 = 0 , len2 = 0;
memset(dp , -1 , sizeof(dp));
memset(a , 0 , sizeof(a));
memset(b , 0 , sizeof(b));
while(x) {
a[++len1] = x % 10;
x /= 10;
}
while(y) {
b[++len2] = y % 10;
y /= 10;
}
return dfs(len1 , 0 , 1 , a , 0) - dfs(len2 , 0 , 1 , b , 0);
}
int main()
{
int t;
cin >> t;
int ans = 0;
while(t--) {
ans++;
int A , B;
cin >> A >> B >> k;
cout << "Case " << ans << ": ";
if(k >= 90)
cout << 0 << endl;
else
cout << Get(B , A - 1) << endl;
}
return 0;
}

lightoj 1068 - Investigation(数位dp)的更多相关文章

  1. LightOJ 1068 Investigation (数位dp)

    problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...

  2. light oj 1068 - Investigation 数位DP

    思路:典型的数位DP!!! dp[i][j][k]:第i位,对mod取余为j,数字和对mod取余为k. 注意:由于32位数字和小于95,所以当k>=95时,结果肯定为0. 这样数组就可以开小点, ...

  3. LightOJ 1140 计数/数位DP 入门

    题意: 给出a,b求区间a,b内写下过多少个零 题解:计数问题一般都会牵扯到数位DP,DP我写的少,这道当作入门了,DFS写法有固定的模板可套用 dp[p][count] 代表在p位 且前面出现过co ...

  4. Investigation LightOJ - 1068

    Investigation LightOJ - 1068 常规数位dp题,对于不同k分开记忆化.注意:k大于82(1999999999的数位和)时不会有答案,直接输出0即可.还有,按照这种记录不同k时 ...

  5. LightOJ 1032 - Fast Bit Calculations 数位DP

    http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...

  6. lightoj 1021 - Painful Bases(数位dp+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...

  7. LightOJ1068 Investigation(数位DP)

    这题要求区间有多少个模K且各位数之和模K都等于0的数字. 注意到[1,231]这些数最大的各位数之和不会超过90左右,而如果K大于90那么模K的结果肯定不是0,因此K大于90就没有解. 考虑到数据规模 ...

  8. lightoj 1021 (数位DP)

    题意:给你一个b进制的数,再给你一个十进制数k,你可以重新排列b进制数的每一位得到其他b进制数,问你这些数中有多少可以整除k? 思路:数位dp. #include <cstdio> #in ...

  9. 数位dp(D - How Many Zeroes? LightOJ - 1140 )

    题目链接:https://cn.vjudge.net/contest/278036#problem/D 题目大意:T组测试数据,每一次输入两个数,求的是在这个区间里面,有多少个0,比如说19203包括 ...

随机推荐

  1. python多线程详解

    目录 python多线程详解 一.线程介绍 什么是线程 为什么要使用多线程 二.线程实现 threading模块 自定义线程 守护线程 主线程等待子线程结束 多线程共享全局变量 互斥锁 递归锁 信号量 ...

  2. Oracle SQL常用内置系统函数总结

    Oracle数据库  内置系统函数主要分为以下类别:数学函数.字符串函数.日期函数.转换函数.聚合函数.分析聚合函数 一.数学函数 ------------返回数字       abs(n):返回数字 ...

  3. 手机APP测试之Fiddler

    之前测试基本上是web端,突然接手了一个要在指定pad上测试APP的任务,于是决定研究研究pad抓包.最开始考虑有jmeter进行抓包测试,发现抓不到(可能方法有问题,后续还需继续研究),然后用fid ...

  4. CentOS 7服务器安装brook和bbr加速

    一.安装Brook 执行一键部署脚本 $ wget -N --no-check-certificate wget -N --no-check-certificate https://raw.githu ...

  5. 算法实战-OJ之旅

    算法虽然不是特别简单,但没有你想象中的那么难. Sort Array By Parity easy AC-17ms. 按照<算法导论>排序一章的一些概念,第二种可以称为是原址的(in-pl ...

  6. Selenium+java - 日期控件的处理

    前言 一般的日期控件都是input标签下弹出来的,设置日期使用selenium中的sendKeys 方法就可以解决. 但是我们也会碰到下面的时间日期控件(这个时候这个文本框是不允许我们输入时间的)如图 ...

  7. hdu1241 油田计数

    具体思路:求联通块,在"@“的周围进行dfs,使用8个方向向量来代表搜索的方向 贴一下我的主要代码段: int dir[8][2]={{1,1},{-1,-1},{1,-1},{-1,1}, ...

  8. [luogu4886] 快递员(点分治,树链剖分,lca)

    dwq推的火题啊. 这题应该不算是点分治,但是用的点分治的思想. 每次找重心,算出每一对询问的答案找到答案最大值,考虑移动答案点,使得最大值减小. 由于这些点一定不能在u的两颗不同的子树里,否则你怎么 ...

  9. springboot启动慢解决方法

    jdk的配置文件中,使用securerandom.source设置了熵源: cat /usr/java/jdk1.8.0_121/jre/lib/security/java.security secu ...

  10. day0203

    day02 1.for i in range() --->用于设置for循环的迭代设置. ranage 也是一个前闭后开的. 2.random.randrange() --->随机产生给予 ...