题目

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 ;
}

【数位dp】CF 55D Beautiful numbers的更多相关文章

  1. 数位DP CF 55D Beautiful numbers

    题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...

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

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

  3. CF 55D Beautiful numbers (数位DP)

    题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...

  4. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

  5. 【数位DP】CF55D Beautiful numbers

    $dp[x][p][pp]$表示第x位,当前已有数字mod 2520(1~9数字的lcm)为p,当前各位数字的lcm为pp 观察到数组太大,考虑压缩,第三维lcm最多只有9个数字,打表发现最多只有48 ...

  6. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  7. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  8. [Codeforces-div.1 55D] Beautiful numbers

    [Codeforces-div.1 55D] Beautiful numbers 试题分析 还是离散化...\(f_{i,j,k}\)表示i位,gcd为j,余数为k. #include<iost ...

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

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

随机推荐

  1. (Java实现) 活动选择

    活动选择的类似问题都可以这么写 import java.util.ArrayList; public class huodongxuanze { /** * //算法导论中活动选择问题动态规划求解 * ...

  2. Java实现蓝桥杯VIP算法训练 奇变的字符串

    试题 算法训练 奇变的字符串 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 将一个字符串的奇数位(首位为第0位)取出,将其顺序弄反,再放回原字符串的原位置上. 如字符串" ...

  3. Java实现 LeetCode 234 回文链表

    234. 回文链表 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否 ...

  4. java中Calender类的详细用法(详解)

    一. 如何创建 Calendar 对象 Calendar 是一个抽象类, 无法通过直接实例化得到对象. 因此, Calendar 提供了一个方法 getInstance,来获得一个Calendar对象 ...

  5. Java实现哥德巴赫猜想

    验证哥德巴赫猜想:任何一个大于 6 的偶数,都能分解成两个质数的和.要求输入一个整数,输出这个 数能被分解成哪两个质数的和. eg : 14 14=3+11 14=7+7 public class T ...

  6. Java实现第八届蓝桥杯纸牌三角形

    纸牌三角形 题目描述 A,2,3,4,5,6,7,8,9 共9张纸牌排成一个正三角形(A按1计算).要求每个边的和相等. 下图就是一种排法(如有对齐问题,参看p1.png). A 9 6 4 8 3 ...

  7. jQuery实现拖拽元素

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 有关指针 -> 和* 的重载

    1, #include<iostream> #include<string> using namespace std; class test{ int i; public: t ...

  9. 09.Django-数据库优化

    Django查询数据库性能优化 现在有一张记录用户信息的UserInfo数据表,表中记录了10个用户的姓名,呢称,年龄,工作等信息. models文件 from django.db import mo ...

  10. java8 Date相关工具

    自己整理的java8 常用的工具类 /** * @Author taozhaoyang * @Description 结合 java8 线程安全 * @Date 9:55 2018/7/30 **/ ...