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是 ...
 
随机推荐
- feign包名路径添加问题
			
1. feign包名路径添加问题 1.1. 问题 在SpringCloud中使用feign调用路径中,不能在类上直接添加@RequestMapping(value = "/hospital- ...
 - Enum扩展特性,代替中文属性
			
由于对英语的天生缺陷,在枚举时一直使用中文,这样就不用看注释就知道枚举意思,今天看到博文 https://www.cnblogs.com/emrys5/p/Enum-rename-htmlhelper ...
 - JavaScript深入(操作BOM对象)
			
浏览器对象模型(BOM) BOM的核心是window, 向下有: document(文档):document下由button,text,from,等等表单元素组成. location(地址对象),hi ...
 - Aseprite入门教程
			
因为最近在学cocos2d-x和vs搭配做手机游戏开发,想自己做一些素材,所以找到了这款软件,Aseprite v1.1.12.刚安装上时也是不懂该怎么操作,随着逐渐地摸索,对初始的使用有了一些了解. ...
 - C语言数组一种巧妙的使用方式
			
作为计算机一种比较古老的语言,它并没有随着岁月老去,而是仍旧在整个领域发挥出耀眼的光芒,就像写作,有很多光芒万丈的句子值得我们去珍藏,今天就遇到了个比较巧妙的数组使用方法,做个记录,以供后续使用. # ...
 - Hashtable 为什么不叫 HashTable?
			
前几天在写<HashMap 和 Hashtable 的 6 个区别>这篇文章的时候,差点把 Hashtable 写成了 HashTable,后来看源码证实了是:Hashtable,小写的 ...
 - 古典音乐 (java基础 继承)
			
摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 一.前言 小朽不才,最近爱上了听古典音乐收录了,mozart ,贝多芬… ...
 - Linux文件系统及文件属性
			
一.Linux文件系统 1.穿件文件系统 Linux中当磁盘格式化创建文件系统时,会创建一定数量的节点索引Inode以及一定数量的块block,其中inode具有存储文件属性以及指向文件实体block ...
 - C#.Net Core 操作Docker中的redis数据库
			
做软件开发的人,会在本机安装很多开发时要用到的软件,比如数据库,有MS SQL Server,MySQL,等,如果每种数据库都按照在本机确实有点乱,这个时候我们就想用虚拟机来隔离,这样就不会扰乱本机一 ...
 - Java中的instanceof和isInstance基础讲解
			
1. instanceof 是一个操作符 使用方法: ? 1 2 if(a instanceof B){ } 表示:a 是不是 B 这种类型 2. isInstance是Class类的一个方法 ? 1 ...