zoukankan      html  css  js  c++  java
  • Codeforces 55D. Beautiful numbers 数位DP

    D. Beautiful numbers
     

    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).

    Sample test(s)
    input
    1
    1 9
    output
    9
    题意:定义一个数能被其各个位上的数整除的数称为美丽数
            给你一个区间,问你区间内美丽数的个数
    题解:  被各个位上数整除,也就是被各个位上的lcm整除,利用这点
               我们定义dp[20][9*8*7*5][9*8*7*5],显然太大,可知取与不取,总共有48种可能
               简化为dp[20][9*8*7*5][50],进行数位dp
     
    ///meek
    
    #include<bits/stdc++.h>
    using namespace std;
    
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-')f=-1;ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            x=x*10+ch-'0';ch=getchar();
        }return x*f;
    }
    //****************************************
    const int  N=100000+50;
    #define mod 10000007
    #define inf 10000007
    #define maxn 10000
    
    ll lcm(ll a,ll b)  {
        ll h,l;
        h=__gcd(a,b);
        l=a*b/h;
        return l;
    }int H[N];
    ll dp[20][2525][50],vis[20][2525][50],d[50];
    ll dfs(int dep,int sum,int f,int G) {
       if(dep<0) return sum%G==0;
       if(f&&vis[dep][sum][H[G]]) return dp[dep][sum][H[G]];
       if(f) {
           vis[dep][sum][H[G]]=1;
            ll& re=dp[dep][sum][H[G]];
           re+=dfs(dep-1,(sum*10)%2520,f,G);
           for(int i=1;i<=9;i++) {
                re+=dfs(dep-1,(sum*10+i)%2520,f,lcm(G,i));
          }
          return re;
       }
       else {
            ll re=0;
            re+=dfs(dep-1,(sum*10)%2520,0<d[dep],G);
          for(int i=1;i<=d[dep];i++) {
            re+=dfs(dep-1,(sum*10+i)%2520,i<d[dep],lcm(G,i));
          }return re;
       }
    }
    ll cal(ll x) {
        if(x==0) return 1;
       int len=0;//mem(dp),mem(vis);
       while(x) d[len++]=x%10,x/=10;
       return dfs(len-1,0,0,1);
    }
    int main() {
        int j=0;
        for(int i=1;i<=2520;i++) {
            if(2520%i==0) H[i]=j++;
        }
        int T=read();
        while(T--) {
            ll l,r;
            scanf("%I64d%I64d",&l,&r);
            printf("%I64d
    ",cal(r)-cal(l-1));
        }
        return 0;
    }
    代码
  • 相关阅读:
    学会分辨
    学会自重
    GIT使用笔记-fatal:multiple stage entries for merged file处理办法
    GIT-查看本地html帮助文档
    VS2013 GIT 克隆远程仓库
    GIT远程仓库地址变更
    VS2013中使用git发布解决方案master分支的时候出现错误
    C# WebService输出JSON 实现二
    WINDOWS下PhoneGap(Cordova)安装笔记
    SQLSERVER2012 Audit (审核)功能
  • 原文地址:https://www.cnblogs.com/zxhl/p/4968712.html
Copyright © 2011-2022 走看看