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. 本地启动项目后cookie跨域获取不到的处理方式

    问题现象   最近在做Vue项目,很多时候调试代码需要本地访问localhost来进行,然而请求接口是通过代理实现的,那么就会存在一种情况是:代理域名下种植的cookie,在localhost域名下访 ...

  2. HDU 1023 Train Problem II( 大数卡特兰 )

    链接:传送门 题意:裸卡特兰数,但是必须用大数做 balabala:上交高精度模板题,增加一下熟悉度 /************************************************ ...

  3. MBR和GPT分区学习

    1.MBR 早期的windows和linux都采用的MBR的方法来处理开机引导程序和分区表,对于linux的MBR分区来讲,0柱面0磁道1扇区用于引导驱动程序,第一扇区有512个字节,前446字节存放 ...

  4. ansible 主机清单 /etc/ansible/hosts

    主机清单 [webservers] ansible01 ansible02 ansible03 ansible04 [root@ftp:/root] > ansible webservers - ...

  5. bat启动.exe的应用程序

    新建一个文本文档,编写如下,完成后保存将后缀名txt改为bat即可. rem 启动***(要启动的服务名) @echo off rem  程序安装的顶层目录 d: rem 设置显示文字颜色 color ...

  6. 安装NexT主题

    Hexo 安装主题的方式非常简单,只需要将主题文件拷贝至站点目录的 themes 目录下, 然后修改下配置文件即可. 下载主题包 在终端窗口下,定位到 Hexo 站点目录下.使用 Git checko ...

  7. css预编译器——Less的使用

      方法一:仅介绍在客户端环境下使用的方法 1 新建test.less并引入.less该文件(和css一样在head处引入),注意rel="stylesheet/less": &l ...

  8. WinServer-IIS-Dynamic IP Restrictions

    动态IP限制 来自为知笔记(Wiz)

  9. video_capture模块分析

    1. 对外接口      VideoCaptureModule     控制接口      VideoCaptureDataCallback Vie中的ViECapturer继承,用于响应抓包数据 2 ...

  10. 使用malloc分别分配2KB的空间,然后用realloc调整为6KB的内存空间,打印指针地址

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...