求一段区间中被k整除,各个位数相加之和被k整除的数的个数。

这不是重点,重点是k太大了,最大值有10000,所以不能直接开那么大的数组。

仔细分析一下可以发现,由于数最大是2的31次方(2147483648),所以当k>90时,直接输出0即可。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
#define maxn 30
LL dp[maxn][][];
LL digit[maxn];
LL n,m,K;
LL dfs(int len,LL pre,LL before,bool fp)
{
if(!len)
{
return pre==&&before==;
}
if(!fp && dp[len][pre][before] != -)
return dp[len][pre][before];
LL ret = ;
LL fpmax = fp ? digit[len] : ;
for(int i=;i<=fpmax;i++)
{
ret += dfs(len-,(pre*+i)%K,(before+i)%K,fp && i == fpmax);
}
if(!fp)
dp[len][pre][before] = ret;
return ret;
} LL f(LL n)
{
int len = ;
while(n)
{
digit[++len] = n % ;
n /= ;
}
return dfs(len,,,true);
} void init()
{
memset(dp,-,sizeof(dp));
}
int main()
{
//freopen("test.txt","r",stdin);
// cout<< ((LL)1<<31)<<endl;
int t;
scanf("%d",&t);
int Case=;
while(t--)
{
scanf("%d%d%d",&n,&m,&K);
if(K>)
{
printf("Case %d: 0\n",++Case);
continue;
}
init();
printf("Case %d: %lld\n",++Case,f(m)-f(n-));
}
return ;
}

Lightoj 1068(数位DP)的更多相关文章

  1. lightoj 1021 (数位DP)

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

  2. LightOJ - 1032 数位DP

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  3. Lightoj 1140(数位DP)

    求一个区间内的数含有多少个0. dp[len][pre]表示长度为len的数,含有pre个0. 需要加一个标记,来表示前缀是否为0(可以是一串连续的0),如果前缀一直为0,就一直搜,如果前缀不为0,就 ...

  4. light oj 1068 数位dp

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  5. lightoj 1205 数位dp

    1205 - Palindromic Numbers    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  6. LightOJ 1068 Investigation (数位dp)

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

  7. LightOJ 1032 - Fast Bit Calculations 数位DP

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

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

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

  9. light oj 1068 - Investigation 数位DP

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

随机推荐

  1. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

  2. hdu1856 选出更多的孩子

    题目大意: 老师选取2个学生对应的号码,这两人视作朋友,同时朋友的朋友也可以看成自己的朋友. 最后老师选出一个人数最多的朋友圈. 这里学生的人数不大于10^7,所以操作时需要极为注意,操作步数能省则省 ...

  3. UML的关联(Association), 聚合(Aggregation), 组合(Composition)区别

    转载:http://blog.csdn.net/ocean181/article/details/6117369 UML的关联(Association), 聚合(Aggregation), 组合(Co ...

  4. bzoj5108 [CodePlus2017]可做题 位运算dp+离散

    [CodePlus2017]可做题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 87  Solved: 63[Submit][Status][Dis ...

  5. BZOJ1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名

    n<=1000头牛各有一个未知值Ai,已知m<=10000条形如Ax>Ay的不等关系,求将整个序列排序的最少比较次数. Aa>Ab,Ab>Ac -------> A ...

  6. CSS属性操作二

    9.float属性 基本浮动规则 先来了解一下block元素和inline元素在文档流中的排列方式. block元素通常被现实为独立的一块,独占一行,多个block元素会各自新起一行,默认block元 ...

  7. centos 中找不到 php-config

    两种解决方式: 1.CentOS6 PHP extension install: Cannot find php-config. Please use --with-php-config=PATH 2 ...

  8. python学习之 - re模块

    re模块功能:实现字符串匹配. 元字符 描述\ 将下一个字符标记符.或一个向后引用.或一个八进制转义符.例如,“\\n”匹配\n.“\n”匹配换行符.序列“\\”匹配“\”而“\(”则匹配“(”.即相 ...

  9. MongoDB学习day09--Mongoose aggregate 多表关联查询

    Mongodb的aggregate应用之前已经说过了. 可以参考day06 Mongoose的aggregate函数应用 var mongoose=require('./db.js'); var Or ...

  10. lemon oa前端页面——由user-base-list谈项目组织

    content user-base-list.jsp中指定<%pageContext.setAttribute("currentHeader", "user&quo ...