forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10015.cpp
59 lines (52 loc) · 776 Bytes
/
10015.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
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++)
#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++)
bool is_prime(int n)
{
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
{
return false;
}
return true;
}
static vector<int> primes;
inline int survivor(int n)
{
int i, s;
for (s = 0, i = 1; i <= n; i++)
{
s = (s + primes[n - i]) % i;
}
return s + 1;
}
static int n;
int main()
{
primes.push_back(2);
for (int i = 3; i < 100000; i += 2)
{
if (is_prime(i))
{
primes.push_back(i);
}
}
while (scanf("%d", &n))
{
if (!n)
{
break;
}
printf("%d\n", survivor(n));
}
return 0;
}