CF 55D

Posted kongbb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 55D相关的知识,希望对你有一定的参考价值。

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

数位dp。
在这道题中离散化是最关键的一步操作,不然dp数组占用内存过大,无法申请。

技术图片
#include<cstdio>
#include<cstring>

int shu[20];
int lcm[2530];
long long dp[20][50][2530];

void init(){
    memset(dp,-1,sizeof(dp));
    int cnt = 0;
    for(int i=1;i<=2520;i++) // 离散化操作 将所有的因子处理出来
        if(2520%i==0)lcm[i]=++cnt;
    //printf("%d
",cnt); // 48
}
int gcd(int a, int b){
    return b?gcd(b,a%b):a;
}
long long dfs(int k, int lc, int r, bool shangxian){
    if(k==0)return r%lc==0;
    if(!shangxian && dp[k][lcm[lc]][r]!=-1)
        return dp[k][lcm[lc]][r];
    int maxn = shangxian?shu[k]:9;
    long long cnt = 0;
    for(int i=0;i<=maxn;i++){
        int now=lc;
        if(i)
            now *= i / gcd(i,lc);
        cnt += dfs(k-1,now,(r*10+i)%2520,shangxian&&i==maxn);
    }
    return shangxian?cnt:dp[k][lcm[lc]][r]=cnt;
}
long long solve(long long x){
    int k=0;
    while(x){
        shu[++k] = x%10;
        x/=10;
    }
    return dfs(k,1,0,1);
}
int main(){
    int t;
    long long A, B;
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%I64d %I64d",&A,&B);
        printf("%I64d
",solve(B)-solve(A-1));
    }
    return 0;
}
View Code

 








以上是关于CF 55D的主要内容,如果未能解决你的问题,请参考以下文章

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

CF55D [数位DP]

CF55D Beautiful numbers

CF55D Beautiful numbers (数位dp)

CF55D Beautiful numbers 数位dp

[CF55D]Beautiful numbers(数位dp,状态压缩)