codefroces 55D Beautiful numbers
[Description]
美丽数是指能被它的每一位非0的数字整除的正整数。
[Input]
包含若干组数据,每组数据一行两个数n,m,表示求[n,m]之间的美丽数的个数。
[output]
对于每组数据输出一个答案,各占一行。
1
1 9
9
1
12 15
2
[Hit]
0 < n , m < 10^18
测试数据不超过100组
基本思路是用:dp[len][mod][lcm]表示<=len的长度中,此数为mod,各数位的最小公倍数为lcm的数的个数来进行记忆化搜索。方法和上一题类似。
但我们发现,len在[1,20]范围内,mod在[1,1^18]范围内,lcm在[1,2520]范围内。所以dp数组肯定超内存。
下面我们来进行内存优化:
假设这个数为a,各个数位的值分别为ai,那么我们发现lcm(ai) | a.
而[1,9]的最小公倍数是2520.那么lcm(ai) | 2520, 所以lcm(ai) | (a%2520).
所以第二维大小我们可以从1^18降到2520,方法是%2520.
现在的dp数组的内存是20*2520*2520,还是很大。
然后我们再考虑:
我们发现某一个数的各个数位的数的最小公倍数最大是2520,而且只能是2520的公约数。而2520的公约数有48个。所以第三维我们只用[50]的空间就行了。
方法是用Hash进行离散化。‘
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lol;
int bit[],ha[],len,cnt;
lol f[][][],ans;
int gcd(int x,int y)
{
if (!y) return x;
return gcd(y,x%y);
}
int lcm(int x,int y)
{
return x*y/gcd(x,y);
}
void has(int x,int l)
{
if (x>) return;
if (ha[l]==)
ha[l]=++cnt;
has(x+,lcm(l,x));
has(x+,l);
}
lol dfs(int pos,int pre_num,int pre_lcm,bool flag)
{int nxt_num,nxt_lcm,r,i;
lol s=;
if (pos==)
return pre_num%pre_lcm==;
if (flag&&f[pos][pre_num][ha[pre_lcm]]!=-)
return f[pos][pre_num][ha[pre_lcm]];
if (flag) r=;
else r=bit[pos];
for (i=;i<=r;i++)
{
nxt_num=pre_num*+i;
nxt_num%=;
if (i)
nxt_lcm=lcm(pre_lcm,i);
else nxt_lcm=pre_lcm;
s+=dfs(pos-,nxt_num,nxt_lcm,flag||(i<r));
}
if (flag)
f[pos][pre_num][ha[pre_lcm]]=s;
return s;
}
lol solve(lol x)
{
len=;
while (x)
{
len++;
bit[len]=x%;
x/=;
}
return dfs(len,,,);
}
int main()
{lol l,r;
int T;
cin>>T;
has(,);
memset(f,-,sizeof(f));
while (T--)
{
scanf("%I64d%I64d",&l,&r);
ans=solve(r)-solve(l-);
printf("%I64d\n",ans);
}
}
codefroces 55D Beautiful numbers的更多相关文章
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- [Codeforces-div.1 55D] Beautiful numbers
[Codeforces-div.1 55D] Beautiful numbers 试题分析 还是离散化...\(f_{i,j,k}\)表示i位,gcd为j,余数为k. #include<iost ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- 【数位dp】CF 55D Beautiful numbers
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
随机推荐
- 结对开发五--对一千个数long型的一维数组求最大子数组的和
一.设计思想 我们根据第一个实验,再让他自动生成1000个随机long型数.大致思想和实验一一样,自己已埋入炸弹. 二.实验代码 package com.minirisoft; import java ...
- PTA題目的處理(三)
题目7-1 高速公路超速處罰 1.實驗代碼 #include <stdio.h> //#include <stdlib.h> int main() { int csp,lsp; ...
- 201621123057 《Java程序设计》第7周学习总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 答: ...
- 【原创】Webpack构建中hash的优化
背景: SPA的vue应用,采用webpack2构建,打包入口为main.js 输出:main模块打包成app.js,公共lib打包成vendor.js,公共样式打包成app.css,运行时依赖打包成 ...
- JAVA_SE基础——13.选择结构语句
if选择结构 语法: if(条件){ 代码块 } public class Test{ public static void main(String[] args){ int a = 5; if(a ...
- sublime使用攻略
一些常用的快捷键 Ctrl+Enter 在下一行插入新行.举个例子:即使光标不在行尾,也能快速向下插入一行. Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Ctrl+Shift ...
- LinuxMint18使用单独分区作为Home挂载点
安装LinuxMint时,我没有为/home选择单独的分区,所以在安装完系统之后就得手动配置一番. 首先是创建一个分区,这个很简单就不说啦. 然后就是把现有的/home下的文件移动到新的分区里面 操作 ...
- vue中简单的小插曲
我们现在来学习一下vue中一些简单的小东西: 首先我们必须要引入vue.js文件哦! 1.有关文本框里的checkbox js代码: new Vue({ el:"#app", da ...
- Struts(十八):通过CURD来学习PrepareInterceptor拦截器
PrepareInterceptor拦截器的用法: 1.若Action实现了Preparable接口,则Action方法需实现prepare()方法: 2.PrepareInterceptor拦截器S ...
- Havel-Hakimi定理---通过度数列判断是否可图化
0.可图:一个非负整数组成的序列如果是某个无向图的度序列,则该序列是可图的. 1.度序列:Sequence Degree,若把图G所有顶点的度数排成一个序列,责成该序列为图G的一个序列.该序列可以是非 ...