Skip to content

Instantly share code, notes, and snippets.

@mhmoodlan
Created September 28, 2017 06:22
Show Gist options
  • Save mhmoodlan/11805a82fee784a2721ae9db3fba90e6 to your computer and use it in GitHub Desktop.
Save mhmoodlan/11805a82fee784a2721ae9db3fba90e6 to your computer and use it in GitHub Desktop.
#DP #Adhoc #JousephProblem #UVa #Solved
//https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=87
#include <bits/stdc++.h>
#define ll long long
#define sz(v) ((int) ((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define lp(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep(i, v) for(int i = 0; i < sz(v); ++i)
using namespace std;
const int MAX = 15;
const int OO = 1e4;
int cache[105];
int calcM(int n, int cycle) {
cache[0] = cache[1] = 0;
for(int i = 2; i <= n; i++) {
if(i == n) cache[i] = cache[i-1] + 1;
else
cache[i] = (cache[i-1] + cycle) % i;
}
return cache[n];
}
int main() {
int n;
while(cin>>n && n!=0) {
lp(i, 100) {
clr(cache, -1);
calcM(n, i+1);
if(cache[n] == 12) { cout << i+1 << endl; break;}
//cout << cache[n-1] << endl;
}
}
//cout << calcM(7, 3) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment