Codeforces 55D. Beautiful numbers

题意

求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等)。

思路

一开始以为是数位DP的水题,觉得只需要记录搜到当前位出现了哪些数字作为状态即可,明显是假算法...感觉这是一道数位DP好题。可以这样思考:一个数要想被其各位数字分别都整除,等价于它被那些数字的LCM整除。因此记录当前位,当前数对(1~9的LCM)取模的结果,当前出现的数字的LCM这三个值作为状态才合理,即dp[pos][sum][lcm]。不过有一点需要注意,1-9的LCM的值会达到2520,直接存的空间复杂度是不对的,而1-9的数字任意组合的LCM明显没有那么多个,因此考虑离散化这些LCM,即可压缩空间。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<cmath>
#include<functional>
#include<string>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=5e3+10,mod=1e9+7,INF=0x3f3f3f3f;
int LCM,dig[30],id[maxn];
ll dp[30][maxn][100];
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll Lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}
void init()
{
int num=0;
LCM=1;
for (int i=1;i<=9;++i)
LCM=Lcm(LCM,i);
for (int i=1;i<=LCM;++i)
if (LCM%i==0)
id[i]=num++;
memset(dp,-1,sizeof(dp));
}
ll dfs(int pos, int sum, int lcm, int lit)
{
if (pos==-1)
return sum%lcm==0;
if (!lit&&dp[pos][sum][id[lcm]]!=-1)
return dp[pos][sum][id[lcm]];
int up=lit?dig[pos]:9;
ll cnt=0;
for (int i=0;i<=up;++i)
{
int ns=(sum*10+i)%LCM,nl=i==0?lcm:Lcm(lcm,i);
cnt+=dfs(pos-1,ns,nl,lit&&i==dig[pos]);
}
if (!lit)
dp[pos][sum][id[lcm]]=cnt;
return cnt;
}
ll count(ll n)
{
int pos=0;
while (n)
{
dig[pos++]=n%10;
n/=10;
}
return dfs(pos-1,0,1,1);
}
int main()
{
init();
int T;
cin>>T;
while (T--)
{
ll l,r;
cin>>l>>r;
cout<<count(r)-count(l-1)<<endl;
}
return 0;
}

Codeforces 55D. Beautiful numbers(数位DP,离散化)的更多相关文章

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

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

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

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

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

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

  4. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  5. codeforces 55D. Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

  6. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

  7. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

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

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

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. 怎样设置cookie生效的域名和路径

    Domain 属性指定浏览器发出HTTP请求时, 哪些域名需要附带这个Cookie. 比如 Domain 设置为 example.com, 那 abc.example.com 也会在发起请求时附带这个 ...

  2. 怎样获取xhr的当前状态

    一个XMLHttpRequets实例从创建到发起请求再到获取数据, 期间会有一些中间状态, 这些状态是必要的, 我们可以通过判断这些状态值来进行不同的动作. xhr中获取请求状态的属性是: xhr.r ...

  3. [转载]SSD原理与实现

    [转载]SSD原理与实现 这里只mark一下,对原论文讲解的很好的博文 https://zhuanlan.zhihu.com/p/33544892 这里有一个关于SSD的很好的程序实现,readme里 ...

  4. 使用Docker搭建MySQL服务

    一.安装docker windows 和 mac 版可以直接到官网下载 docker desktop linux 的安装方法可以参考 https://www.cnblogs.com/myzony/p/ ...

  5. cmd设置电脑自动关机

    cmd设置电脑自动关机 设置:(3600代表一小时,单位s) shutdown -s -t 3600 取消 shutdown -a

  6. requests模块发送数据

    通过json dumps发送 import requests import json def agent(): """ 执行命令采集硬件信息 将执行的信息发送给API : ...

  7. 5.移动端自动化测试-小知识 import和from...import的区别

    一.import   1 import导入的时,需要使用模块名的限定. 举个例子,我们首先创建一个md.py文件,里面有一个函数 2 然后在1.py文件中引用这个函数. 注意,我们需要使用md.的方式 ...

  8. shell脚本——sed命令

    sed 命令 作为行编辑器,对文件进行编辑(以行为单位进行编辑) sed编辑文件,却不改变原文件 sed工作原理: 指定一个文本文件,依次读取文本文件中的每行内容,读取到模式空间中,在模式空间中进行匹 ...

  9. 【Day3】3.提取商城分类结构

    import re with open('index.html','r',encoding='utf-8') as f: html = re.sub('\n','',f.read()) section ...

  10. Oracle笔记(十二) 集合、序列

    一.集合 在数学的操作之中存在交.差.并.补的概念,而在数据的查询中也存在此概念,有如下几个连接符号: UNION:连接两个查询,相同的部分不显示: UNION ALL:连接两个查询,相同的部分显示: ...