【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. MAVEN相关文章

    Maven入门指南① :Maven 快速入门及简单使用 Maven入门指南② :Maven 常用命令,手动创建第一个 Maven 项目 Maven入门指南③:坐标和依赖 Maven入门指南④:仓库 M ...

  2. Linux 内核3.10.5 专场

    今天本人十分靠谱地下载了linux 内核的3.10.5版本,这个版本是最新的稳定版. 听路飞大虾(哪个路飞?就是那个戴草帽的橡胶小伙,航海很多时候都很空闲的,于是最近他也开始研读linux 内核了.) ...

  3. 使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍

    使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍 Photon PUN Unity 网络游戏开发 Photon常用类介绍: IPunCallback PUNGIPunCa ...

  4. Siki_Unity_3-8_Lua编程(未完)

    Unity 3-8 Lua编程 任务1&2&3:前言 课程内容: Lua从入门到掌握 为之后的xLua和其他热更新方案打下基础 任务4:Lua简介 Lua是轻量小巧的脚本语言--无需编 ...

  5. POJ 1417 并查集 dp

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  6. tikv 安装

    export HostIP="127.0.0.1" docker run -d -p 2379:2379 -p 2380:2380 --name pd pingcap/pd \ - ...

  7. SQL行列轉換方法(詳細例子)

    普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法. 问题:假设有张学生成绩表(tb)如下 ...

  8. [leetcode-908-Smallest Range I]

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  9. Table Tennis Game 2(找规律)

    Description Misha and Vanya have played several table tennis sets. Each set consists of several serv ...

  10. 2018-2019-20172321 《Java软件结构与数据结构》第五周学习总结

    2018-2019-20172321 <Java软件结构与数据结构>第五周学习总结 教材学习内容总结 第9章 排序与查找 9.1查找 查找是这样一个过程,即在某个项目组中寻找某一指定目标元 ...