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. angular6组件通信

    此文章是用markdown书写,赋值全部到vscode打开即可. # Angular组件通信 ## .父组件传递数据到子组件 - `@Input`:属性绑定,父组件向子组件传递数据 ```js // ...

  2. Golang Context 包详解

    Golang Context 包详解 0. 引言 在 Go 语言编写的服务器程序中,服务器通常要为每个 HTTP 请求创建一个 goroutine 以并发地处理业务.同时,这个 goroutine 也 ...

  3. Java性能权威指南读书笔记--之一

    JIT(即时编译) 解释型代码:程序可移植,相同的代码在任何有适当解释器的机器上,都能运行,但是速度慢. 编译型代码:速度快,电视不同CPU平台的代码无法兼容. java则是使用java的编译器先将其 ...

  4. 48.QT-网络通信讲解1

    网络概念 MAC地址(硬件地址) 网络IP地址(如192.168.1.101) 网络端口(实现多路通信,用来给不同应用程序来区分使用,范围0~65535,比如浏览网页服务(80端口), FTP服务(2 ...

  5. extjs4 表单验证自定义

    extjs4 在验证上面支持的也特别好,他可以使用自带的格式验证,也可以自定义验证 比如:正则验证,密码重复填写对比验证,以及 调用后台方法验证,下面将验证方法统一写出以供参考 function lo ...

  6. 【Java例题】8.1手工编写加法器的可视化程序

    1. 手工编写加法器的可视化程序. 一个Frame窗体容器,布局为null,三个TextField组件,一个Button组件. Button组件上添加ActionEvent事件监听器ActionLis ...

  7. HTML/CSS:div水平与元素垂直居中(2)

    单个div水平居中:设置margin的左右边距为自动 div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中 ...

  8. mysql中防止sql注入

    什么是sql注入 图片来源:百度百科 python 操作mysql产生sql注入问题 不用ORM框架,框架中已经集成了防范sql注入的功能,使用pymysql实践一下: # 导入pymysql模块 i ...

  9. VS调试时修改代码

    最近碰到一个问题,就是vs在调试模式下无法修改代码之后再继续,这种严重影响工作效率的问题怎么能忍,所以决心把这个坑填满.网上搜了大堆有头无尾有尾无头的答案,我一个一个试了几乎都没啥用.最后通过不断的测 ...

  10. (二)对象以及变量的并发访问--synchronized的使用细节,用法

    具体的记录synchronized关键的各种使用方式,注意事项.感觉一步一步跟我来都可以看懂滴 大致是按照以下思路进行书写的.黑体字可以理解为结论, 1.synchronized锁的是什么? 2.sy ...