• POJ 1142:Smith Numbers(分解质因数)


     

                                   Smith Numbers

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 14521   Accepted: 4906

    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

    题意

    定义一个Smith Numbers:对于一个数n,如果n中的各位数相加的和与n的每个质因数上的各位数字相加的和相等;

    如:4937775= 3*5*5*65837 ; 4+9+3+7+7+7+5==3+5+5+6+5+8+3+7=42,即4937775是Smith Numbers

    给出一个数n,求出大于n的最小的Smith Numbers

    思路

    由定义可推出,Smith Numbers一定不是素数,所以可以先判断一个数是否是素数,然后写个对数n的各位数求和的函数,分解质因数,暴力求解即可

    AC代码

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <limits.h>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <set>
    #include <string>
    #define ll long long
    #define ms(a) memset(a,0,sizeof(a))
    #define pi acos(-1.0)
    #define INF 0x3f3f3f3f
    const double E=exp(1);
    const int maxn=1e6+10;
    using namespace std;
    int a[maxn];
    int is(ll x)//判断素数
    {
    	for(int i=2;i*i<=x;i++)
    	{
    		if(x%i==0)
    			return 0;
    	}
    	return 1;
    }
    ll sum(ll n)
    {
    	ll ans=0;
    	while(n)
    	{
    		ans+=n%10;
    		n/=10;
    	}
    	return ans;
    }
    int main(int argc, char const *argv[])
    {
    	ll n;
    	while(~scanf("%lld",&n))
    	{
    		if(n==0)
    			break;
    		ll _;
    		for(ll x=n+1;;x++)
    		{
    			ll ans;
    			ll flag=0;
    			ll y=x;
    			//如果不是素数
    			if(is(y)==0)
    			{
    				ans=sum(y);
    				for(int i=2;i*i<=y;i++)
    				{
    					if(y%i==0)
    					{
    						while(y%i==0)
    						{
    							flag+=sum(i);
    							y/=i;
    						}
    					}
    				}
    				if(y>1)
    					flag+=sum(y);
    				if(flag==ans)
    				{
    					_=x;
    					break;
    				}
    			}
    		}
    		printf("%lld
    ",_);
    	}
    	return 0;
    }
    
  • 相关阅读:
    The parent project must have a packaging type of POM
    oracle中PLSQL存储过程中如何使用逗号分隔的集合(逗号分隔字符串转换为一个集合)
    此实现不是 Windows 平台 FIPS 验证的加密算法的一部分的解决办法方案
    @Modules( ... ) 多个包路径问题
    phpstorm常用操作---1、phpstorm安装插件
    phpstorm中如何配置phpunit(单元测试)
    前端性能优化---3、静态资源使用cdn加速
    前端性能优化---2、图片响应式加载
    前端性能优化---1、懒加载和复杂资源点击时再请求
    Javascript进阶课程---1、使用工厂模式创建对象
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324445.html
Copyright © 2020-2023  润新知