Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117
题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除。范围2^64。
解题思路:
一开始SB地开了10维数组记录情况。
首先要求能被各位上的数整除,可以转化为被一个数整除问题。
这个数就是各位上数的最小公倍数LCM(不是GCD)。
其次,处理整除问题,得转化成数位DP的余数模板。1~9的LCM最大是2520, 那么%2520,让其可以开数组进行记忆化搜索。
最后, 对于不能%2520最后结果,再%各个数位累计过来的LCM。
这样下来,需要开20*2520*2520的数组,往CF上一交你会发现MLE。
仔细观察每次的LCM,其范围是1~2520没错,但是都是整除gcd的结果(LCM=a*b/gcd(a,b) ),也就是说所有LCM都是某个数的约数。
这个数其实就是2520。所以DP之前,为2520打个表,把LCM给离散化Hash。这样其实只有48个LCM了。数组开20*2520*50即可。
注意结果是int64。
#include "cstdio"
#include "cstring"
using namespace std;
#define LL long long
LL dp[][][],digit[],Hash[];
int gcd(int a,int b) {return b==?a:gcd(b,a%b);}
int lcm(int a,int b) {return a*b/gcd(a,b);}
LL dfs(int len,int Remain,int Lcm,bool fp)
{
if(!len) return Remain%Lcm?:;
printf("%d\n",Lcm);
if(!fp&&dp[len][Remain][Hash[Lcm]]!=-) return dp[len][Remain][Hash[Lcm]];
LL ret=,fpmax=fp?digit[len]:;
for(int i=;i<=fpmax;i++)
ret+=dfs(len-,(Remain*+i)%,i==?Lcm:lcm(Lcm,i),fp&&i==fpmax);
if(!fp) dp[len][Remain][Hash[Lcm]]=ret;
return ret;
}
LL f(long long x)
{
int len=;
while(x)
{
digit[++len]=x%;
x/=;
}
return dfs(len,,,true);
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
LL l,r,cnt=;
scanf("%d",&T);
memset(dp,-,sizeof(dp));
for(int i=;i*i<=;i++)
{
if(%i==)
{
Hash[i]=cnt++;
if(i*i!=) Hash[/i]=cnt++;
}
}
while(T--)
{
scanf("%I64d%I64d",&l,&r);
LL res=f(r)-f(l-);
printf("%I64d\n",res);
}
}
2908091(#) | neopenx | CodeForces | 55D | Accepted | 19800 | 780 | GNU C++ 4.6 | 1121 |
2014-10-30 19:41:38
|
Codeforces 55D (数位DP+离散化+数论)的更多相关文章
- codeforces 55D 数位dp
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 628D 数位dp
题意:d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d 题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少 ...
- codeforces 401D (数位DP)
思路:很明显的数位dp,设dp[i][j] 表示选取数字的状态为i,模m等于j的数的个数,那么最后的答案就是dp[(1<<n)-1][0].状态转移方程就是,dp[i|(1<< ...
- Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)
大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...
- Codeforces - 914C 数位DP
题意有点难以描述,简略的就是给定一个二进制\(n\),每一步操作能使\(n\)的位为1的数的和转化为一个十进制,然后转化为该数的二进制再进行相同的操作 查询\([0,n]\)中操作数恰好为\(k\)的 ...
- Codeforces #55D (数位dp+离散化)
Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive ...
- Codeforces 13C Sequence --DP+离散化
题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为 ...
随机推荐
- [另开新坑] 算导v3 #26 最大流 翻译
26 最大流 就像我们可以对一个路网构建一个有向图求最短路一样,我们也可以将一个有向图看成是一个"流量网络(flow network)",用它来回答关于流的问题. Just as ...
- 重新编译安装gcc-4.1.2(gcc版本降级)之TFS安装
wget http://gcc.parentingamerica.com/releases/gcc-4.1.2/gcc-4.1.2.tar.gz tar -zxfv gcc-4.1.2.tar.gz ...
- vim技巧之快速进入引号删除至右引号前的内容
参考:http://blog.chinaunix.net/uid-23381466-id-88482.html f'l ct' #fX,X可用任何字符,l表示向右移一位,ct'表示删除至引号前di'
- HDU 1521 排列组合 指数型母函数
排列组合 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- 解决Unable to reach a settlement: [diffie-hellman-group1-sha1, diffie-hellman-group-exchange-sha1] and [curve25519-sha256@li
SharpSSH或JSCH使用diffie-hellman-group1-sha1和diffie-hellman-group-exchange-sha1密钥交换算法,而OpenSSH在6.7p1版本之 ...
- jQuery常规选择器
//简单选择器$('div').css('color','red'); //元素选择器,返回多个元素$('#box').css('color','red');//id选择器,返回单个元素$('.box ...
- Kafka学习笔记(二):Partition分发策略
kafka版本0.8.2.1 Java客户端版本0.9.0.0 为了更好的实现负载均衡和消息的顺序性,Kafka Producer可以通过分发策略发送给指定的Partition.Kafka保证在par ...
- ASP.NET之AreaRegistration
ASP.NETMVC的AreaRegistration是用来干什么呢? 它是用来提供一个很好的接口让我们可以将Controller定义在其他的Library项目中,这主要可以用来解决模块化开发:一般情 ...
- laravel数据库查询是use方法的使用
){ return $query->where('effectivetime','<',date('Y-m-d')); }else{ ...
- Html页面插入flash代码
转自:http://www.educity.cn/jianzhan/402117.html 转自:http://www.cnblogs.com/yxc_fj/articles/1390621.html ...