Lightoj 1068(数位DP)
求一段区间中被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)的更多相关文章
- lightoj 1021 (数位DP)
题意:给你一个b进制的数,再给你一个十进制数k,你可以重新排列b进制数的每一位得到其他b进制数,问你这些数中有多少可以整除k? 思路:数位dp. #include <cstdio> #in ...
- LightOJ - 1032 数位DP
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...
- Lightoj 1140(数位DP)
求一个区间内的数含有多少个0. dp[len][pre]表示长度为len的数,含有pre个0. 需要加一个标记,来表示前缀是否为0(可以是一串连续的0),如果前缀一直为0,就一直搜,如果前缀不为0,就 ...
- light oj 1068 数位dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- lightoj 1205 数位dp
1205 - Palindromic Numbers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...
- LightOJ 1068 Investigation (数位dp)
problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...
- LightOJ 1032 - Fast Bit Calculations 数位DP
http://www.lightoj.com/volume_showproblem.php?problem=1032 题意:问1~N二进制下连续两个1的个数 思路:数位DP,dp[i][j][k]代表 ...
- lightoj 1021 - Painful Bases(数位dp+状压)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1021 题解:简单的数位dp由于总共就只有16个存储一下状态就行了.求各种进制能 ...
- light oj 1068 - Investigation 数位DP
思路:典型的数位DP!!! dp[i][j][k]:第i位,对mod取余为j,数字和对mod取余为k. 注意:由于32位数字和小于95,所以当k>=95时,结果肯定为0. 这样数组就可以开小点, ...
随机推荐
- hust 1017 dancing links 精确覆盖模板题
最基础的dancing links的精确覆盖题目 #include <iostream> #include <cstring> #include <cstdio> ...
- 【二分+交互】codeforces B. Glad to see you!
codeforces.com/contest/809/problem/B 只需要找到2个被选中的,首先,注意到将区间二等分时左侧区间为[l,mid],右侧区间为[mid+1,r],dui(mid,mi ...
- HDU 3763 CDs
http://acm.hdu.edu.cn/showproblem.php?pid=3763 题意: 两组数据 看重复的有多少 如果每输入一个就去查找的话O(n^2) 会超时 所以可以用二法 第一组数 ...
- linux命令2——进程相关
(1)ps -ef :可以看到内核的线程.
- BMP格式,转载
BMP文件格式,又称为Bitmap(位图)或是DIB(Device-Independent Device,设备无关位图),是Windows系统中广泛使用的图像文件格式.由于它可以不作任何变换地保存图像 ...
- Intersection of Two Linked Lists(链表)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【APUE】进程基础
进程标识符:非负整数 ID为0的进程通常是是调度进程,常被称为交换进程.该进程是内核的一部分,它并不执行任何磁盘上的程序,因此也被称为系统进程 ID为1的进程是init进程,在自举过程结束时由内核调用 ...
- Angular2.x
Angular版本 Angular1和Angular4分别是Angular的两个版本,也就是Angular1.x和Angular2.x(除了Angular1以外,其余都属于Angular2.x). 1 ...
- 设计模式 之代理(Proxy)模式
为什么这里要定义代理呢?所谓代理代理,当然就是你不想做的事.找别人去做,这就是代理.所以,当你写代码的时候.你想保持类的简单性.重用性.你就能够把事件尽量都交给其他类去做.自己仅仅管做好自己的事.也就 ...
- PHP根据两点间的经纬度计算距离
/** * 说明: 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ function getDistance($lat ...