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
  • 相关阅读:
    ubuntu下环境变量
    Linux/Unix里,ln -s
    ubuntu安装和查看已安装
    Android系统中 setprop,getprop,watchprops命令的使用
    js中Math.random()生成指定范围数值的随机数
    mysql下sql语句 update 字段=字段+字符串
    铁道部2012年版全国72个铁路枢纽城市
    phprpc 使用实例(例实没错却不能执行)函数冲突gzdecode
    电脑开机一直蓝屏,一直重启要怎么办?
    电脑重装系统重装不了,老是蓝屏,是不是硬盘烧坏了!
  • 原文地址:https://www.cnblogs.com/kongbb/p/10350746.html
Copyright © 2011-2022 走看看