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. tomcat 映射虚拟路径

    编辑server.xml   在  <Host></Host>中添加 <Context path="/renbao/img/" docBase=&qu ...

  2. 封装HttpClient进行http请求与https请求

    一.https忽略证书 /** * 用于进行Https请求的HttpClient * * @author joey * */ public class SSLClient { public stati ...

  3. 关于android的设备管理器-DevicePolicyManager(一)

    在Andorid的设置->安全里面有个设备管理器的选项,相信大部分android用户都不太会去注意这个东西.近期在安装了一个应用之后发现这个里面的东西变了.怎么回事呢,研究研究看看.</s ...

  4. 设计模式实例(Lua)笔记之七(Decorator模式)

    1.描写叙述 就说说"我"上小学的的糗事吧. 我上小学的时候学习成绩非常的差,班级上 40 多个同学,我基本上都是在排名 45 名以后,依照老师给我的定义就是"不是读书的 ...

  5. QQ在线人数统计图数据解析

    转载请注明出处:http://blog.csdn.net/xiaoy_h/article/details/27980851 我相信非常多人一定去过这个地方: http://im.qq.com/onli ...

  6. iOS开发之UIAlertController的适配

    在iOS8中,只能用UIAlertController.而原来的UIAlertView及UIActionSheet已经被抛弃掉了.但是如果一台iOS 7 的手机运行到有UIAlertControlle ...

  7. linux 下的文件搜索、可执行文件搜索

    1. whereis 与 which 速度快,只是模糊查询,例如查询 $ whereis mysql,则会将mysql, mysql.ini, mysql*所在的目录都找出来: whereis 查看的 ...

  8. systemd服务管理---systemctl命令列出所有服务

    1.列出系统所有服务 #systemctl list-units --all --type=service

  9. javascript对象的相关操作

    Window对象 我们知道浏览器对象模型(BOM)是javascript的组成之一,它提供了独立于内容与浏览器窗口进行交互的对象.其分层结构如下: window对象是整个BOM的核心其有documen ...

  10. LeetCode 437. Path Sum III (STL map前缀和)

    找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...