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 ...
随机推荐
- HIVE使用mysql作为外置数据库配置详情
Hive安装配置(mysql) 安装mysql hadoop$:sudo apt-get update hadoop$:sudo apt-get install mysql-server 启动mysq ...
- beta冲刺1-咸鱼
前言:这篇算是开始补之前的开端,毕竟beta阶段我们从前面开始就有在陆续做了. 今天的工作: 接收了新成员*1,然后几个人聚了一下,并且讨论了一下目前遇到的问题,以及目前需要处理的问题. 目前遇到的问 ...
- nyoj 非洲小孩
非洲小孩 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 家住非洲的小孩,都很黑.为什么呢?第一,他们地处热带,太阳辐射严重.第二,他们不经常洗澡.(常年缺水,怎么洗 ...
- 05-移动端开发教程-CSS3兼容处理
CSS3的标准并没有全部定稿,目前CSS3的标准分成了不同的模块,具体的标准由各个模块推动标准和定稿,标准制定的过程中,浏览器也在不断的发新的版本来兼容新的标准.浏览器有时会给一些在试验阶段或非标准阶 ...
- JAVA_SE基础——8.基本数据类型
基本数据类型有:整数类型.浮点类型.字符类型.布尔类型 整数类型 整数类型用来存储整数数值,即没有小数部分的数值.与C.C++语言相同,整数在Java语言中有3种表示形式:十进制.八进制和十六进制. ...
- New UWP Community Toolkit - DeveloperTools
概述 UWP Community Toolkit 中有一个开发者工具集 DeveloperTools,可以帮助开发者在开发过程中进行 UI 和功能的调试,本篇我们结合代码详细讲解 Develope ...
- Mybatis和Hibernate本质区别和应用场景
Hibernate:是一个标准ORM(对象关系映射)框架.入门门槛较高,不需要程序员写sql语句,sql语句自动生成,对sql语句优化.修改比较困难 应用场景:适用于需求变化不多的中小型项目,比如后台 ...
- Spring Security入门(3-7)Spring Security处理页面的ajax请求
- ssh整合之七注解结合xml形式
1.我们之前的纯xml的方式,我们的配置文件很多,我们可以使用注解结合xml的方式进行开发,这样的话,我们的配置文件,会少很多,同时,我们可以直接在类中看到配置,这样,我们就可以快速地搭建一个ssh整 ...
- centos7 yum相关的常用命令
[root@mini1 ~]# history |grep yum 40 yum repolist 42 cd /etc/yum.repos.d/ 49 yum clean all 50 yum re ...