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

  

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<LL,LL>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL n,len,l,r,tot;
LL f[][][];
LL d[];
LL rnk[];
LL mrk[];
inline LL dfs(LL now,LL lcm,LL rem,LL fp)
{
if (now==)return rem%lcm==;
if (!fp&&f[now][rnk[lcm]][rem]!=-)return f[now][rnk[lcm]][rem];
LL ans=;
LL mx=fp?d[now-]:;
LL nexlcm,nexrem=rem*%;
for (LL i=;i<=mx;i++)
{
nexlcm=lcm;
if (i)nexlcm=nexlcm*i/__gcd(lcm,i); nexrem+=i;if (nexrem>=)nexrem-=; ans+=dfs(now-,nexlcm,nexrem,fp&&i==mx); nexrem-=i;if (nexrem<)nexrem+=;
}
if (!fp)f[now][rnk[lcm]][rem]=ans;
return ans;
}
inline LL calc(LL x)
{
if (x==-)return ;
if (x==)return ;
LL xxx=x;
len=;
while (xxx)
{
d[++len]=xxx%;
xxx/=;
}
LL sum=;
for (LL i=;i<=d[len];i++)
{
sum+=dfs(len,i?i:,i,i==d[len]);
}
return sum;
}
inline void init()
{
priority_queue<LL,vector<LL>,greater<LL> >q;
q.push();
while ()
{
LL now=q.top();q.pop();
if (now>)break;
if (rnk[now])continue;
for (LL i=;i<=;i++)q.push(i*now);
rnk[now]=++tot;
mrk[tot]=now;
}
}
int main()
{
init();
LL T=read();
memset(f,-,sizeof(f));
while (T--)
{
l=read();
r=read();
if (r<l)swap(l,r);
printf("%lld\n",calc(r)-calc(l-));
}
}

cf 55D

[暑假集训--数位dp]cf55D Beautiful numbers的更多相关文章

  1. [暑假集训--数位dp]LightOj1205 Palindromic Numbers

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  2. 【数位dp】Beautiful Numbers @2018acm上海大都会赛J

    目录 Beautiful Numbers PROBLEM 题目描述 输入描述: 输出描述: 输入 输出 MEANING SOLUTION CODE Beautiful Numbers PROBLEM ...

  3. [暑假集训--数位dp]hdu3709 Balanced Number

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  4. [暑假集训--数位dp]hdu3555 Bomb

    The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...

  5. [暑假集训--数位dp]hdu3652 B-number

    A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- ...

  6. [暑假集训--数位dp]hdu2089 不要62

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍, ...

  7. [暑假集训--数位dp]hdu5787 K-wolf Number

    Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation o ...

  8. [暑假集训--数位dp]LightOJ1140 How Many Zeroes?

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...

  9. [暑假集训--数位dp]LightOj1032 Fast Bit Calculations

    A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true&quo ...

随机推荐

  1. C-基础:形参char *&p与char *p

    char* &p:以引用传递的方式传指针char* p: 以值传递的方式传指针

  2. vue入坑教程(三)vue父子组件之间互相调用方法以及互相传递数据

    1.父组件调用子组件的方法 父组件: <template> <div> <button v-on:click="clickParent">点击& ...

  3. cocos2dx for lua 摄像机移动

    在cocos2dx中,我们想通过移动摄像机来做一些特殊处理,比如将摄像机聚焦在某个物体上,或者摄像机颤抖,摄像机原理观察sprite回收状况等等, 都需要通过相机移动来使用. cocos2dx中的摄像 ...

  4. Java微信公众号开发----关键字自动回复消息

    在配置好开发者配置后,本人第一个想要实现的是自动回复消息的功能,说明以下几点: 1. url 仍然不变,还是开发配置里的url 2. 微信采用 xml 格式传输数据 3.微信服务器传给我们的参数主要有 ...

  5. redis学习笔记(2)

    redis学习笔记第二部分 --配置文件介绍 二,解析redis的配置文件redis.conf常见配置参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通 ...

  6. k8s资源指标API及metrics-server资源监控

    简述: 在k8s早期版本中,对资源的监控使用的是heapster的资源监控工具. 但是从 Kubernetes 1.8 开始,Kubernetes 通过 Metrics API 获取资源使用指标,例如 ...

  7. Nordic Collegiate Programming Contest 2015​ E. Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  8. Linux学习-Linux的账号与群组

    使用者识别码: UID 与 GID Linux 主机并不会直接认识 你的"帐号名称"的,他仅认识 ID 啊 (ID 就是一组号码啦). 由于计算机仅认识 0 与 1,所 以主机对于 ...

  9. POJ 3281 网络流 拆点 Dining

    题意: 有F种食物和D种饮料,每头牛有各自喜欢的食物和饮料,而且每种食物或者饮料只能给一头牛. 求最多能有多少头牛能同时得到它喜欢的食物或者饮料. 分析: 把每个牛拆点,中间连一条容量为1的边,保证一 ...

  10. iOS开发~CocoaPods安装和使用

    随着 iOS 开发者的增多,业界也出现了为 iOS 程序提供依赖管理的工具,它的名字叫做:CocoaPods. CocoaPods项目的源码 在 Github 上管理.该项目开始于 2011 年 8 ...