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/ ...
随机推荐
- 软件测试_APP测试_兼容性测试
APP的兼容测试主要就是测试APP的安装.启动.运行.卸载测试,以及安装时间 .启动时间.CPU占用.内存占用.流量耗用.电量耗用等性能上的测试. 兼容性测试点: 一.内部兼容性: 1.与本地和其他主 ...
- C#易忘点
下面是自己总结的一些C#语言方面用的少容易忘的地方,总结一下,在用的时候看一下.(仅针对本人) 参数数组 定义一个函数,用来取得数字的和,但是数字的个数不确定. 解决方案: 1,定义一个函数,参数传递 ...
- anoconda 神经网络 相关操作
1. conda 相关操作 建立新环境:conda crearte -n NewName python=版本(3.6) 激活环境:conda activate NewName 关闭环境:conda d ...
- PAT甲题题解-1012. The Best Rank (25)-排序水题
排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...
- 【Alpha】第一次Scrum Meeting
本次会议内容概括如下: 总结了一周以来大家任务的完成情况,对消耗时间进行统计,并评估了各自对团队的贡献(影响)程度 整理并融合所有人的工作的结果生成了相应的总结性文档 进一步明确了团队中各个成员的定位 ...
- vis.js绘图库的一个BUG以及源码修正
1. BUG 1.1 BUG触发情况 在使用vis.js绘图时,加入两个节点A和B之间既存在一条从A指向B的边,同时也存在一条从B指向A的边,那么这个绘图库就会崩溃. 1.2 BUG解析 vis.js ...
- beta圆桌2!
031602111 傅海涛 1.今天进展 文档转换差不多完成 2.存在问题 无 3.明天安排 增加新功能和完善之前的功能 4.心得体会 接口真难 031602115 黄家雄 1.今天进展 考了六级 2 ...
- 11th 回顾5个问题
当初提出的5个问题: 1.书中说很多非常成功的软件都是赢在用户体验,后面的第12章也专门提到了用户体验,说软件开发时可以使用5W1H的方法来判断用户的体验,而需求分析需要获取用户需求,进行用户调研,那 ...
- Activiti End Event及其派生类使用范例
http://tynerblain.com/blog/2006/08/11/bpmn-end-events-1/ 普通流程完结就用end event,而比如无效卡支付时,就会进入cancel end ...
- Python3《机器学习实战》学习笔记(一):k-近邻算法(史诗级干货长文)
https://blog.csdn.net/c406495762/article/details/75172850