hoj1017 Joseph's problem II

/*This Code is Submitted by billforum for Problem 1017 at 2012-02-17 22:42:30*/
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <string.h>
using namespace std;
int a[19];
int main()
{
    int k,m,ans;
    a[10]=93313;
    a[11]=459901;
    a[12]=1358657;
    a[13]=2504881;
    a[14]=13482720;
  while(cin>>k)
    {
        if(k==0) break;
        else if(k>=10) cout<<a[k]<<endl;
        else
        {
        for(m=1;;m++)
        {
            for(ans=0;ans<k;ans++)
            {
                int j=ans;
                for(int i=k+1;i<=2*k;i++)
                j=(j+m)%i;
                if(j>=k) break;
            }
            if(ans==k)
            {
                cout<<m<<endl;
                break;
            }
        }
        }

    }
    return 0;
}

 

posted @ 2012-02-17 22:44  wuzhibin  阅读(416)  评论(0编辑  收藏  举报