51nod-1232: Perfect number

Source: Internet
Author: User
[Portal: 51nod-1232] Brief question:

Perfect number definition: A number can be divisible by a non-zero number of each digit, for example, 11,12, 15, but 13 is not a perfect number, because 13 cannot be divisible by 3.

The T-group query is given. Each group queries the number of perfection numbers in input L, R, and output L to R.

Question:

First, it is easy to think that as long as the current number is divisible by the LCM of all non-zero numbers in each bit

In fact, the maximum LCM is only 2520, and only 48 different LCM values can be formed.

You can use this to save space and separate the LCM discrete numbers.

After F [I] [J] [k] is set to the I-th digit, the current number % 2520 = J, the discrete number of the LCM of the current number is K.

Then you can simply perform a memory-based search.

Reference code:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespace std;typedef long long LL;LL f[21][3100][51];int p[3100],Mod=2520;void pre(){    int k=0;p[0]=0;    for(int i=1;i<=Mod;i++) if(Mod%i==0) p[i]=++k;}int cnt[21],tot;int gcd(int a,int b){    if(a==0) return b;    else return gcd(b%a,a);}int getd(int a,int b){    if(a==0) return b;    if(b==0) return a;    return a*b/gcd(a,b);}LL dfs(int tp,int x,int d1,int d2){    if(x==1)    {        if(d2==0) return 0;        return d1%d2==0;    }    if(tp==0&&f[x][d1][p[d2]]!=-1) return f[x][d1][p[d2]];    if(tp==0)    {        f[x][d1][p[d2]]=0;        for(int i=0;i<=9;i++)        {            if(i==0) f[x][d1][p[d2]]+=dfs(tp,x-1,(d1*10+i)%Mod,d2);            else f[x][d1][p[d2]]+=dfs(tp,x-1,(d1*10+i)%Mod,getd(d2,i));        }        return f[x][d1][p[d2]];    }    else    {        LL ans=0;        for(int i=0;i<=cnt[x-1];i++)        {            if(cnt[x-1]==0) ans+=dfs(tp,x-1,(d1*10+i)%Mod,d2);            else if(i==0) ans+=dfs(tp^1,x-1,(d1*10+i)%Mod,d2);            else if(i==cnt[x-1]) ans+=dfs(tp,x-1,(d1*10+i)%Mod,getd(d2,i));            else ans+=dfs(tp^1,x-1,(d1*10+i)%Mod,getd(d2,i));        }        return ans;    }}LL solve(LL x){    if(x==0) return 0;    LL d=x;tot=0;    while(d!=0){cnt[++tot]=d%10;d/=10;}    LL ans=0;    for(int i=0;i<=cnt[tot];i++)    {        if(i==0) ans+=dfs(0,tot,0,0);        else if(i==cnt[tot]) ans+=dfs(1,tot,i,i);        else ans+=dfs(0,tot,i,i);    }    return ans;}int main(){    pre();    int T;    scanf("%d",&T);    memset(f,-1,sizeof(f));    while(T--)    {        LL l,r;        scanf("%lld%lld",&l,&r);        printf("%lld\n",solve(r)-solve(l-1));    }    return 0;}
 

51nod-1232: Perfect number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.