PC110706 Smith Number //数论 枚举

原题链接

Smith Numbers

 

While skimming his phone directory in 1982, mathematician Albert Wilansky 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 named this type of number after his brother-in-law: the Smith numbers.

As this property is true for every prime number, Wilansky excluded them from the definition. Other Smith numbers include 6,036 and 9,985.

Wilansky was not able to find a Smith number which was larger than the telephone number of his brother-in-law. Can you help him out?

Input

The input consists of several test cases, the number of which you are given in the first line of the input. Each test case consists of one line containing a single positive integer smaller than  109.

Output

For every input value  n, compute the smallest Smith number which is larger than  n and print it on a single line. You can assume that such a number exists.

Sample Input

1
4937774

Sample Output

4937775


此题大部分的OJ里都有,我在POJ,JOJ中都做过。
总觉得这么暴力解决TLE 但是终究没有。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

bool prime(int n)
{
	int i;
	for(i=2;i*i<=n;i++)
		if(n%i==0)
			return false;
	return true;
}

int getsum(int x)
{
	int sum=0;
	while(x)
	{
		sum=sum+x%10;
		x=x/10;
	}
	return sum;
}

bool judge(int x)
{
	int sum1,sum2=0,i,num=0;
	int p[10000];
	sum1=getsum(x);
	for(i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			p[num++]=i;
			x=x/i;
			while(x%i==0)
			{
				p[num++]=i;
				x=x/i;
			}
		}
		if(x==1)
			break;
	}
	if(x>1)
	{
		p[num++]=x;
	}
	for(i=0;i<num;i++)
	{
		sum2=sum2+getsum(p[i]);
	}
	if(sum1==sum2)
		return true;
	else
		return false;
}

int main()
{
	int n,i,cases;
	cin>>cases;
	
	while(cases--)
	{
		cin>>n;
		for(i=n+1;;i++)
		{
			if(prime(i))
				continue;
			if(judge(i))
			{
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值