• codeforces 55D 数位dp


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

    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

     题意:

    求L,R区间内有多少数能被自身的除了0之外的各个位整除。

    代码:

    //考虑到1~9的数的lcm是2520,可以dp[20][2520][2520]表示,但是内存受不了,因为0和1不用判断所以就只有7个数字需要判断是否
    //能整除原数因此可以 1<<8 把这七个数字压缩一下就好了。
    //这题还可以将最后一维离散化,因为2~9的数字随意组合的lcm只有48个,然后醉后一维就成了48,这样更快。
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=100000;
    const ll mod=2520;
    int bit[20];
    ll dp[20][2600][1<<8];
    int check(int sum,int sta)
    {
        for(int i=2;i<=9;i++){
            if((sta&(1<<(i-2)))&&(sum%i!=0))
                return 0;
        }
        return 1;
    }
    ll dfs(int pos,int sum,int sta,bool limt)
    {
        if(pos==0) return check(sum,sta);
        if(!limt&&dp[pos][sum][sta]!=-1)
            return dp[pos][sum][sta];
        ll ans=0;
        int maxb=(limt?bit[pos]:9);
        for(int i=0;i<=maxb;i++){
            if(i<2) ans+=dfs(pos-1,(sum*10+i)%mod,sta,limt&&(i==maxb));
            else ans+=dfs(pos-1,(sum*10+i)%mod,sta|(1<<(i-2)),limt&&(i==maxb));
        }
        if(!limt)
            dp[pos][sum][sta]=ans;
        return ans;
    }
    ll solve(ll n)
    {
        int nu=0;
        while(n){
            bit[++nu]=n%10;
            n/=10;
        }
        return dfs(nu,0,0,1);
    }
    int main()
    {
        int t;
        ll L,R;
        memset(dp,-1,sizeof(dp));
        scanf("%d",&t);
        while(t--){
            scanf("%lld%lld",&L,&R);
            printf("%lld
    ",solve(R)-solve(L-1));
        }
        return 0;
    }
  • 相关阅读:
    GIT DIFF生成.PATCH文件
    C++中static关键字作用总结
    模版与泛型编程
    模版以及全特化,偏特化
    重载运算与类型转换
    GDB调试工具(待整理)
    面向对象(primer)
    7种获取高度的区别
    把页面主体内容限定在安全区内
    ios浏览器下载,apple-itunes-app
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7401637.html
Copyright © 2020-2023  润新知