zoukankan      html  css  js  c++  java
  • Smith Numbers经典

    Description

    While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the digits of the prime factors of that number. Got it? Smith's telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:
    4937775= 3*5*5*65837
    The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers. As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition. Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!

    Input

    The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.

    Output

    For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n,and print it on a line by itself. You can assume that such a number exists.

    Sample Input

    4937774
    0

    Sample Output

    4937775

    思路:

    本题的关键是质因数分解

    首先有如下性质:

    • 任意合数都可被分解为几个质因数的乘积
    • 给定合数的质因数分解表达式唯一
    根据上述性质,我们的质因数分解思路如下:
    设被分解合数为N,则分解步骤如下:
    • 初始状态,M = 2
    • 用M试除N,若能整除,说明M为N的质因数,则更新N = N / M,M不变;若不能整除,则N不变,M++
    上述方法蕴涵两个特性:
    • 被当前M整除的N其所有质因数均大于等于M。譬如:N若能被5整除,则其所有质因数均大于等于5,即其不可能再被2或3整除
    • 不需要判断当前M是否为素数,因为若为合数则必然不能整除。证明如下:假设M当前为合数,且M的一个质因数为P,则若N能被M整除则必然能被P整除,这与特性1矛盾-------摘抄的。

    #include<iostream>

    #include<stdio.h>

    #include<string.h>

    using namespace std;

    #define MAXN 10000

    #define LL int

    bool a[MAXN+1];

    LL save[MAXN+1];

    LL Smith(LL x)

    {

    LL sum=0;

    while(x)

    {

    sum=sum+x%10;

    x=x/10;

    }

    return sum;

    }

    void prime()

    {

    LL i,j;

    //memset(a,1,MAXN+1);//曾今用这个判断素数,超时。

    a[1]=1;

    for(i=2,j=2;i*j<=MAXN;j++)

    a[i*j]=1;

    for(i=3;i<100;i+=2)

    {

    if(!a[i])

    {

    for(j=2;i*j<=MAXN;j++)

    a[i*j]=1;

    }

    }

    }

    bool myth(LL x)

    {

    LL i;

    if(x==2)return true;

    if(x%2==0||x==1)return false;

    for(i=3;i*i<=x;i+=2)

    if(x%i==0)return false;

        return true;

    }

    int main()

    {

    LL x,sum,j,i,temp;

    prime();

    j=0;

    for(i=2;i<=MAXN;i++)

    if(!a[i])

        save[j++]=i;

    while(scanf("%d",&x)&&x)

    {

    while(++x)

    {

    sum=0;

    temp=x;

    if(myth(temp))

    continue;

    i=0;

    while(temp!=1)

    {

    while(temp%save[i]==0)

    {

       sum+=Smith(save[i]);

        temp=temp/save[i];

    }

    i++;

    if(myth(temp))//大于10000的素数最多出现一次

    {

         sum+=Smith(temp);

     break;

    }

    }

     

    if(sum==Smith(x))

    {

    printf("%d\n",x);

        break;

    }

    }

    }

    return 0;

    }

     



  • 相关阅读:
    Visual Studio统计有效代码行数
    Release 下调试代码设置 (VS2005/2008)(转)
    vs2008编译出来的程序不能运行或需要安装vcredist_x86.exe才能运行解决办法
    优化3D图形流水线
    (转)地形碰撞高度计算
    stlport调试watch查看容器里面的值
    JavaScript调试、测试和发布工具
    MYGUI/CEGUI中文输入的问题
    jar包的生成及运行
    安卓百度地图开发so文件引用失败问题研究
  • 原文地址:https://www.cnblogs.com/heqinghui/p/2604877.html
Copyright © 2011-2022 走看看