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是 ...
随机推荐
- IDEA GIT 忽略文件 最佳方式
前言 转载一篇博客,简单,实用. 原文地址:intellij idea 忽略文件不提交 ps:下面均为转载博客的内容: 在intellij中忽略提交文件,分两种情况, 文件没有纳入版本管理 第一种,文 ...
- javascript 垃圾回收机制和内存管理
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 垃圾回收机制的原理是找到不再被使用的变量,然后释放其占用的内存,但这个过程不是时时的,因为其开销比较大,所 ...
- (八)Struts标签基础(一)
一.Struts标签分类 二.标签的使用 2.1 标签的主题 主题的设置与struts.xml中的常量<constant name="struts.ui.theme" val ...
- LeetCode 1047. Remove All Adjacent Duplicates In String
1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...
- 基于【 centos7】四 || FastDFS集群+Nginx负载均衡
1. 架构设计 1.1 架构图 FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用F ...
- css之弹性盒模型
弹性盒子(Flexible Box/filebox)是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式.引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子 ...
- Layui 实现input 输入和选择
<div class="layui-col-md4"> <label class="layui-form-label">移交单位< ...
- hive 存储格式对比
Apache Hive支持Apache Hadoop中使用的几种熟悉的文件格式,如TextFile,RCFile,SequenceFile,AVRO,ORC和Parquet格式. Cloudera I ...
- 将用户赋予sudo权限:配置sudoers文件
xxx is not in the sudoers file.This incident will be reported.的解决方法 1.切换到root用户下,怎么切换就不用说了吧,不会的自己百 ...
- css多行超出时,超出高度,显示省略号
.layout display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden;