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\n",solve(n)-solve(m-1));
    }
    return 0;





}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值