【HDU】 1333 Smith Numbers

来源:互联网 发布:js请求url获取返回值 编辑:程序博客网 时间:2024/06/13 04:22

Smith Numbers


题目链接

  • Smith Numbers

题目大意

    现在我们定义一种数叫Smith Numbers,这种数能被分解成若干个(>1)质数的乘积且数位和相同,现在让你求比n大的最小的Smith Numbers是哪个。


题解

    这个题本来直接暴力就行了的…结果中间传参传错了WA好久…


代码

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#define maxn 100000000using namespace std;int n,tab[1000005],vis[1000005],h,cnt[1000005];int sum(int a){    int ans=0;    while (a)    {        ans+=a%10;        a/=10;    }    return ans;}void dealtab(int high){    h=0;    for (int i=2;i<=high;i++) if (!vis[i])    {        int j=i;        cnt[h]=sum(i);        tab[h++]=i;        while (j<=high)        {            vis[j]=1;            j+=i;        }    }}bool check_(int p){    for (int i=0;tab[i]*tab[i]<=p;i++) if (p%tab[i]==0) return 0;    return 1;}int main(){    dealtab(100000);    while(scanf("%d",&n),n!=0)    {        for (int i=n+1;i<maxn;i++)        {            if (check_(i)) continue;            int x1=sum(i),x2=0,t=i;            for (int j=0;tab[j]*tab[j]<=t;j++)            {                int x3=cnt[j];                if (t==1) break;                while (t%tab[j]==0)                {                    t/=tab[j];                    x2+=x3;                }            }            if (t>1) x2+=sum(t);            if (x1==x2)            {                printf("%d\n",i);                break;            }        }    }    return 0;}
0 0