【CF55D】Beautiful numbers(动态规划)
【CF55D】Beautiful numbers(动态规划)
题面
题解
数位\(dp\)
如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除。
所以\(dp\)的时候前面所有数的\(lcm\)要压进\(dp\)值中。
又因为\(lcm\)的余数也是有意义的,但是又不能暴力记,
所以记录一下\([1,9]\)所有数的\(lcm\)也就是\(2520\)就好了。
但是数组太大,实际上,有意义的\(lcm\)个数只有不到\(50\)个,重新编号就可以压缩状态了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
inline ll read()
{
ll x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int lcm[3000],tot,p[50];
ll f[20][50][2520];
int W[20],top;
int LCM(int a,int b){if(b==0)return a;return a/__gcd(a,b)*b;}
ll dfs(int x,int Lcm,int r,int t)
{
if(!x)return r%p[Lcm]==0;
if(f[x][Lcm][r]!=-1&&!t)return f[x][Lcm][r];
int up=t?W[x]:9;ll ret=0;
for(int i=0;i<=up;++i)
ret+=dfs(x-1,lcm[LCM(p[Lcm],i)],(r*10+i)%2520,t&(i==W[x]));
if(!t)f[x][Lcm][r]=ret;
return ret;
}
ll Solve(ll x)
{
top=0;
while(x)W[++top]=x%10,x/=10;
return dfs(top,1,0,1);
}
int main()
{
for(int i=0;i<(1<<9);++i)
{
int sum=1;
for(int j=0;j<9;++j)
if(i&(1<<j))sum=LCM(sum,j+1);
lcm[sum]=1;
}
for(int i=1;i<=2520;++i)if(lcm[i])lcm[i]=++tot,p[tot]=i;
memset(f,-1,sizeof(f));int T=read();
while(T--)
{
ll l=read(),r=read();
cout<<Solve(r)-Solve(l-1)<<endl;
}
return 0;
}
【CF55D】Beautiful numbers(动态规划)的更多相关文章
- 洛谷 CF55D Beautiful numbers 解题报告
CF55D Beautiful numbers 题意 \(t(\le 10)\)次询问区间\([l,r](1\le l\le r\le 9\times 10^{18})\)中能被每一位上数整除的数的个 ...
- [暑假集训--数位dp]cf55D Beautiful numbers
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb ...
- CF55D Beautiful numbers 题解
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- CF55D Beautiful numbers
题目链接 题意 定义一个数字\(x\)是\(beautiful\ number\)当且仅当\(x\)可以被其十进制表示下所有非\(0\)位置的数整除. 例如\(24\)是一个\(beautiful\ ...
- cf55D. Beautiful numbers(数位dp)
题意 题目链接 Sol 看到这种题就不难想到是数位dp了. 一个很显然的性质是一个数若能整除所有位数上的数,则一定能整除他们的lcm. 根据这个条件我们不难看出我们只需要记录每个数对所有数的lcm(也 ...
- CF55D Beautiful numbers (数位dp)
题目链接 题解 一个数能被一些数整除,那么一定被这些数的\(lcm\)整除 那么我们容易想到根据\(lcm\)设状态 我们可以发现有用的\(lcm\)只有\(48\)个 那么按照一般的数位\(dp\) ...
- 【数位DP】CF55D Beautiful numbers
$dp[x][p][pp]$表示第x位,当前已有数字mod 2520(1~9数字的lcm)为p,当前各位数字的lcm为pp 观察到数组太大,考虑压缩,第三维lcm最多只有9个数字,打表发现最多只有48 ...
- 【CF55D】Beautiful numbers
[CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- Excel小技巧整理(持续更新)
合并某列中相同单元格 参考https://jingyan.baidu.com/article/9158e00006db70a25512286f.html 使用方法 先给需要合并的列排序,这样相同数据会 ...
- Windows下Mongo分片及集群
这里简单介绍一下windows下mongodb的分片设置和集群搭建,希望能够为迷茫的新手起到一点点作用.其实windows下与linux下思路是一致的,只是绑定时的ip,与端口号不同,linux下可以 ...
- DataRow的RowState属性变化
DataRow的RowState属性(状态)取值有5种:Detached, Unchanged, Added, Deleted, Modified. 当我们用DataRow newRow = Data ...
- 获取一个数组里面第K大的元素
如何在O(n)内获取一个数组比如{9, 1, 2, 8, 7, 3, 6, 4, 3, 5, 0, 9, 19, 39, 25, 34, 17, 24, 23, 34, 20}里面第K大的元素呢? 我 ...
- MYSQL存储过程调试过程
mysql不像oracle有plsqldevelper工具用来调试存储过程,所以有几种简单的方式追踪执行过程: 1.用一张临时表,记录调试过程: 2.直接在存储过程中,增加select xxx,在控 ...
- Pvmove中断后恢复LV状态
Pvmove中断后恢复LV状态 pvmove执行时关闭中断窗口后,pvmove进程会被强制杀掉,从而导致lv的状态异常,无法重新进行pvmove和其他lvm镜像增加相关操作,可以通过如下方式修复: ...
- 集合set、map、list
一.set 无序.可重复 public static void main(String[] args){ Set<String> set=new HashSet<String> ...
- os模块大全详情
python常用模块目录 一:os模块分类: python os.walk详解 二:os模块大全表 序号 方法 方法 1 os.access(path, mode) 检验权限模式 2 os.chdir ...
- 第六次ScrumMeeting博客
第六次ScrumMeeting博客 本次会议于10月31日(二)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 除了汇报任务外,窦鑫泽同学还就前 ...
- wireshark解析https协议方法
本文仅介绍通过协商密钥的方式实现https解析的方法 wireshark支持pem