cf55D. Beautiful numbers(数位dp)
题意
Sol
看到这种题就不难想到是数位dp了。
一个很显然的性质是一个数若能整除所有位数上的数,则一定能整除他们的lcm。
根据这个条件我们不难看出我们只需要记录每个数对所有数的lcm(也就是2520)取模的结果
那么\(f[i][j][k]\)表示还有\(i\)个数要决策,之前的数模\(2520\)为\(j\),之前的数的lcm为k的方案
第三维可以预处理
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10;
const double eps = 1e-9;
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int f[21][2533][233], lim[MAXN], l, r, tot;
map<int, int> id;
void Get(int p) {
tot = 0;
while(p) lim[++tot] = p % 10, p /= 10;
}
int gcd(int a, int b) {
return !b ? a : gcd(b, a % b);
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
int dfs(int x, int sum, int lm, int opt) {//剩下i位需要决策,之前的数在%2520的意义下,数位上的数的lcm为lm的方案书, 是否顶着上界
if((!opt) && (~f[x][sum][id[lm]])) return f[x][sum][id[lm]];
if(!x) return sum % lm ? 0 : 1;
int res = 0;
for(int i = 0; i <= (opt ? lim[x] : 9); i++) {
int nxt = dfs(x - 1, (sum * 10 + i) % 2520, i == 0 ? lm : lcm(lm, i), opt && (i == lim[x]));
res += nxt;
}
if(!opt) f[x][sum][id[lm]] = res;
return res;
}
int calc(int x) {
Get(x);
return dfs(tot, 0, 1, 1);
}
void solve() {
l = read(); r = read();
cout << calc(r) - calc(l - 1) << '\n';
}
signed main() {
for(int i = 1, cnt = 0; i <= 2520; i++)
if(!(2520 % i))
id[i] = ++cnt;
memset(f, -1, sizeof(f));
for(int T = read(); T--; solve());
return 0;
}
cf55D. Beautiful numbers(数位dp)的更多相关文章
- CF55D Beautiful numbers (数位dp)
题目链接 题解 一个数能被一些数整除,那么一定被这些数的\(lcm\)整除 那么我们容易想到根据\(lcm\)设状态 我们可以发现有用的\(lcm\)只有\(48\)个 那么按照一般的数位\(dp\) ...
- 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是 ...
随机推荐
- 软件测试人员需要掌握的linux命令(二)
2 设备管理 2.1 mount 名称 : mount 使用权限 : 系统管理者或/etc/fstab中允许的使用者 使用方式 : mount [-hV] mount [-fnrsvw] [-t vf ...
- webpack vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
当我们出现以下报错! 解决方案: // webpack配置文件 const path = require('path'); const htmlWebpackPlugin = require('htm ...
- Vue -- 基础语法和使用
Vue 渐进式 JavaScript 框架 通过对框架的了解与运用程度,来决定其在整个项目中的应用范围,最终可以独立以框架方式完成整个web前端项目 一.走进Vue 1.what -- 什么是Vue ...
- Nacos发布0.5.0版本,轻松玩转动态 DNS 服务
阿里巴巴微服务开源项目Nacos于近期发布v0.5.0版本,该版本主要包括了DNS-basedService Discovery,对Java 11的支持,持续优化Nacos产品用户体验,更深度的与Sp ...
- Java中的Interrupt使用
初心 用interrupt中断程序 初步实现 public class InterruptionInJava implements Runnable{ @Override public void ru ...
- 值得关注的10个python语言博客
大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建立这个博客的原因,向大家分享我自己学到的新知识.今天我向大家推荐10个值得我们关注 ...
- Go基础系列:空接口
空接口 空接口是指没有定义任何接口方法的接口.没有定义任何接口方法,意味着Go中的任意对象都可以实现空接口(因为没方法需要实现),任意对象都可以保存到空接口实例变量中. 空接口的定义方式: type ...
- 实战!基于lamp安装Discuz论坛-技术流ken
简介 我前面的博客已经详细介绍了lamp采用yum安装以及编译安装的方式,这篇博客将基于yum安装的lamp架构来实战安装Discuz论坛,你可以任选其一来完成. 系统环境 centos7.5 服务器 ...
- python模块之shutil
shutil是一个用于简化文件操作的模块. 复制文件(传入源文件对象和目标文件对象) import shutil f1 = open(r'/Users/jingxing/PycharmProjects ...
- Keras入门(二)模型的保存、读取及加载
本文将会介绍如何利用Keras来实现模型的保存.读取以及加载. 本文使用的模型为解决IRIS数据集的多分类问题而设计的深度神经网络(DNN)模型,模型的结构示意图如下: 具体的模型参数可以参考文章 ...