【CF55D】Beautiful numbers(动态规划)

题面

洛谷

CF

题解

数位\(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(动态规划)的更多相关文章

  1. 洛谷 CF55D Beautiful numbers 解题报告

    CF55D Beautiful numbers 题意 \(t(\le 10)\)次询问区间\([l,r](1\le l\le r\le 9\times 10^{18})\)中能被每一位上数整除的数的个 ...

  2. [暑假集训--数位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 ...

  3. CF55D Beautiful numbers 题解

    题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...

  4. CF55D Beautiful numbers

    题目链接 题意 定义一个数字\(x\)是\(beautiful\ number\)当且仅当\(x\)可以被其十进制表示下所有非\(0\)位置的数整除. 例如\(24\)是一个\(beautiful\ ...

  5. cf55D. Beautiful numbers(数位dp)

    题意 题目链接 Sol 看到这种题就不难想到是数位dp了. 一个很显然的性质是一个数若能整除所有位数上的数,则一定能整除他们的lcm. 根据这个条件我们不难看出我们只需要记录每个数对所有数的lcm(也 ...

  6. CF55D Beautiful numbers (数位dp)

    题目链接 题解 一个数能被一些数整除,那么一定被这些数的\(lcm\)整除 那么我们容易想到根据\(lcm\)设状态 我们可以发现有用的\(lcm\)只有\(48\)个 那么按照一般的数位\(dp\) ...

  7. 【数位DP】CF55D Beautiful numbers

    $dp[x][p][pp]$表示第x位,当前已有数字mod 2520(1~9数字的lcm)为p,当前各位数字的lcm为pp 观察到数组太大,考虑压缩,第三维lcm最多只有9个数字,打表发现最多只有48 ...

  8. 【CF55D】Beautiful numbers

    [CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...

  9. CodeForces 55D Beautiful numbers

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

随机推荐

  1. git在windows7下面使用

    1. 首选安装. 2. 打开Git Bash 3. 输入,就是配置一下用户名啥的 $ git config --global user.name "Jack Liao" $ git ...

  2. 基于testng自动化添加allure报告展示以及jenkins集成

    本地执行方式: 1.下载地址 http://allure.qatools.ru/ 2.执行机器添加环境变量 如mac:vi /etc/profile export ALLURE_HOME=/Users ...

  3. linux信号处理相关知识

      因为要处理最近项目中碰上的多个子进程退出信号同时到达,导致程序不当产生core的情况,今天我花了时间看了一些关于linux信号处理的博客. 总结一下:(知识未经实践) linux信号分两种,一种实 ...

  4. appium -- 页面出现弹窗,关闭后,无法识别页面元素(转)

    原文:https://www.cnblogs.com/leavescy/p/9733001.html; 1. 问题:如图所示:在修改手势密码的过程中,点击了返回按钮后,弹出该弹窗:点击继续设置后,就发 ...

  5. Jmeter接口测试(九)授权

    下面应该是jmeter的授权设置,但是由于本人目前对这块了解还不深,暂时写个标题,以后有时间再来补充,大家可以先看下一篇内容

  6. CentOS 下 MySQL 5.6 基于 RPM 的下载、安装、配置

    CentOS 下 MySQL 5.6 基于 RPM 的下载.安装.配置 系统: CentOS 7 x86_64 MySQL 版本: 5.6.40 安装方式: RPM 下载 下载地址 操作系统 选择 R ...

  7. windows更改MySQL存储路径

    在C:\ProgramData\MySQL\MySQL Server 5.7文件夹 my.ini是默认的配置文件.在这里我们只更改数据存储路径.不更改配置文件 1 # Path to the data ...

  8. HP Vitrual Connect 配置快速参考

    使用任意浏览器,在地址栏输入VC的管理地址(如果不知道VC的管理地址请从OA中进入) 输入用户名和密码登入VC,验证成功后将进入VM的配置向导 点击"Next"继续,将先进行Dom ...

  9. 首次使用windows管理界面访问安装在UNIX或linux下的DP服务器时提示无权限访问的解决方法

    用windwos GUI管理界面连接时提示无权限访问: 在/etc/opt/omni/server/users/userlist 添加一行: "" "*" &q ...

  10. sqli-labs学习笔记 DAY3

    DAY 3 sqli-labs lesson 6 同lesson 5,只是把单引号改为双引号 sqli-labs lesson 7 同lesson 5,只是把单引号后面加两个空格,使用Burpsuit ...