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. 【Android】SDK Manager 设置代理

    这里是 Mac 系统下,Windows 环境类似.打开 Android SDK Manager, Proxy Settings 设置如下所示: PS: 注意勾选 "Force https:/ ...

  2. Usaco Training [1.3] wormhole

    传送门 解题要素:代码能力 解题步骤:理解题意 - >搜索枚举所有可能的配对情况 - >判断冲突并求解 - >调试 一. 理解题意 这里讲几个不容易理解的点: 1. +x方向 即向右 ...

  3. @Value注解 和 @Data注解

    @Value注解 service层代码 @Service public class HelloServiceImpl implements HelloService { @Autowired priv ...

  4. LR(1)语法分析器生成器(生成Action表和Goto表)java实现(二)

    本来这次想好好写一下博客的...结果耐心有限,又想着烂尾总比断更好些.于是还是把后续代码贴上.不过后续代码是继续贴在BNF容器里面的...可能会显得有些臃肿.但目前管不了那么多了.先贴上来吧hhh.说 ...

  5. java学习-NIO(一)简介

    I/O简介 在 Java 编程中,直到最近一直使用 流 的方式完成 I/O.所有 I/O 都被视为单个的字节的移动,通过一个称为 Stream 的对象一次移动一个字节.流 I/O 用于与外部世界接触. ...

  6. 如何成为PHP程序员?

    当今,互联网的蓬勃发展,移动互联网的火热,以及国家提出的“互联网+”.这些趋势可以让我们明显的感觉到互联网的重要,不可替代.网站也是大家最早接触,最早认识的一种新事物.谈到网站,无非最长脸的莫过于PH ...

  7. 大数据学习之旅2——从零开始搭hadoop完全分布式集群

    前言 本文从零开始搭hadoop完全分布式集群,大概花费了一天的时间边搭边写博客,一步一步完成完成集群配置,所以相信大家按照本文一步一步来完全可以搭建成功.需要注意的是本文限于篇幅和时间的限制,也是为 ...

  8. 颜色下拉菜单(combox)

    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...

  9. thinkPhP 引入Smarty模板引擎及配置

    做配置:  TMPL_ENGINE_TYPE = “Smarty” 给smarty做配置: TMPL_ENGINE_CONFIG = array( 左标记, 右标记, )

  10. React SPA 应用 hash 路由如何使用锚点

    当我们在做 SPA 应用的时候,为了兼容老的浏览器(如IE9)我们不得不放弃 HTML5 browser history api 而只能采用 hash 路由的这种形式来实现前端路由,但是因为 hash ...