[Description]

美丽数是指能被它的每一位非0的数字整除的正整数。

[Input]

包含若干组数据,每组数据一行两个数n,m,表示求[n,m]之间的美丽数的个数。

[output]

对于每组数据输出一个答案,各占一行。

Input
1
1 9
Output
9
Input
1
12 15
Output
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的更多相关文章

  1. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  2. [Codeforces-div.1 55D] Beautiful numbers

    [Codeforces-div.1 55D] Beautiful numbers 试题分析 还是离散化...\(f_{i,j,k}\)表示i位,gcd为j,余数为k. #include<iost ...

  3. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  4. CodeForces 55D Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  6. CF 55D - Beautiful numbers(数位DP)

    题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...

  7. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  8. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  9. 【数位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 ...

随机推荐

  1. W班-项目选题报告成绩

    作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/907 作业要求 1份团队选题报告(word电 ...

  2. 凡事预则立(Beta)

    听说--凡事预则立 吸取之前alpha冲刺的经验教训,也为了这次的beta冲刺可以更好更顺利地进行,更是为了迎接我们的新成员玮诗.我们开了一次组内会议,进行beta冲刺的规划. 上一张我们的合照: 具 ...

  3. Beta第二天

    听说

  4. Spring Boot jar包linux服务器部署

    Spring Boot 部署 一.使用命令行java -jar 常驻 nohup java -jar spring-boot-1.0-SNAPSHOT.jar > log.file 2>& ...

  5. windows系统下安装 node.js (node.js安装及环境配置)

    node.js简介 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node. ...

  6. Angular.js 1++快速上手

    AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Goole所收购.是一款优秀的前端JS框架.AngularJS有着诸多特性,最为核心的是:MVC,撗块化,自动化双向数据绑 ...

  7. Node入门教程(4)第三章:第一个 Nodejs 程序

    第一个 Nodejs 程序 本教程仅适合您已经有一定的JS编程的基础或者是后端语言开发的基础.如果您是零基础,建议您先学一下老马的前端免费视频教程 第一步:创建项目文件夹 首先创建 demos 文件夹 ...

  8. 第一次制作和使用图标字体-IcoMoon

    开题:之前就有所耳闻,最近两天第一次运用到图标字体.刚开始嘛,一脸懵逼的状态.成功运用之后就来记录一下使用过程咯! 1. 打开在线生成工具:https://icomoon.io/app/#/selec ...

  9. Centos7安装openvpn及客户端配置

    1.openvpn介绍 VPN直译就是虚拟专用通道,是提供给企业之间或者个人与公司之间安全数据传输的隧道,使用OpenSSL加密库中的SSLv3/TLSv1协议函数库. 目前OpenVPN能在Sola ...

  10. 快速获取表单多条数据,使用ajax传递给后台

    当表单中有多条数据需要向后台传递时,一个一个的获取显然是不可取的办法,可以借助表单的serialize()方法获取. HTML: <form id="form"> &l ...