-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoseph.cpp
130 lines (119 loc) · 3.06 KB
/
joseph.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
/***********************************************************
* @名称: 305 - Joseph
* @作者: Shawn
* @创建时间: 2017-12-05 21:30:38
* @修改人: Shawn
* @修改时间: 2017-12-05 21:30:38
* @备注: wa
* @题目来源: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=241
***********************************************************/
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
//ifstream fin("joseph.in");
//ofstream fout("joseph.out");
int last = 0;
vector<int> results(14, -1);
while (true)
{
int n;
cin >> n;
if (results[n] > -1)
{
cout << results[n] << "\n";
continue;
}
int m;
if (n == 13)
{
m = 2504880;
}
else if (n == 12)
{
m = 1358656;
}
else
{
m = 0;
}
if (n == 0)
{
break;
}
while (true)
{
++m;
vector<int> a(2 * n);
for (int i = 0; i <= 2 * n - 1; ++i)
{
if (i < n)
{
a[i] = 1;
}
else
{
a[i] = 2;
}
}
bool ok = true;
int nowPos = (m - 1) % (n * 2), died = 0;
for (int i = n * 2; i >= 1; --i)
{
if (a[nowPos] == 1)
{
if (died < n)
{
ok = false;
}
break;
}
else if (a[nowPos] == 2)
{
++died;
}
a[nowPos] = 0;
int count = 0;
if (i == 1)
{
break;
}
vector<int> loop;
vector<bool> dp(2 * n, false);
while (count < m)
{
++nowPos;
nowPos %= 2 * n;
if (a[nowPos] > 0)
{
++count;
if (dp[nowPos] == true)
{
nowPos = loop[(m - count) % loop.size()];
break;
}
else
{
loop.push_back(nowPos);
dp[nowPos] = true;
}
}
}
}
if (ok == true)
{
break;
}
}
cout << m << "\n";
last = m;
results[n] = m;
}
return 0;
}