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. configurationmanager.getsection usage example.

    1.app.config(note that attribute case sensitive!) <?xml version="1.0" encoding="ut ...

  2. 無法使用 system/bin/r 讀取 pmic pm8937 hardware regitster 的原因

    Platform Qualcomm MSM8917 + PM8937 + PMI8940 起因 同事問我 PM8937 的 VREG_L17 如何設定成 3.3V, 從 PM8937 hardware ...

  3. 调用Thread.interrupt()方法到底会发生什么?

    1. 当线程处于Blocked状态(sleep,wait,join),线程会退出阻塞状态,并抛出一个InterruptedException.park除外,它有响应但是不会抛出异常 2. 当线程处于R ...

  4. python笔记6:模块

    6. 模块(一个 .py 文件称为一个模块Module) import 语句 类似 _xxx 和 __xxx 这样的 函数/变量 是非公开的(private),不应该被直接引用 函数定义: 外部不需要 ...

  5. httpd安装和配置(cgi、wsgi)

    参考:http://webpy.org/cookbook/mod_wsgi-apache.zh-cn 一.yum方式安装: 1.yum install httpd 输入y后继续. 2.看到一下类似的返 ...

  6. Hdoj 2509 Be the Winner

    Diciption Let's consider m apples divided into n groups. Each group contains no more than 100 apples ...

  7. java.lang.NoSuchMethodError: main Exception in thread "main" ===Exception

    java.lang.NoSuchMethodError: mainException in thread "main" 出现该异常是因为在之前我的项目中自定义了一个String类, ...

  8. 解决每次启动Office都出出现设置向导的问题

    昨天不知道怎么地,每次打开Excel都会出现"正在配置Office"的设置向导,并且修复.重置用户均无法解决. 在网上搜索了一下,找到了一个解决方法:把"C:\Progr ...

  9. 【Objective-C Runtime动态加载】---动态创建类Class

    a.使用objc_allocateClassPair创建一个类Class    const char * className = "Calculator";    Class kc ...

  10. GetModuleFileNameA()与GetCurrentDirectoryA()

    头文件: #include <windows.h> GetModuleFileNameA() char moduleFileName[MAX_PATH]; GetModuleFileNam ...