zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Java SE 5.0(JDK 1.5)新特性
    第22章—开启HTTPS
    第21章—websocket
    IE8Get请求中文不兼容:encodeURI的使用
    JavaScript自定义函数
    disable的错误使用
    20190401-记录一次bug ConstraintViolationException
    new Date()的浏览器兼容性问题
    单例模式(转)
    SQL Server使用一个语句块批量插入多条记录的三种方法和union和union all区别
  • 原文地址:https://www.cnblogs.com/kongbb/p/10350746.html
Copyright © 2011-2022 走看看