CF 55D 离散化各个位数的倍数
来源:互联网 发布:js按钮点击事件隐藏 编辑:程序博客网 时间:2024/05/19 20:21
传送门
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).
11 9
9
112 15
2
/**CF 55D 数位dp(一个数是组成它的所有位数对应数的倍数)题目大意:找出给定区间内有多少数,其本身是所有组成它的各个位上的数的倍数解题思路:(kuangbin)一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除,而1到9的最小公倍数为2520, 数位DP时我们只需保存前面那些位的最小公倍数就可进行状态转移,到边界时就把所有位的lcm求出了, 为了判断这个数能否被它的所有数位整除,我们还需要这个数的值,显然要记录值是不可能的,其实我们只 需记录它对2520的模即可,这样我们就可以设计出如下数位DP:dfs(pos,mod,lcm,f),pos为当前 位,mod为前面那些位对2520的模,lcm为前面那些数位的最小公倍数,f标记前面那些位是否达到上限, 这样一来dp数组就要开到19*2520*2520,明显超内存了,考虑到最小公倍数是离散的,1-2520中可能 是最小公倍数的其实只有48个,经过离散化处理后,dp数组的最后一维可以降到48,这样就不会超了。*/
#include<iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;#define me(x,y) memset(x,y,sizeof(x));#define LL long longtypedef long long ll;const int mod=2520;int a[25];LL dp[25][2550][50];int index[2550];int gcd(int a,int b){ if(b==0)return a; else return gcd(b,a%b);}int lcm(int a,int b){ return a/gcd(a,b)*b;}void init(){ int num=0; for(int i=1;i<=mod;i++) { if(mod%i==0) index[i]=num++; }}LL dfs(int pos,int presum,int prelcm,int limit){ if(pos<=0) return presum%prelcm==0; // if(sta<0) return 0; if(!limit && dp[pos][presum][index[prelcm]]!=-1) return dp[pos][presum][index[prelcm]]; int up=limit ? a[pos] : 9; LL ans=0; for(int i=0;i<=up;i++) { int nowsum=(presum*10+i)%mod; int nowlcm=prelcm; if(i) nowlcm=lcm(nowlcm,i); ans+=dfs(pos-1,nowsum,nowlcm,limit&&i==up); } if(!limit) dp[pos][presum][index[prelcm]]=ans; return ans;}LL solve(LL x){ int pos=0; while(x) { a[++pos]=x%10; x/=10; } return dfs(pos,0,1,1);}int main(){ int t; LL l,r; init(); cin>>t; me(dp,-1); while(t--) { cin>>l>>r; //while(~scanf("%d%d",&l,&r)&&(l+r)) //while(~scanf("%d",&r)) printf("%I64d\n",solve(r)-solve(l-1)); } return 0;}
- CF 55D 离散化各个位数的倍数
- CF 55D 数位dp(一个数是组成它的所有位数对应数的倍数)
- 【cf】55d beautiful numbers【精妙的数位dp+离散化】
- CF 633 D 离散化+二分查找+暴力(还是要多学习学习呀,挺好的题目)
- CF 19D 线段树+set压缩坐标轴+离散化map
- CF #261 Div2 D. Pashmak and Parmida's problem (离散化+逆序对+线段树)
- 将一个n位数分解为各个位数的数字。
- 一个数的各个位数交换
- 分离一个整数各个位数的算法
- JavaScript取各个位数的方法
- 将十进制的各个位数分离出来
- CF 112D. Petya and Divisors 枚举(标记倍数最后出现位置)
- cf#345-C - Watchmen-map离散化
- 数位dp cf 55d
- CF 55D 数位dp
- CF 55D Beautiful numbers
- CF 55D 数位DP
- CF 55D(数位DP)
- C++primer 第十五章笔记 初稿
- 【HDU
- react开发报错Use the prop-types package from npm instead.
- Pytorch 学习笔记之自定义 Module
- 让axios发送表单请求形式的键值对post数据
- CF 55D 离散化各个位数的倍数
- C++模版类的声明与定义
- 重定向与管道命令
- 剑指offer 练习四(Java版)
- HDU 4825 Xor Sum (01字典树模板题)
- hdu1596 find the safest road(DijKstra模板简单变形)
- 微信小程序制作表格代码
- maven设置了bootclasspath
- nginx+tomcat+memcache配置