zoukankan      html  css  js  c++  java
  • codeforces 55D. Beautiful numbers (数位dp)

    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
    题意:给你一个区间,找到这个区间里面的"漂亮数","漂亮数"的定义为这个数能够整除所有位数上(不为0)的数。

    思路:一个数能够整除所有位上的数,那么它就能够整数所有位上的值的最小公倍数。因为一个位数上的数为1~9,它们的最小公倍数最大为2520,而且所有情况的最小公倍数的数量为48个。又因为所有最小公倍数都能整除2520,即都是2520的约数,所以我们可以先存下前面所代表的十进制数模上2520的余数yushu,并且记录前面的所有位上的数的最小公倍数lcm,那么最后如果yushu%lcm==0,就表示这个数可行。这里还要注意,最小公倍数不用从1~2520依次枚举,因为可行的就只有48个,需要hash一下。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<bitset>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef long double ldb;
    #define inf 99999999
    #define pi acos(-1.0)
    #define MOD 1000000007
    #define maxn 100050
    
    int has[55]={0,1,2,3,4,5,6,7,8,9,10,12,14,15,18,20,21,24,28,30,35,36,40,42,45,56,60,63,70,72,84
    ,90,105,120,126,140,168,180,210,252,280,315,360,420,504,630,840,1260,2520};
    
    ll dp[25][2550][55];
    int wei[30];
    int gcd(int a,int b){
        return b ? gcd(b,a%b) : a;
    }
    
    
    ll dfs(int pos,int yushu,int index,int flag){
        int i,j;
        if(pos==0){
            if(yushu%has[index]==0)return 1;
            else return 0;
        }
        if(!flag && dp[pos][yushu][index ]!=-1)return dp[pos][yushu][index ];
    
        int ed=flag?wei[pos]:9;
        int yushu1,lcm1,index1;
        ll ans=0;
        for(i=0;i<=ed;i++){
            yushu1=(yushu*10+i)%2520;
            if(i==0)index1=index;
            else{
                int lcm=has[index];
                lcm1=lcm*i/gcd(lcm,i);
                index1=lower_bound(has,has+49,lcm1)-has;
            }
            ans+=dfs(pos-1,yushu1,index1,flag&&(i==ed) );
    
        }
        if(!flag)dp[pos][yushu][index]=ans;
        return ans;
    
    
    
    
    }
    
    
    ll solve(ll x)
    {
        int i,j,len=0;
        ll t=x;
        //memset(dp,-1,sizeof(dp));
        while(t){
            wei[++len]=t%10;
            t/=10;
        }
        return dfs(len,0,1,1);
    }
    
    
    int main()
    {
        int i,j,T;
        ll n,m;
        memset(dp,-1,sizeof(dp));
        scanf("%d",&T);
        while(T--)
        {
            scanf("%I64d%I64d",&m,&n);
            printf("%I64d
    ",solve(n)-solve(m-1));
        }
        return 0;
    
    
    
    
    
    }




  • 相关阅读:
    说说如何用js实现一个模板引擎
    JS组件系列——又一款MVVM组件:Vue(二:构建自己的Vue组件)
    React之ref详细用法
    安装SQL Server提示“重叠的IO操作正在进行”解决
    HDU3746 Cyclic Nacklace 【KMP】
    读&lt;大数据日知录:架构与算法&gt;有感
    VMware虚拟机安装Linux英文改中文系统并更新yum安装输入法
    数据结构的基本概念
    MFC TreeCtrl 控件(一):简单使用
    Java生成word文档
  • 原文地址:https://www.cnblogs.com/herumw/p/9464528.html
Copyright © 2011-2022 走看看