-
Notifications
You must be signed in to change notification settings - Fork 1
/
UVa 11728.cpp
236 lines (176 loc) · 3.62 KB
/
UVa 11728.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <bits/stdc++.h>
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define rep(i, n) for(int i = 0 ; i < (n) ; i++)
#define iter(i, a, b) for(int i = (a) ; i < (b) ; i++)
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef pair< int , int > pii;
typedef pair< int , pii > iii;
typedef vector<int> vi;
typedef vector< pii > vii;
typedef map<int, int> mii;
#define INF (1 << 29)
#define PI (2.0 * acos(0.0))
#define MAXX 10010
ll _sieve_size;
bitset<MAXX> bs;
vector<int> primes;
void sieve(ll upperbound){
_sieve_size = upperbound + 1;
bs.set();
bs[0] = bs[1] = 0;
for (ll i = 2; i <= _sieve_size; i++){
if (bs[i]){
for (ll j = i * i; j <= _sieve_size; j += i){
bs[j] = 0;
}
primes.push_back((int)i);
}
}
}
bool isPrime(ll N)
{
if (N <= _sieve_size) return bs[N];
for (int i = 0; i < (int)primes.size(); i++)
if (N % primes[i] == 0) return false;
return true;
}
vi primeFactors(ll N){ //N = a^i X b^j X ... X c^k
vi factors;
ll PF_idx = 0, PF = primes[PF_idx];
while(PF * PF <= N){
while(N % PF == 0){
N /= PF;
factors.push_back(PF);
}
PF = primes[++PF_idx];
}
if(N != 1)
factors.push_back(N);
return factors;
}
vii primePowFactors(ll N){
vii powFactors;
ll PF_idx = 0, PF = primes[PF_idx];
while(PF * PF <= N){
ll power = 0;
while(N % PF == 0){
N /= PF;
power++;
}
if(power > 0)
powFactors.push_back(make_pair(PF, power));
PF = primes[++PF_idx];
}
if(N != 1)
powFactors.push_back(make_pair(N, 1));
return powFactors;
}
ll numPF(ll N){
ll PF_idx = 0, PF = primes[PF_idx], ans = 0;
while(PF * PF <= N){
while(N % PF == 0){
N /= PF;
ans++;
}
PF = primes[++PF_idx];
}
if(N != 1)
ans++;
return ans;
}
ll numDiffPF(ll N){
ll PF_idx = 0, PF = primes[PF_idx], ans = 0;
while(PF * PF <= N){
if(N % PF == 0)
ans++;
while(N % PF == 0)
N /= PF;
PF = primes[++PF_idx];
}
if(N != 1)
ans++;
return ans;
}
ll sumPF(ll N){
ll PF_idx = 0, PF = primes[PF_idx], ans = 0;
while(PF * PF <= N){
while(N % PF == 0){
N /= PF;
ans += PF;
}
PF = primes[++PF_idx];
}
if(N != 1)
ans += N;
return ans;
}
ll numDiv(ll N){ //numDiv(N) = (i + 1) X (j + 1) X ... X (k + 1)
ll PF_idx = 0, PF = primes[PF_idx], ans = 1;
while(PF * PF <= N){
ll power = 0;
while(N % PF == 0){
N /= PF;
power++;
}
ans *= (power + 1);
PF = primes[++PF_idx];
}
if(N != 1)
ans *= 2;
return ans;
}
ll sumDiv(ll N){ //sumDiv(N) = ( (a^(i + 1) - 1 ) / (a - 1) ) X ( (b^(j + 1) - 1 ) / (b - 1) ) X ... X ( (c^(k + 1) - 1 ) / (c - 1) )
ll PF_idx = 0, PF = primes[PF_idx], ans = 1;
while(PF * PF <= N){
ll power = 0;
while(N % PF == 0){
N /= PF;
power++;
}
ans *= ((ll)pow((double)PF, power + 1.0) - 1) / (PF - 1);
PF = primes[++PF_idx];
}
if(N != 1)
ans *= ((ll)pow((double)N, 2.0) - 1) / (N - 1);
return ans;
}
ll EulerPhi(ll N){
ll PF_idx = 0, PF = primes[PF_idx], ans = N;
while(PF * PF <= N){
if(N % PF == 0)
ans -= ans / PF;
while(N % PF == 0)
N /= PF;
PF = primes[++PF_idx];
}
if(N != 1)
ans -= ans / N;
return ans;
}
int main(){
//READ("UVa 11728.txt");
sieve(MAXX);
int case_no = 0;
ll solve[1005];
mem(solve, -1);
for(ll i = 1; i < 1005; i++){
if(isPrime(i))
solve[i + 1] = i;
else{
ll x = sumDiv(i);
if(x < 1005)
solve[x] = i;
}
}
int n;
while(1){
scanf("%d", &n);
if(n == 0)
break;
printf("Case %d: %lld\n", ++case_no, solve[n]);
}
return 0;
}