1 #include <iostream> 2 #include <sstream> 3 #include <string> 4 #include <algorithm> 5 #include <vector> 6 #include <list> 7 using namespace std; 8 9 #define GI ({int _t; scanf("%d", &_t); _t;}) 10 #define FOR(i, a, b) for (int i=a; i<b; i++) 11 #define REP(i, a) FOR(i, 0, a) 12 template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();} 13 int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;} 14 #define DBG(x) cout << #x << "::" << x << endl; 15 #define DBGV(_v) { REP(_i, _v.size()) { cout << _v[_i] << "\t";} cout << endl;} 16 17 int josephus_winner(int n, int m) { 18 // cout << n << "\t" << m << endl; 19 int res = 0; 20 vector <int> people; 21 REP(i, n) { people.push_back(i+1); } 22 int loc = 0; 23 while (people.size() > 1) { 24 if (loc >= people.size()) loc %= people.size(); 25 // cout << "Killing " << *(people.begin()+loc) << " ...\n"; 26 people.erase(people.begin()+loc); 27 loc += (m-1); 28 } 29 // cout << n << "\t" << m << "\t" << people[0] << endl; 30 return people[0]; 31 } 32 33 int main() { 34 int num; 35 while (1) { 36 num = GI; 37 if (num == 0) break; 38 FOR(i, 1, num+1) { 39 int winner = josephus_winner(num, i); 40 if (winner == 13) { 41 printf("%d\n", i); 42 break; 43 } 44 } 45 } 46 return 0; 47 }