CodeForces

来源:互联网 发布:任我行软件 编辑:程序博客网 时间:2024/06/06 12:55

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

Examples
input
11 9
output
9
input
112 15
output
2




题意:求区间内,有多少个完美数


解题思路:数位Dp经典题。类似不要13这道题。这里的难点是如何表示能否被整除。其实只用判断能否被2520整除即可。庆祝下……终于搞懂完美数了。




#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>#include<bitset>using namespace std;typedef long long int ll;ll dp[40][50][3000];//第i个数,前i个数字的最小公倍数,余数。int dig[40];map<int,int> m;//映射int gcd(int a,int b){    if(!b) return a;    else return gcd(b,a%b);}int lcm(int a,int b){    return a/gcd(a,b)*b;}//具体可以参考不要13这道题。ll dfs(int pos,int sta,int mod,bool limit){        if(pos==0)        return mod%sta==0;        //记忆化搜索    if(!limit && dp[pos][m[sta]][mod]!=-1)        return dp[pos][m[sta]][mod];        //循环遍历下一位的每一个数字,end记录最大能达到的数    int end=limit?dig[pos]:9;    ll ans=0;        for(int i=0;i<=end;i++){        if(i!=0)            ans+=dfs(pos-1,lcm(sta,i),(mod*10+i)%2520,limit&&i==end);        else            ans+=dfs(pos-1,sta,(mod*10+i)%2520,limit&&i==end);    }        if(!limit)        dp[pos][m[sta]][mod]=ans;        return ans;    }ll solve(ll x){    int pos=1;    while(x){        dig[pos++]=x%10;        x/=10;    }        return dfs(pos-1,1,0,1);}int main(){        int cnt=0;    for(int i=1;i<=2520;i++)        if(2520%i==0)            m[i]=cnt++;        memset(dp,-1,sizeof(dp));    ll l,r;    int t;    scanf("%d",&t);    for(int qqq=1;qqq<=t;qqq++){        scanf("%I64d%I64d",&l,&r);                printf("%I64d\n",solve(r)-solve(l-1));    }        return 0;}






原创粉丝点击