CodeForces 55D Beautiful numbers(数位dp+数学)
题目链接:http://codeforces.com/problemset/problem/55/D
题意:一个美丽数就是可以被它的每一位的数字整除的数。
给定一个区间,求美丽数的个数。
显然这是一道数位dp,就是满足一个数能被所有位数的lcm整除即可。
一般都会设dp[len][mod][LCM],mod表示余数,LCM表示前len位的lcm。
但是如果直接裸mod会很复杂,于是再想lcm{0,1,2,3,4,5,6,7,8,9}=2520;
而且lcm{a,b,c,d....}{a,b,c,d...表示各个位数)去重之后能被lcm{0,1,2....9}
整除。我们要求的是sum%lcm(a,b,c,d..}==0,所以只要满足
sum%lcm(0,1,2,...9}%lcm(a,b,c,d..}==0即可。于是mod就可以表示为
sum%lcm(0,1,2,...9}为多少。但是mod<=2520 && LCM<=2520这样
肯定存不下,于是要考虑如何处理LCM,毕竟很明显0~9的最大公倍数种类不会
超过48个。于是可以考虑一下离散化一下LCM,
if 2520 % num == 0 -> LCM[num]=temp++;
这样dp的三维就可以设为dp[20][2520][48];
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int mmax = 2520;
ll n , m , dp[20][mmax][50];
int temp , dig[20] , LCM[mmax + 10];
ll gcd(ll a , ll b) {
return b > 0 ? gcd(b , a % b) : a;
}
ll lcm(ll a , ll b) {
return a / gcd(a , b) * b;
}
void init() {
temp = 0;
for(int i = 1 ; i <= mmax ; i++) {
if(mmax % i == 0) {
LCM[i] = temp++;
}
else {
LCM[i] = 0;
}
}
}
ll dfs(int len , int count , int mod , int flag) {
if(!len) {
return mod % count == 0;
}
if(!flag && dp[len][mod][LCM[count]] != -1) {
return dp[len][mod][LCM[count]];
}
int t = flag ? dig[len] : 9;
ll sum = 0;
for(int i = 0 ; i <= t ; i++) {
int Nextmod = (mod * 10 + i) % mmax;
int Nextcount;
if(i == 0) {
Nextcount = count;
}
else {
Nextcount = (int)lcm(count , i);
}
sum += dfs(len - 1 , Nextcount , Nextmod , flag && i == t);
}
if(!flag)
dp[len][mod][LCM[count]] = sum;
return sum;
}
ll Gets(ll x) {
memset(dig , 0 , sizeof(dig));
int len = 0;
if(x == 0) {
dig[++len] = 0;
}
while(x) {
dig[++len] = x % 10;
x /= 10;
}
return dfs(len , 1 , 0 , 1);
}
int main() {
int t;
scanf("%d" , &t);
init();
memset(dp , -1 , sizeof(dp));
while(t--) {
scanf("%I64d%I64d" , &n , &m);
printf("%I64d\n" , Gets(m) - Gets(n - 1));
}
return 0;
}
CodeForces 55D Beautiful numbers(数位dp+数学)的更多相关文章
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 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- ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
随机推荐
- tensorflow学习笔记——常见概念的整理
TensorFlow的名字中已经说明了它最重要的两个概念——Tensor和Flow.Tensor就是张量,张量这个概念在数学或者物理学中可以有不同的解释,但是这里我们不强调它本身的含义.在Tensor ...
- go 学习笔记之工作空间
搭建好 Go 的基本环境后,现在可以正式开始 Go 语言的学习之旅,初学时建议在默认的 GOPATH 工作空间规范编写代码,基本目录结构大概是这个样子. . |-- bin | `-- hello.e ...
- 关于FFT分析音频的学习
本文部分知识从以下文章学习: https://zhuanlan.zhihu.com/p/19763358 傅里叶变换的知识 https://www.cnblogs.com/RabbitHu/p/FFT ...
- JavaFX Metro UI 和 开发库
目录 [隐藏] 1 Metro UI For JavaFX! 1.1 例子 1.2 Switch 1.3 Button 1.4 案例: 2 ConsrolsFX 3 Notification 的使用 ...
- excel 导入 下载模板 demo
import org.apache.commons.beanutils.PropertyUtils;import org.apache.commons.lang3.StringUtils;import ...
- 【0806 | Day 9】三张图带你了解数据类型分类和Python深浅拷贝
一.数据类型分类 二.Python深浅拷贝
- docker 容器之间互联
容器之间的互联 一. 实验目的: 1. 熟悉容器之间基本的网络原理: 2. 掌握容器之间互联的方法: 二. 实验环境: Ubuntu16.04+Docker 三. 实验内容: ...
- 利用cookie实现浏览器中多个标签页之间的通信
原理: cookie是浏览器端的存储容器,而且它是多页面共享的,利用cookie多页面共享的特性,可以实现多个标签页的通信. 比如: 一个标签页发送消息(将发送的消息设置到cookie中),一个标签页 ...
- 使用bibtex为latex论文添加参考文献
此文以引用Shannon的Prediction and Entropy of Printed English为例 1. bib文件 1.1 准备工作 进入Google Scholar 点击设置 ...
- JavaScript 数组、字符串、Map、Set 方法整理
在线阅读 https://www.kancloud.cn/chenmk/web-knowledges/1080519 数组 isArray():Array.isArray(value) 用于检测变量是 ...