Beautiful numbers

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

11 9

Output

9

Input

112 15

Output

2

DP好难2333
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

const int mod=2520;
typedef long long LL;

long long dp[25][mod][50];

int index[2530],bit[25];

void init()
{
    int num=0;
    for(int i=1;i<=mod;i++)
    {
        if(mod%i==0)
            index[i]=num++;
    }
}

int gcd(int a,int b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}

int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}

LL dfs(int pos,int presum,int prelcm,bool flag)
{
    if(pos==-1)return presum%prelcm==0;
    if(!flag&&dp[pos][presum][index[prelcm]]!=-1)
        return dp[pos][presum][index[prelcm]];
    LL ans=0;
    int up=flag?bit[pos]:9;
    for(int i=0;i<=up;i++)
    {
        int nowsum=(presum*10+i)%mod;
        int nowlcm=prelcm;
        if(i)nowlcm=lcm(nowlcm,i);
        ans+=dfs(pos-1,nowsum,nowlcm,flag&&i==up);
    }
    if(!flag)dp[pos][presum][index[prelcm]]=ans;
    return ans;
}

LL solve(LL x)
{
    int len=0;
    while(x)
    {
        bit[len++]=x%10;
        x/=10;
    }
    return dfs(len-1,0,1,true);
}

int main()
{
    int T;
    init();
    memset(dp,-1,sizeof(dp));
    scanf("%d",&T);
    while(T--)
    {
        LL r,l;
        scanf("%I64d%I64d",&l,&r);
        printf("%I64d\n",solve(r)-solve(l-1));
    }
    return 0;
}
时间: 2024-11-04 08:39:02

Beautiful numbers的相关文章

Codeforces Beta Round #51---D. Beautiful numbers(数位dp, 巧妙)

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 num

CodeForces 55D Beautiful numbers(数位dp&amp;&amp;离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers 题意 ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少. 求一个区间内的Beautiful numbers有多少个.Beautiful numbers指:一个数能整除所有组成它的非0数字. 例如15可以被1和5整除,所以15是Beautiful numbers. 思路 Beautiful numbers指:一个数能整除所有组成它的非0数字. 等同于 一个数能整除 所有组成它的

CodeForces 55D Beautiful numbers

Beautiful numbers 题目链接 Description 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

C. Beautiful Numbers

C. Beautiful Numbers Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) Total Submission(s) : 27   Accepted Submission(s) : 7 Problem Description Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly

CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+快速幂)

C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal

CF 55D - Beautiful numbers(数位DP)

题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公倍数是 2520({5,7,8,9}),先预处理出所有可能的最小公倍数m[c] dp[i][d][c]表示长度i, 余数d,最小公倍数是m[c]的个数. #include<cstdio> #include<cstring> #define mod 2520 ll dp[35][2520

[暑假集训--数位dp]cf55D Beautiful numbers

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 num

cf Beautiful numbers(数位dp)

Beautiful numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 55D Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number i

CodeForces 55D Beautiful numbers 数位DP+数学

题意大概是,判断一个正整数区间内有多少个整数能被它自身的每一个非零的数字整除. 因为每一个位置上的整数集s = {0,1,2,3,4,5,6,7,8,9} lcm(s) = 2520 现在有一个整数t是由s中一个或者多个数字构成的,记为abcde,显然t = a*10^4+b*10^3+c*10^2+d*10^1+e 要使得t能被a,b,c,d,e整除,必然有t % lcm(a,b,c,d,e) = 0 因为a,b,c,d,e去重之后一定是s的一个子集,所以lcm(s)一定是lcm(a,b,c,