###### 110706 Smith Numbers

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);
}
}

}


#### programming-challenges Smith Numbers (110706) 题解

2015-08-26 07:38:24

#### 【HDU】 1333 Smith Numbers

2016-05-28 11:08:20

#### 分解质因数——Poj 1142 Smith Numbers

2013-03-31 11:41:11

#### PC/UVa 110706/10042 Smith Numbers

2012-02-29 19:20:46

#### Smith Numbers - PC110706

2014-04-13 16:36:39

#### UVA 10042 Smith Numbers（数论）

2014-07-17 19:42:00

#### 【POJ】1142 - Smith Numbers（容斥原理）

2016-07-22 17:55:28

#### POJ 1142 Smith Numbers【Euler分解质因数】

2016-09-03 23:00:43

#### Smith Numbers（分解质因数）

2016-07-08 13:01:26

#### acm pku 1142 Smith Numberd的模拟实现方法

2010-07-01 20:46:00

## 不良信息举报

110706 Smith Numbers