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).

Examples

Input
1
1 9
Output
9
Input
1
12 15
Output
2

数位dp。
在这道题中离散化是最关键的一步操作,不然dp数组占用内存过大,无法申请。
#include<cstdio>
#include<cstring> int shu[];
int lcm[];
long long dp[][][]; void init(){
memset(dp,-,sizeof(dp));
int cnt = ;
for(int i=;i<=;i++) // 离散化操作 将所有的因子处理出来
if(%i==)lcm[i]=++cnt;
//printf("%d\n",cnt); //
}
int gcd(int a, int b){
return b?gcd(b,a%b):a;
}
long long dfs(int k, int lc, int r, bool shangxian){
if(k==)return r%lc==;
if(!shangxian && dp[k][lcm[lc]][r]!=-)
return dp[k][lcm[lc]][r];
int maxn = shangxian?shu[k]:;
long long cnt = ;
for(int i=;i<=maxn;i++){
int now=lc;
if(i)
now *= i / gcd(i,lc);
cnt += dfs(k-,now,(r*+i)%,shangxian&&i==maxn);
}
return shangxian?cnt:dp[k][lcm[lc]][r]=cnt;
}
long long solve(long long x){
int k=;
while(x){
shu[++k] = x%;
x/=;
}
return dfs(k,,,);
}
int main(){
int t;
long long A, B;
init();
scanf("%d",&t);
while(t--){
scanf("%I64d %I64d",&A,&B);
printf("%I64d\n",solve(B)-solve(A-));
}
return ;
}

CF 55D的更多相关文章

  1. 数位DP CF 55D Beautiful numbers

    题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...

  2. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

  3. CF 55D - Beautiful numbers(数位DP)

    题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...

  4. CF 55D Beautiful numbers (数位DP)

    题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...

  5. cf 55D 数位dp 好题

    /* 刚开始我考虑0的情况,想将他剔除就将lcmn设为-1,这样还要判断0和lcmn是-1的情况很麻烦而且但是一直出错 后来觉得不用管0的情况就行了,可以认为符合. 解:将lcmn离散化,因为1-9的 ...

  6. 【数位dp】CF 55D Beautiful numbers

    题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...

  7. CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)

    题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...

  8. 数位DP入门

    HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...

  9. 【转载】ACM总结——dp专辑

    感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一 ...

随机推荐

  1. windows mklink

    https://blog.csdn.net/kilosnow/article/details/7179705 创建目录链接E:\>mklink /d c:\b d:\a为 c:\b <&l ...

  2. property--name--id-这三者在值传递的过程中的实现关系

    作者:light链接:https://www.zhihu.com/question/286739416/answer/454300180来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  3. React中setState同步更新策略

    setState 同步更新 我们在上文中提及,为了提高性能React将setState设置为批次更新,即是异步操作函数,并不能以顺序控制流的方式设置某些事件,我们也不能依赖于this.state来计算 ...

  4. php第三节(运算符)

    <?php //算术运算符 + - * / % //++ 前加加 先做加运算后座赋值运算 后加加 先做赋值运算后座加运算 //-- 前减减 先做加运算后座赋值运算 后减减 先做赋值运算后座加运算 ...

  5. <<学会提问>>第一章学习笔记

    中国应不应该现在取消死刑? 中医是不是伪科学? 读书无用论? 集体主义和团队精神? 欧洲难民危机,你是支持接收难民,还是反对? 欧洲白左是不是幼稚圣母,抑或是右派种族歧视,顽固保守? 如何看待&quo ...

  6. WebMagic 启动例子报错

    报错内容: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/Http ...

  7. 转:Spring Boot应用中的异常处理

    引自:https://www.cnblogs.com/yangfanexp/p/7616570.html 楼主前几天写了一篇“Java子线程中的异常处理(通用)”文章,介绍了在多线程环境下3种通用的异 ...

  8. PHP入门笔记--基础语法二

    一.函数 自定义函数 任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和类定义. <?php function foo() { function bar() { echo &qu ...

  9. Hadoop(6)-HDFS的shell操作

    1.基本语法 使用 hadoop fs 具体命令   或者   hdfs dfs 具体命令 hadoop命令的shell源码 hdfs命令的shell源码 由此可见,这两个命令最后都是执行的一个jav ...

  10. Python基础03

    while循坏while属于条件判断 条件满足====>执行 条件不满足====>退出循环 whlie循环格式 while 条件 : 执行语句 while 1 == 1: print(&q ...