CF55D Beautiful numbers (数位dp)
题解
一个数能被一些数整除,那么一定被这些数的\(lcm\)整除
那么我们容易想到根据\(lcm\)设状态
我们可以发现有用的\(lcm\)只有\(48\)个
那么按照一般的数位\(dp\)
设出状态:\(f_{i,j,k,0/1}\)表示前\(i\)位,\(lcm=j\),模\(lcm\)的余数是\(k\),是否达到上界
但是这样子是无法转移的(因为新添加一个数模数可能会产生变化)
那么我们把模数统一成\(2520\)
复杂度\(O(T*L*48*2500*2)\)
其中\(L\)是输入数的位数
然后就会\(TLE\)
考虑稍微优化一下。
因为是多组询问。
如果之前已经询问过一个很大的数了,
这一次询问其实有很多答案我们已经知道了。
我们可以去掉状态的\(01\)那一维。
这样复杂度就可以去掉那个询问组数了。
具体看第二个代码。
Code
int gcd(int x, int y) {
return !y ? x : gcd(y, x % y);
}
int lcm(int x, int y) {
if (!x || !y) return x | y;
return x * y / gcd(x, y);
}
LL dfs(int x, int n, int m, int op) {
if (!x) return !num[n] || m % num[n] == 0;
if (f[x][n][m][op] != -1) return f[x][n][m][op];
LL &res = f[x][n][m][op]; res = 0;
for (int i = 0; i <= (op ? a[x] : 9); i++)
res += dfs(x - 1, M[lcm(num[n], i)], (m * 10 + i) % 2520, op & i == a[x]);
return res;
}
LL calc(LL x) {
if (!x) return 1;
l = 0;
while (x) a[++l] = x % 10, x /= 10;
for (int i = 1; i <= l; i++)
memset(f[i], -1, sizeof(f[i]));
return dfs(l, 0, 0, 1);
}
void solve() {
LL l = gi<LL>(), r = gi<LL>();
printf("%lld\n", calc(r) - calc(l - 1));
return ;
}
int main() {
for (int i = 1; i < (1 << 9); i++) {
int d = 0;
for (int j = 0; j < 9; j++)
if (i >> j & 1)
d = lcm(d, j + 1);
if (!M[d]) num[++cnt] = d, M[d] = cnt;
}
int T = gi<int>();
while (T--) solve();
return 0;
}
LL dfs(int x, int n, int m, int op) {
if (!x) return !num[n] || m % num[n] == 0;
if (!op && f[x][n][m] != -1) return f[x][n][m];
LL res = 0;
for (int i = 0; i <= (op ? a[x] : 9); i++)
res += dfs(x - 1, M[lcm(num[n], i)], (m * 10 + i) % 2520, op & i == a[x]);
if (!op) f[x][n][m] = res;
return res;
}
CF55D Beautiful numbers (数位dp)的更多相关文章
- cf55D. Beautiful numbers(数位dp)
题意 题目链接 Sol 看到这种题就不难想到是数位dp了. 一个很显然的性质是一个数若能整除所有位数上的数,则一定能整除他们的lcm. 根据这个条件我们不难看出我们只需要记录每个数对所有数的lcm(也 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
随机推荐
- (转)从0移植uboot(三) _编译最小可用uboot
ref: https://www.cnblogs.com/xiaojiang1025/p/6436752.html 前两篇介绍了uboot-2013.01的配置原理以及大体的运行流程,本文将讨论如何对 ...
- ActiveMQ 简单应用
ActiveMQ简单应用到复杂的订单模块,提高前台的访问速度. 一.当提交订单后,发送消息给ActiveMQ. @Service public class JmsSend { private stat ...
- VS2013+WDK8.1 驱动开发环境配置
Windows Driver Kit 是一种完全集成的驱动程序开发工具包,它包含 WinDDK 用于测试 Windows 驱动器的可靠性和稳定性,本次实验使用的是 WDK8.1 驱动开发工具包,该工具 ...
- gRPC Golang/Python使用
gRPC Golang/Python使用 以前开发网站都是用http协议,学过TCP/IP协议的人都知道,在传输层TCP的基础上,应用层HTTP就是填充了一定规则的文本. 1.gRPC使用和介绍 工作 ...
- Deep Learning方向的paper
转载 http://hi.baidu.com/chb_seaok/item/6307c0d0363170e73cc2cb65 个人阅读的Deep Learning方向的paper整理,分了几部分吧,但 ...
- SQL 语句使用关键字错误
异常为SQL Error: 1064, SQLState: 42000 时 ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIG ...
- CVE-2019-0214: Apache Archiva arbitrary file write and delete on the server
CVE-2019-0214: Apache Archiva arbitrary file write and delete on the server Severity: Medium Vendor: ...
- vulnhub攻略
https://www.vulnhub.com/entry/21ltr-scene-1,3/ 这个靶机国内https://www.cnblogs.com/hack404/p/11423228.html ...
- Mysql的管理
Linux系统中:mysql进入的命令为mysql -u root -p +你的mysql密码. Mysql是如何添加用户呢? 在mysql命令行下,使用use mysql;进入mysql的数据库中. ...
- 使用fastjson统一序列化响应格式
摘要:其实很简单的处理方式,只不够优雅,或者说没有找到fastjson为其提供便捷的处理方式.因此记录下处理该问题的过程. 目标:将所有api请求,即响应为APPLICATION_JSON的内容做统一 ...