forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10852.cpp
57 lines (53 loc) · 784 Bytes
/
10852.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
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n)
{
for (int i = 3, sq = sqrt(n); i <= sq; i += 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
struct result
{
int p;
int diff;
};
int main()
{
int t, n;
vector<int> primes;
result best;
primes.push_back(2);
for (int i = 3; i < 15000; i += 2)
if (is_prime(i))
{
primes.push_back(i);
}
cin >> t;
while (t--)
{
cin >> n;
best.diff = 0;
for (int i = 0, sz = primes.size(); primes[i] <= n; i++)
{
for (int p = 0; p * primes[i] <= n; p++)
{
if ((p + 1) * primes[i] <= n)
{
continue;
}
if (n - primes[i] * p > best.diff)
{
best.p = primes[i];
best.diff = n - primes[i] * p;
}
}
}
cout << best.p << endl;
}
return 0;
}