CF55D Beautiful numbers 题解
题目
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
Volodya是个奇怪的男孩,他的品味也很奇怪。 在他看来,当且仅当正整数可以被其每个非零数字整除时,它才是Beautiful的。 我们不会对此争论,而只计算给定范围内的 Beautiful number的数量。
输入格式
The first line of the input contains the number of cases $ t (1 \le t \le 10)$. Each of the next t lines contains two natural numbers \(l_i\) and \(r_i (1 \le li \le ri \le 9 \times 10^{18})\).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
输入的第一行包含数据组数\(t(1 \le t \le 10)\), 接下来的t行中的每行包含两个自然数$l_i \(和\)r_i(1 \le l_i \le r_i \le 9 \times 10 ^ {18})$.
请不要使用%lld在C ++中读取或写入64位整数。 建议使用cin(也可以使用%l64d).
输出格式
Output should contain tt numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from \(l_{i}\) to \(r_i\), inclusively).
t行,每行输出从\(l_{i}\) 到 \(r_i\)的Beautiful number的数量
题解
这道题是数位DP我好像没学过啊,而且还是黑题...最开始真是一点思路都没有,后来看了dalao们的题解才能写出来.
这道题让求一段范围内的,比较容易想到的就是差分,求出\(1\)到\(r\)的数量,再求出\(1\)到\(l-1\)的数量,相减即可.
注意每位数都是\(1-9\),他们的最小公倍数是\(2520\),所以如果一个数可以整除\(2520\),就一定可以整除\(1-9\).
设\(dp[i][j][k]\),\(i\)表示还剩下多少位数要规划,这前\(i\)位数模\(2520\)的余数是\(j\),这前\(i\)位数的最小公倍数是\(k\).
如果最后的数能整除\(k\),那么它一定能整除它各位数字的最小公倍数
有可能出现的\(k\)都是\(2520\)的因数,所以将\(j\)取模\(2520\),若所得的结果整除\(k\),那么原来的数也一定整除\(k\).
所以就可以得到状态转移方程
\(dp[i][j][k]= \Sigma_{x=1}^{x_{max}} \ \ \ \ dp[i−1][(j \times 10+x) \% 2520][lcm(k,x)]\)
\(lcm(a,b)\)表示\(a\)和\(b\)的最小公倍数
但是如果开这么大的数组,会MLE,所以需要继续优化,注意第三维是\(lcm(k,x)\),是从\(1-9\)之间取数字做\(lcm\)运算,得到的结果数量远远少于\(2520\),所以与处理一下做一个离散化即可.
希望有一天能自己独立写出黑题.
代码
#include <bits/stdc++.h>
using namespace std;
int number[20], mp[2521], cnt;
long long dp[20][2521][50],l, r, t;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
long long dfs(int length, int pre, int mod, bool limit ,long long ans = 0) {
if (!length) return pre % mod == 0;
if (!limit && dp[length][pre][mp[mod]] != -1) return dp[length][pre][mp[mod]];
int ed = limit ? number[length] : 9;
for (int i = 0; i <= ed; i++)
ans += dfs(length - 1, (pre * 10 + i) % 2520, i == 0 ? mod : mod * i / gcd(mod, i), limit && i == ed);
if (!limit) dp[length][pre][mp[mod]] = ans;
return ans;
}
long long solve(long long n, int length = 0) {
while (n) number[++length] = n % 10, n /= 10;
return dfs(length, 0, 1, 1);
}
int main() {
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= 2520; i++)
if (!(2520 % i)) mp[i] = ++cnt;
cin >> t;
while (t--) {
cin >> l >> r;
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
题目链接 题意 定义一个数字\(x\)是\(beautiful\ number\)当且仅当\(x\)可以被其十进制表示下所有非\(0\)位置的数整除. 例如\(24\)是一个\(beautiful\ ...
- CF55D Beautiful numbers (数位dp)
题目链接 题解 一个数能被一些数整除,那么一定被这些数的\(lcm\)整除 那么我们容易想到根据\(lcm\)设状态 我们可以发现有用的\(lcm\)只有\(48\)个 那么按照一般的数位\(dp\) ...
- CF1265B Beautiful Numbers 题解
Content 给定一个 \(1\sim n\) 的排列,请求出对于 \(1\leqslant m\leqslant n\),是否存在一个区间满足这个区间是一个 \(1\sim m\) 的排列. 数据 ...
- cf55D. Beautiful numbers(数位dp)
题意 题目链接 Sol 看到这种题就不难想到是数位dp了. 一个很显然的性质是一个数若能整除所有位数上的数,则一定能整除他们的lcm. 根据这个条件我们不难看出我们只需要记录每个数对所有数的lcm(也 ...
- 【数位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(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...
- 【CF55D】Beautiful numbers
[CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...
随机推荐
- Linux权限管理命令chown、chgrp、umask详解
命令chown详解 命令chown,所在路径为: 可以看到,这个命令的路径为:/usr/bin/chown ,所以它的执行权限是所有用户 命令的基本功能是改变文件或目录的所有者(只有root可以进行, ...
- CentOS8.1中搭建Gitlab服务器
依旧是写在前面的话♠:很多IT人从业N年也许都还没有亲自搭过一次Gitlab服务器,是不是?有木有?!通常都是背着自己的笔记电脑到一家公司入职,或入职后领到公司分配的电脑,然后分配了Git账号,拿了将 ...
- 头条面试居然跟我扯了半小时的Semaphore
一个长头发.穿着清爽的小姐姐,拿着一个崭新的Mac笔记本向我走来,看着来势汹汹,我心想着肯定是技术大佬吧!但是我也是一个才华横溢的人,稳住我们能赢. 面试官:看你简历上有写熟悉并发编程,Semapho ...
- jsp页面用DBHelper实现简单的登陆验证
首先我们需要写一个简单的登陆页面login.jsp,然后用from表单提交给index.jsp页面.在index.jsp页面通过DBHelper连接数据库判断账号和密码,如果密码正确则显示登陆成功. ...
- 网络KPI异常检测之时序分解算法
时间序列数据伴随着我们的生活和工作.从牙牙学语时的“1, 2, 3, 4, 5, ……”到房价的走势变化,从金融领域的刷卡记录到运维领域的核心网性能指标.时间序列中的规律能加深我们对事物和场景的认识, ...
- cubic-bezier() 函数
2020-03-13 cubic-bezier() 函数 ubic-bezier() 函数定义了一个贝塞尔曲线(Cubic Bezier). 贝塞尔曲线曲线由四个点 P0,P1,P2 和 P3 定义. ...
- 在kubernetes中搭建harbor,并利用MinIO对象存储保存镜像文件
前言:此文档是用来在线下环境harbor利用MinIO做镜像存储的,至于那些说OSS不香吗?或者单机harbor的,不用看了.此文档对你没啥用,如果是采用单机的harbor连接集群MinIO,请看我的 ...
- PyQt5中QTableView函数讲解
如果想熟悉QTableWidget,请参考PyQt5高级界面控件之QTableWidget(四) setSpan(int, int, int, int)四个参数分别代表,起始行,列,合并的行数,全并的 ...
- 解决GITLAB无法启动runsv no running
可以先尝试用 systemctl start gitlab-runsvdir.service 如果冻结不动的话,就需要查看系统服务项了 systemctl -t target 如果系统正常的话,所有任 ...
- postman发送json数据
原文链接:https://blog.csdn.net/weixin_33387378/article/details/90721599 1.设置header Content-Type appli ...