-
Notifications
You must be signed in to change notification settings - Fork 2
/
Q10042.cpp
79 lines (69 loc) · 1.19 KB
/
Q10042.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
#include <stdio.h>
#define MAX 39000
int len,prime[20000];
bool notp[MAX+10];
void pprime() {
int i,j,k;
notp[0] = notp[1] = 1;
for(i=4;i<=MAX;i+=2)
notp[i] = 1;
for(i=9;i<=MAX;i+=3)
notp[i] = 1;
for(i=5,k=2;i*i<=MAX;i+=k,k=6-k)
if(notp[i] == 0) {
for(j=i*i;j<=MAX;j+=i)
notp[j] = 1;
}
len = 2;
prime[0] = 2,prime[1] = 3;
for(i=5,k=2;i<=MAX;i+=k,k=6-k)
if(notp[i] == 0)
prime[len++] = i;
}
int go(int x) {
int sum = 0;
while(x > 0) {
sum += x%10;
x /= 10;
}
return sum;
}
bool smith(int n) {
int i,j,sum1,sum2,tmp;
int t;
sum1 = sum2 = 0;
tmp = n;
while(tmp > 0) {
sum1 += tmp%10;
tmp /= 10;
}
tmp = n;
for(i=0;i<len && prime[i] <= n/ prime[i];i++)
if(n%prime[i] == 0) {
tmp = 1;
break;
}
if(tmp == n) return 0;
tmp = n;
for(i=0;i<len && prime[i] <= n/prime[i];i++) {
t = go(prime[i]);
while(tmp % prime[i] == 0) {
sum2 += t;
tmp /= prime[i];
}
}
if(tmp > 1)
sum2 += go(tmp);
return sum1 == sum2;
}
int main() {
int t,n;
pprime();
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(n++;smith(n) == 0;n++);
printf("%d\n",n);
}
return 0;
}