Idea: Mathematics + tabulation
Analysis:
1. The traditional Joseph question is given n people and m. The current person is kicked out every m times and the number of the person left behind
2. The first k are good people, and the last k are bad people. Now the smallest m is required so that k bad guys are kicked out when no good guy is kicked out.
3. According to the traditional method, the number of n persons ranges from 0 ~ N-1
The first time a [1] = (m-1) % n; // The number of the person is 0 ~ N-1
Second a [2] = (a [1] + m-1) % (n-1 );
I times a [I] = (a [I-1] + m-1) % (n-I + 1 );
So we can know the number of the person to be deleted each time. Since k is up to 14, we can first create a table to find 1 ~ 14, and then output.
Code:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int solve(int k){ int pre; int tmp = k+1; int n = 2*k; while(tmp){ if((tmp-1)%n >= k && (tmp-1)%n < 2*k){ pre = (tmp-1)%n; int i; for(i = 2 ; i <= k ; i++){ int x = (pre+tmp-1)%(n-i+1); if(x < k || x >= 2*k) break; else pre = x; } if(i == k+1) return tmp; } tmp++; }}int main(){ int k , ans[20]; for(int i = 1 ; i < 15 ; i++) ans[i] = solve(i); while(scanf("%d" , &k) && k) printf("%d\n" , ans[k]); return 0;}