题目

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

分析

所有的数位是1到9,最小公倍数是5 7 8* 9,是2520,开数组为数位,和,最小公倍数,分别对应dp【20】【2520】【2520】,但是超内存,所以利用hash,把最后的最小公倍数压缩在50以内,因为可能的最小公倍数是48个。

代码

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
long long a[];
int hash[];
long long dp[][][];
int gcd(int a,int b){
if(a<b)swap(a,b);
while(b){
int w=a;
a=b;
b=w%b;
}
return a;
} long long dpp(int pos,int sum,int b,int limit){
if(pos==-)return sum%b==;
if(!limit&&dp[pos][sum][hash[b]]!=-)return dp[pos][sum][hash[b]];
long long sun=;
int end=limit?a[pos]:;
for(int i=;i<=end;i++){
int q=b;
if(i>=)q=q*i/gcd(q,i);
sun+=dpp(pos-,(sum*+i)%,q,limit&&i==a[pos]); }
if(!limit)dp[pos][sum][hash[b]]=sun;
return sun;
} long long go(long long x){
int pos=;
while(x){
a[pos++]=x%;
x/=;
} return dpp(pos-,,,);
} int main(){
long long n,m,t;
int j;
memset(dp,-,sizeof(dp));
for(int i=,j=;i<=;i++){
if(%i==)hash[i]=j++;
}
scanf("%I64d",&t);
while(t--){
scanf("%I64d%I64d",&n,&m);
printf("%I64d\n",go(m)-go(n-));
} return ;
}

最新文章

  1. centos虚拟机复制移动后网络配置无效
  2. java 创建线程的三种方法Callable,Runnable,Thread比较及用法
  3. Android SDK ADT下载地址
  4. [转]理解HTTP幂等性
  5. Struts2(十)OGNL标签二与Struts2标签
  6. 【RSYSLOG】rsyslog作为日志采集器安装配置说明
  7. SVN版本管理trunk及branch相关merge操作
  8. 虚拟键盘,移动web开发的痛
  9. Codeforces Round #198 (Div. 2) B. Maximal Area Quadrilateral
  10. Android HttpClient HttpURLConnection相关介绍
  11. Windows 8.1下 MySQL绿色版安装配置与使用
  12. [Leetcode] Binary search--436. Find Right Interval
  13. PHP基础入门(三)---PHP函数基础
  14. Java基础(7)-异常处理
  15. 移动HTNL5前端框架—MUI
  16. java利用反射交换两个对象中的字段相同的字段值
  17. Linux查看和修改时间、日期
  18. 洗礼灵魂,修炼python(31)--面向对象编程(1)—面向对象,对象,类的了解
  19. LightOJ - 1265 (概率)
  20. 这份书单,给那些想学Hadoop大数据、人工智能的人

热门文章

  1. Java 蓝桥杯 算法训练(VIP) 最大体积
  2. Java实现 LeetCode 1111 有效括号的嵌套深度(阅读理解题,位运算)
  3. Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计
  4. Java实现 蓝桥杯VIP 算法提高 乘法运算
  5. Java实现交替字符串
  6. java实现第四届蓝桥杯逆波兰表达式
  7. java实现第五届蓝桥杯绳圈
  8. 温故知新-java多线程&amp;深入理解线程池
  9. Java规则引擎 Easy Rules
  10. Logstash下字段以及嵌套Json字段类型转换