110706 Smith Numbers

原创 2013年12月05日 14:43:25


import java.util.*;
import static java.lang.Math.*;

public class Main {
	private static List<Long> s_Primes;
	
	static
	{
		s_Primes = new LinkedList<Long>();
		s_Primes.add(2L);
		long limit = (long)sqrt(2000000000L) + 1;
		for (long i = 3L; i <= limit; i += 2L)
		{
			long mid = (long)sqrt(i);
			Iterator<Long> iter = s_Primes.iterator();
			
			boolean isPrime = true;
			while (iter.hasNext())
			{
				long x = iter.next();
				if (x > mid)
					break;
				
				if ((i % x) == 0)
				{
					isPrime = false;
					break;
				}
			}
			
			if (isPrime)
				s_Primes.add(i);
		}
	}
	
	private static List<Long> GetAllPrimeElements(long number)
	{		
		List<Long> result = new LinkedList<Long>();
		Iterator<Long> iter = s_Primes.iterator();
		boolean canTryNext = true;
		long mid = (long)sqrt(number);
		long i = 0;
		while (number > 1)
		{
			if (canTryNext)
			{
				if (!iter.hasNext())
				{
					result.add(number);
					return result;
				}
				
				i = iter.next();
				if (i > mid)
				{
					result.add(number);
					return result;
				}
			}
			
			if ((number % i) == 0)
			{
				canTryNext = false;
				number /= i;
				mid = (long)sqrt(number);
				result.add(i);
			}
			else
				canTryNext = true;
		}
		
		return result;
	}
	
	private static long GetSum(long number)
	{
		long sum = 0;
		while (number > 0)
		{
			sum += number % 10;
			number /= 10;
		}
		return sum;
	}
	
	private static long GetSum(List<Long> elements)
	{
		if ((elements == null) ||
				(elements.size() <= 1))
			return -1;
		
		long sum = 0;
		for (long i : elements)
			sum += GetSum(i);
		
		return sum;
	}
	
	private static boolean IsSmith(long number)
	{
		if (number <= 3)
			return false;
		
		List<Long> elements = GetAllPrimeElements(number);
		
		long numberSum = GetSum(number);
		long elementsSum = GetSum(elements);
		
		return (numberSum == elementsSum);
	}
	
	private static void Handle(long n)
	{
		long number = n + 1;
		while (!IsSmith(number))
			++number;
		System.out.println(number);
	}
	
	public static void main(String[] args)
	{
		Scanner inScanner = new Scanner(System.in);
		long cnt = inScanner.nextLong();
		for (int i = 1; i <= cnt; ++i)
		{
			long n = inScanner.nextLong();
			if (n <= 3L)
				System.out.println(4);
			else
				Handle(n);
		}
	}

}


相关文章推荐

PC110706 Smith Number //数论 枚举

原题链接 Smith Numbers   ...

poj_1142_Smith Numbers(分解质因子)

Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11030   Accepted: 3...

uva 10042 smith numbers

Problem D: Smith Numbers  Background  While skimming his phone directory in 1982, Al...

分解质因数——Poj 1142 Smith Numbers

Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10627   ...

poj1142Smith Numbers

链接1:http://poj.org/problem?id=1142  链接2:http://openoj.awaysoft.com:8080/judge/contest/view.action?c...
  • JHC23
  • JHC23
  • 2012年08月26日 21:43
  • 377

Smith Numbers(分解质因数)

Smith Numbers Time Limit: 1000ms Memory Limit: 10000KB 64-bit integer IO format: %lld      Java cla...
  • x_y_q_
  • x_y_q_
  • 2016年07月08日 13:01
  • 266

hdu 1333 Smith Numbers(暴力思路)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1333 Smith Numbers Time Limit: 2000/1000 MS (Java...

poj 1142 Smith Numbers

分类:其他 难度:1.5   题意:将一个数各个位的和,与因式分解的每个因数各个位的和,如相等,为smith数,4937775= 3*5*5*65837,4+9+3+7+7+7+5= 42, 3+...

POJ-1142 & HDOJ-1333 Smith Numbers 解题报告

分解质因数题。题意:所谓史密斯数就是说这个数分解成质因数后每一个质因数各个数位上的数字之和相加的结果与这个数各个数位上的数字之和相等,并且这个数不能是质数。现在给你一个不超过8位的数字n,求出大于n的...
  • JZQT_T
  • JZQT_T
  • 2015年01月22日 19:09
  • 469

POJ1142——Smith Numbers

Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12388   ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:110706 Smith Numbers
举报原因:
原因补充:

(最多只允许输入30个字)