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是 ...
随机推荐
- 一步一步写News App(一)
一. 新建一个安卓工程,安卓版本全部选2.3.3 二.第一步,添加一个tabhost控件 在MainActivity中声明TabHost tabHost; 然后新建一个private void ini ...
- 同步/异步/阻塞/非阻塞/BIO/NIO/AIO
转摘自:https://www.cnblogs.com/lixinjie/p/a-post-about-io-clearly.html 常规的误区 假设有一个展示用户详情的需求,分两步,先调用一个HT ...
- Redis 测试 数据类型
- React/事件系统
React基于虚拟DOM实现了一个合成事件层,我们所定义的事件处理器会接收到一个合成事件对象的实例事件处理. 并且所有事件都自动绑定在最外层上.如果需要访问原生事件对象,可以使用nativeEvent ...
- webpack最基本的使用方式
1.创建文件夹"webpack-study" 2.使用webstrom打开文件夹所在位置.在根目录上创建一个文件,文件命名为“src”,在src文件下新建css.images.js ...
- 8.Redis的复制(Master/Slave)
Redis的复制(Master/Slave) a)是什么 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave ...
- redis slot 槽点
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求 ...
- ubuntu16.04安装zabbix-server3.4
一.安装前环境准备 部署zabbix需要安装apache,mysql和php sudo apt-get install apache2 sudo apt-get install mysql-serve ...
- python自定义小工具:密码匿名化、毫秒时间显示、人类易读字节
import base64 import time def timestamp2datems(timestamp): ''' 时间戳转为日期字串,精确到ms.单位s :param timestamp: ...
- 一组简单好看的css3渐变按钮
主要代码如下: body { background:#fff } /* Mixins */ /* bg shortcodes */ .bg-gradient1 span,.bg-gradient1:b ...