CodeForces 55DBeautiful numbers 数位dp 离散化
来源:互联网 发布:死亡之翼的背脊数据库 编辑:程序博客网 时间:2024/04/29 05:00
Description
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.
Input
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
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).
Sample Input
11 9
9
112 15
2
规定一个数如果能够被自己每位非零的数整除则称漂亮数
求区间a到b的漂亮数的个数
因为个位数只能是1~9所以只要判断是否是1~9的lcm的因子就好了
lcm = 2520 20*2520*2520会超空间
考虑到2520内就48个因子所以进行离散化
ACcode:
#include <cstdio>#include <cstring>#include <iostream>#define maxn 3000#define mod 2520#define ll long longusing namespace std;ll dp[21][maxn][50];int data[21];int hash[maxn];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;}void has(){ int cnt=0; for(int i=1;i<=mod;i++){ if(mod%i==0){ hash[i]=++cnt; } }}ll dfs(int pos,int pre_mod,int pre_lcm,bool limit){ if(!pos)return pre_mod%pre_lcm==0; if(!limit&&dp[pos][pre_mod][hash[pre_lcm]]!=-1)return dp[pos][pre_mod][hash[pre_lcm]]; int ed=limit?data[pos]:9; ll ans=0; for(int d=0;d<=ed;d++) ans+=dfs(pos-1,(pre_mod*10+d)%mod,lcm(pre_lcm,d?d:1),limit&&d==ed); return limit?ans:dp[pos][pre_mod][hash[pre_lcm]]=ans;}ll fun(ll a){ int len=0; while(a){ data[++len]=a%10; a/=10; } return dfs(len,0,1,1);}void doit(){ ll a,b; scanf("%I64d%I64d",&a,&b); printf("%I64d\n",fun(b)-fun(a-1));}int main(){ has(); int loop;memset(dp,-1,sizeof(dp)); scanf("%d",&loop); while(loop--)doit();}
- CodeForces 55DBeautiful numbers 数位dp 离散化
- CodeForces 55DBeautiful numbers(数位dp)
- codeforces 55DBeautiful numbers( 数位dp )
- 【数位DP+离散化】Beautiful numbers CodeForces
- CodeForces 55DBeautiful numbers
- CodeForces 55D Beautiful numbers(数位dp&&离散化)
- codeforces 55D Beautiful numbers(数位dp+离散化)【模板】
- Beautiful numbers [数位dp][离散化]
- codeforces--55D--Beautiful numbers(数位dp,dfs+记忆化)
- Codeforces 55D Beautiful numbers 数位dp
- Codeforces 55D Beautiful numbers --- 数位DP
- Codeforces 55D Beautiful numbers 数位dp
- 【codeforces】55D. Beautiful numbers 数位DP
- [CodeForces 55D] Beautiful numbers && 数位DP
- Codeforces 55D Beautiful numbers 数位DP
- 【数位DP】 Codeforces 55D Beautiful numbers
- codeforces 55D Beautiful numbers[数位dp]
- [Codeforces 55D]Beautiful numbers(数位DP)
- POJ 1015--Jury Compromise
- 关于eclipse代码提示的配置
- 智能指针之 auto_ptr (pc版与gcc版)
- Unity Shaders and Effects Cookbook (7-2) Surface Shader 中实现 顶点动画
- C++实验5-项目一:数组分离
- CodeForces 55DBeautiful numbers 数位dp 离散化
- UBUNTU实战(3)
- js的Map和Set对象的使用
- html5插入视频
- Android intent跳转工具类
- android各个版本的新特性
- jdk动态代理实现总结和范例
- 响应式导航的制作
- sql入门