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

Example

Input
1
1 9
Output
9
Input
1
12 15
Output
2
思路:数位DP。
被每一位整除则用二进制记录已经包括的数字的个数,以及对2520取模后的状态。
由于对5整除当且仅当最后一个数为0或5,对2整除当且仅当最后一个数为偶数,且1~9的最小公倍数为2520,不包括2,5后的最小公倍数为252。
所以除最后一层对2520取模,其余时候都对252取模即可。
由于整除的状态有限,最多只有48个,于是我们预处理出这48个数,并来一个映射就好(不然会TLE)。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int t,cnt;
long long l,r;
int num[],a[];
long long dp[][][];
int gcd(int x,int y){
return x==?y:gcd(y%x,x);
}
long long dfs(int pos,int x,int y,bool limite){
if(pos==-) return x%y==;
if(dp[pos][x][a[y]]!=-&&!limite) return dp[pos][x][a[y]];
int up=limite?num[pos]:;
long long tmp=;
for(int i=;i<=up;i++)
tmp+=dfs(pos-,pos?(x*+i)%:(x*+i),i?y*i/gcd(y,i):y,limite&&i==num[pos]);
if(!limite) dp[pos][x][a[y]]=tmp;
return tmp;
}
long long slove(long long now){
int pos=;
while(now){
num[pos++]=now%;
now/=;
}
return dfs(pos-,,,);
}
int main(){
scanf("%d",&t);
for(int i=;i<=;i++)
if(%i==)
a[i]=cnt++;
memset(dp,-,sizeof(dp));
while(t--){
scanf("%I64d%I64d",&l,&r);
cout<<slove(r)-slove(l-)<<endl;
}
}
 

vjudge A - Beautiful numbers的更多相关文章

  1. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  2. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  3. CodeForces 55D Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  4. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  5. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

  7. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

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

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

  9. Codeforces Beta Round #51 D. Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. 简述vuex的数据传递流程

    简述vuex的数据传递流程 当组件进行数据修改的时候我们需要调用dispatch来触发actions里面的方法.actions里面的每个方法中都会有一个commit方法,当方法执行的时候会通过comm ...

  2. C#窗体间的跳转传值

    1.开发平台VS2012 2.需求:从一个窗体跳转到另一个窗体,并传递参数,接收返回值. 3.案列如图: 4.代码如下: 登陆窗体: //当点击注册按钮 private void button2_Cl ...

  3. Java Web学习总结(26)——Servlet不同版本之间的区别

    1.   2.3版本 2.3版本 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2. ...

  4. 使用Modernizr检测支持CSS3

    使用Modernizr检测支持CSS3 如果支持某个属性,会增加一个class,名字就是该属性: 不支持,名字是no-某属性 还提供了一个全局Modernizr对象,使用如下: <script ...

  5. MySQL5.7 的 错误堆栈缓冲

    什么是错误缓冲堆栈呢? 举个非常简单的样例,比方运行以下一条语句:mysql> INSERT INTO t_datetime VALUES(2,'4','5');ERROR 1292 (2200 ...

  6. 基于Dragon Board410c 的智能机器人预研-语音识别及定位

    转自:http://www.csdn.net/article/a/2016-01-06/15833642 一.前言 机器人是一种可编程和多功能的.用来搬运材料.零件.工具的操作机,智能机器人则是一个在 ...

  7. web前端project师知识汇总

    分类: Web开发应用  一.何为Web前端project师?           前端project师,也叫Web前端开发project师.他是随着web发展.细分出来的行业.Web前端开发proj ...

  8. 1.RunLoop是什么?

    1.Run loops是线程相关的的基础框架的一部分. 一个run loop就是一个事件处理的循环.用来不停的调度工作以及处理输入事件.使用run loop的目的是让你的线程在有工作的时候忙于工作.而 ...

  9. js判断是对象还是集合

    /*============================================ 函数功能:对返回数据中的列表数据进行非空处理 ============================== ...

  10. yolo源码解析(2):处理图片

    首先安装ffmpeg, 参考https://blog.csdn.net/lwgkzl/article/details/77836207 然后将视频切分为图片, 参考:https://zhuanlan. ...