forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10042.cpp
89 lines (85 loc) · 1.09 KB
/
10042.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n)
{
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3, sq = sqrt(n); i <= sq; i += 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
int digitsum(int n)
{
int result = 0;
while (n)
{
result += n % 10;
n /= 10;
}
return result;
}
int main()
{
int t, n, tmp;
int sum1, sum2;
vector<int> primes;
primes.push_back(2);
for (int i = 3; i < 35000; i += 2)
{
if (is_prime(i))
{
primes.push_back(i);
}
}
cin >> t;
while (t--)
{
cin >> n;
while (++n)
{
if (is_prime(n))
{
continue;
}
sum1 = digitsum(n);
tmp = n;
sum2 = 0;
for (int i = 0, sz = primes.size(); i < sz; i++)
{
if (tmp == 1 || is_prime(tmp))
{
if (tmp != 1)
{
sum2 += digitsum(tmp);
}
break;
}
if (tmp % primes[i] == 0)
{
while (tmp % primes[i] == 0)
{
sum2 += digitsum(primes[i]);
tmp /= primes[i];
}
}
}
if (sum1 == sum2)
{
cout << n << endl;
break;
}
}
}
return 0;
}