problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068

求出区间[A,B]内能被K整除且各位数字之和也能被K整除的数的个数。(1 ≤ A ≤ B < 231 and 0 < K < 10000)

算是最简单的数位dp了。k在这里是10000。三维数组都开不开。可是想想会发现A,B最多有10位,各位数字之和不会超过90。那么当 k >= 90时,就不用dp,由于个位数字之和对k取余不会等于0。

所以数组仅仅需开到dp[12][90][90]。



#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 4010; int dp[15][92][92];
int a,b,k;
int dig[15]; int dfs(int len, int mod1, int mod2, int up)
{
if(len == 0)
return mod1 == 0 && mod2 == 0;
if(!up && dp[len][mod1][mod2] != -1)
return dp[len][mod1][mod2];
int res = 0;
int n = up ? dig[len] : 9;
for(int i = 0; i <= n; i++)
{
int tmod1 = (mod1*10 + i)%k;
int tmod2 = (mod2 + i)%k;
res += dfs(len-1,tmod1,tmod2,up&&i==n);
}
if(!up)
dp[len][mod1][mod2] = res;
return res;
} int cal(int num)
{
int len = 0;
while(num)
{
dig[++len] = num%10;
num /= 10;
}
return dfs(len,0,0,1);
} int main()
{
int test;
scanf("%d",&test);
for(int item = 1; item <= test; item++)
{
scanf("%d %d %d",&a,&b,&k);
if(k >= 90)
{
printf("Case %d: 0\n",item);
continue;
}
memset(dp,-1,sizeof(dp));
printf("Case %d: %d\n",item,cal(b) - cal(a-1));
}
return 0;
}

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

  1. lightoj 1068 - Investigation(数位dp)

    An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is d ...

  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. AVRStudio 6 添加调试功能

    下载这个文件并安装就可以了:http://avr-jungo-usb.software.informer.com/download/ 具体可以看这个贴子:http://electronics.stac ...

  2. 为什么mfc的入口是InitInstance()而没有WinMain() (转)

    学过PE文件格式,就明白,程序在进入WinMain之前要做很多事情,比如初始Dos头,分配函数表,初始化全局变量,之后才进入程序入口(WinMain) MFC对WindowsAPI进行了封装.在用向导 ...

  3. 双击Table表格td变成text修改内容

    //先不多说这里上我的页面 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  4. MVC 二级联动 可以试试

    后台代码,获取数据如下: /// <summary> 2 /// 获取省份 3 /// </summary> 4 public JsonResult GetProvinceli ...

  5. autofac 用法总结

    autofac官网: http://autofaccn.readthedocs.io/en/latest/getting-started/index.html autofac作为一个热门ioc框架,还 ...

  6. (转)Kettle命令行

    kettle使用命令行来运行ktr和kjb 1:cmd方式运行 1.ktr的运行:运行transformation文件是通过Pan.bat来运行的. 打开cmd命令行窗口,转到Pan.bat所在的目录 ...

  7. Educational Codeforces Round 34 D. Almost Difference【模拟/stl-map/ long double】

    D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. TensorFlow——共享变量的使用方法

    1.共享变量用途 在构建模型时,需要使用tf.Variable来创建一个变量(也可以理解成节点).当两个模型一起训练时,一个模型需要使用其他模型创建的变量,比如,对抗网络中的生成器和判别器.如果使用t ...

  9. 10.1综合强化刷题 Day2

    a[问题描述]你是能看到第一题的 friends呢.                                                —— hja世界上没有什么比卖的这 贵弹丸三还令人绝 ...

  10. [LOJ6235]区间素数个数

    题目大意: 给定$n(n\leq10^{11})$,求$\pi(n)$. 思路: 计算$\pi$函数有$O(n^{\frac23})$的Lehmer算法,这里考虑$O(\frac{n^{\frac34 ...