Codeforces #55D-Beautiful numbers (数位dp)
4 seconds
256 megabytes
standard input
standard output
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri (1 ≤ li ≤ ri ≤ 9 ·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
1
1 9
9
1
12 15
2 题目链接:http://codeforces.com/problemset/problem/55/D 题意
找区间[l,r]中的B数:该数能被其每一位数字整除。 分析
能被每一位数字整除,则能被所有位上的数字的lcm(最小公倍数)整除。而1到9的lcm为2520,于是我们可以进行数位dp,dp[i][j][k]表示到第i位时,
该数为j,且此前各数位的lcm为k。可是很显然,数值j不能直接表示出来,我们得想个办法优化。因为递归到最后实质是求x%lcm,那么我们可以想到一个性质:
x%m==x%(km)%m,这样,x%lcm==x%2520%lcm,于是第二维就缩减到2520以内了。现在空间复杂度则为20*2520*2520,还是爆炸。思考一下哪里可以优化?
突然发现,真正作为最小公倍数的个数并没有那么多,只有48个。至此,空间复杂度就变成了20*2520*50,接下来就是记忆化递归求解了,要注意边界的情况。
详情看代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#include<bitset>
#include<map>
#include<deque>
#include<stack>
using namespace std;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
typedef long long ll;
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+;
const int mod = 1e9+;
const int mm = ;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
ll dp[][][];
int has[];
int bit[];
ll gcd(ll a,ll b){
return (b==)?a:gcd(b,a%b);
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
void init(){
int tot=;
for(int i=;i<;i++){
if(mm%i==){
has[i]=tot++;
}
}
} ll dfs(int pos,int preSum,int preLcm,bool limit){
if(pos<) return preSum%preLcm==; //到最后一位
if(!limit && dp[pos][preSum][has[preLcm]]!=-)
return dp[pos][preSum][has[preLcm]];//记忆化
ll ans=;
int ed = limit?bit[pos]:; //该位有边界
for(int i=;i<=ed;i++){
int nowSum = (preSum*+i)%mm;
int nowLcm = preLcm;
if(i) nowLcm = lcm(nowLcm,i);
ans += dfs(pos-,nowSum,nowLcm,limit && i==ed);
}
if(!limit) dp[pos][preSum][has[preLcm]]=ans;
return ans;
}
ll solve(ll x){
int tot=;
while(x){
bit[tot++]=x%;
x/=;
}
return dfs(tot-,,,);
} int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t;
ll l,r;
ms(dp,-);
init();
scanf("%d",&t);
while(t--){
cin>>l>>r;
cout<<solve(r)-solve(l-)<<endl;
}
return ;
}
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/ ...
随机推荐
- python中类中属性和方法的具体定义方法和使用
1. Python中类中特性分成属性和方法 属性和方法都分为私有和公有的,私有的只可以在本类中使用外部是无法访问的 2. 定义属性(成员变量)的语法格式(公有属性/私有属性) class 类名: de ...
- WebSplider在线爬虫
WebSplider是什么? WebSplider在线爬虫是一个结合Web技术与爬虫技术的项目. WebSplider支持Web页面进行爬虫配置,提交配置到服务器后,服务器端爬虫程序进行数据抓取,最后 ...
- Catlike学习笔记(1.3)-使用Unity画更复杂的3D函数图像
第三篇来了-今天去参加了 Unite 2018 Berlin,感觉就是....非常困...回来以后稍微睡了下清醒了觉得是时候认真学习下了,不过讲的很多东西都是还没有发布或者只有 Preview 的版本 ...
- 华为笔试——C++平安果dp算法
题目:平安果 题目介绍:给出一个m*n的格子,每个格子里有一定数量的平安果,现在要求从左上角顶点(1,1)出发,每次走一格并拿走那一格的所有平安果,且只能向下或向右前进,最终到达右下角顶点(m,n), ...
- Final发布 文案+美工展示
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476项目地址:https://coding.net/u/wuyy694/ ...
- Pipeline Alpha版本项目展示
团队成员简介:http://www.cnblogs.com/cheneygroup/p/4830994.html 团队成员及博客: 李剑锋: Blog: http://www. ...
- 实验 六:分析linux内核创建一个新进程的过程
实验六:分析Linux内核创建一个新进程的过程 作者:王朝宪 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029 ...
- 派生类&简单工厂模式
派生类&简单工厂模式 git链接: Operation3.1.1 题目描述的代码部分的解释 首先是声明一个Rand类作为父类,然后两个子类RandNumber类和RandOperation类, ...
- iOS-copy与mutableCopy浅析
iOS-copy与mutableCopy浅析 iOS 浅谈:深.浅拷贝与copy.strong 总结:当不可变类型对象调用copy拷贝后,不会产生新的对象,属于浅拷贝,其他类型对象不管调用copy亦或 ...
- 共享服务Samba,实现liunx与Windows文件共享
Samba服务程序 是一款SMB协议并有服务器和客户端组成的开源文件共享软件,实现了Linux 与Windows系统之间的文件共享 Samba的配置文件有太多注释的东西,为了方便使用下面的命令,可以更 ...