【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. 读懂UML类图

    平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...

  2. 【MySQL函数】replace()函数

    1.replace()函数 语法:replace(a,b,c) a的b部分被c替换 案例:select replace(abcde,bcd,123)-- 结果a123e

  3. JS 判断checkbox 是否选中

    <input type="checkbox" id="IsEnable" /> 在调试的时候,会出现,一直未true的状态,不管是选中还是未选中 解 ...

  4. MAC清理DS_Store和._文件

    打开终端输入 find . -name .DS_Store -type f -delete ; find . -type d | xargs dot_clean

  5. CSP201403-3:命令行选项

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  6. ovs源码阅读--流表查询原理

    背景 在ovs交换机中,报文的处理流程可以划分为一下三个步骤:协议解析,表项查找和动作执行,其中最耗时的步骤在于表项查找,往往一个流表中有数目巨大的表项,如何根据数据报文的信息快速的查找到对应的流表项 ...

  7. [redis] linux下安装篇(1)

    一.redis是什么redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有 ...

  8. Longge's problem(欧拉函数应用)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  9. Play on Words(欧拉回路)

    Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologi ...

  10. 对石家庄铁道大学网站UI的分析

         作为我们团队的PM,老师对我们提出了一些额外的要求,所以我发表这篇博客来谈一下对石家庄铁道大学网站UI的分析.      首先,PM 对项目所有功能的把握, 特别是UI.最差的UI, 体现了 ...