bnuoj eeny meeny moo

Problem Description

There is a computer classroom. We all know that if there are lots of people surf the net at the same time, the speed will be very slow. Now, the administrator of the classroom will turn off all computers in certain rule. There are N computers marked with number 1,2,…,N. The administrator will first turn off the computer No.1, then he will turn off the computer who is M after than the previous one.(The computer)
E.G.
if there are 5 computers and M=3, the turning off order is:
1 4 3 5 2

if M=2, the order is:
1 3 5 4 2

Now,there are N computers, AD1024 is using the computer No.2, and he wants to know the minimum value of M such that AD1024’s computer can be the last computer which will be turned off.

Problem Analysis

The problem uses the concept of Joseph Circle. And there is a formula of the problem:
Provide k will be turned off in ith term, (k+N)%i will be turned off in the next term.If know this formula, the problem is much easier. Because we just need to enumerate the value of M. Then output the first value of M that fits the problem.

I’ve to say that it is so mathematical…..

##AC CODE

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;


int main(){
    int N;
    while(cin>>N){
        if(N==0) break;
        int M=1;
        int s=0;
        while(1){
            s=0;
            for(int i=2;i<=N-1;i++){
                s=(s+M)%i;
            }
            if(s==0) break;
            M++;
        }
        cout<<M<<endl;
    }
    return 0;
}