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. 这样数组就可以开小点, ...
随机推荐
- POJ 2352 star level
题目链接: http://poj.org/problem?id=2352 题目大意:对于每一颗星星来说,都有一个属于自己的level,这个值为其他星星x,y坐标均不大于本星星的个数.输入时按先y由小到 ...
- hdu 4710
#include<stdio.h> #include<math.h> __int64 min(__int64 a,__int64 b) { return a<b?a:b; ...
- Ubuntu安装sublime Text 3并配置可以输入中文
使用Ubuntu系统后,想找一个顺手的编辑器,sublime作为我的首选编辑器,在安装和配置可输入中文时遇到各种个样的问题,总结一些: 1:问题: 我的系统是Ubuntu 18.04 LTS,尝试多次 ...
- COdevs 1251 括号
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 计算乘法时,我们可以添加括号,来改变相乘的顺序,比如计算X1, X2, X3, X4 ...
- Permutation Sequence(超时,排列问题)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- IDEA下使用protobuf2(java)
目录 一.介绍 二.特点 三.结构 四.选择版本 五.Intellij IDEA中使用Protobuf 1.下载个protoc.exe 2.编辑个.proto文件 3.将.proto文件转成Java类 ...
- Prime Distance(二次筛素数)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- hdu4183往返经过至多每一个点一次/最大流
题意:从s到t,每一个点有f值,仅仅能从f值小的到大的.到T后回来.仅仅能从f值大的到 小的,求可行否. 往返,事实上就是俩条路过去(每一个点最多一次).所以想到流量为2,跑最大流.看是否满2,又要每 ...
- 利用shuf对数据记录进行随机采样
最近在用SVM为分类器做实验,但是发现数据量太大(2000k条记录)但是训练时间过长...让我足足等了1天的啊!有人指导说可以先进行一下随机采样,再训练,这样对训练结果不会有太大影响(这个待考证).所 ...
- udhcp源码详解(二)--转
定义的数据结构对于C程序的重要性,不言而喻.面向对象设计的程序是一个个对象的集合,而面向过程语言设计的程序则是数据结构与算法的集合. 下面来分析的是dhcp server中的定义结构体: 1).在pa ...