zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    hdu3001 Travelling
    android 对一个合并后的联系人选择编辑,手机屏幕会缓慢变暗后再进入编辑界面的问题
    数组指针与指针数组 函数指针与指针函数
    CF:322D
    QRadioButton类中Toggled()信号的使用方法
    Android---App Widget(四)
    github 坑爹的仓库初始化设置
    Failed to load session “ubuntu” -- 12.04
    Linux下的图形界面——X Window的安装
    用户管理操作示例
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7401637.html
Copyright © 2011-2022 走看看