Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:直接输答案过了……没有想到更好的办法,唯有搜索了。用别的办法的人能否讲一下算法?

Posted by first at 2003-11-17 13:14:40 on Problem 1012
In Reply To:直接输答案过了……没有想到更好的办法,唯有搜索了。用别的办法的人能否讲一下算法? Posted by:carlis at 2003-11-16 14:46:12
竟然能过
#include <iostream>
using namespace std;
int Josephus(int,long,int);
bool ok(int ,long );
int main()
{
	 int M;
	 long ans[14];
	 long sample;
	  for(M=1;M<14;M++)
	  {
		  sample=1;
		  while(!ok(M,sample))
			  sample++;
		  ans[M]=sample;
	  }
	  cin>>M;
	  while(M)
	  {	cout<<ans[M]<<endl;
	    cin>>M;}
	  return 0;
}
int Josephus(int n,long k,int i)
{
	if(i==1)
		return (k-1)%n;
	else
		return (k+Josephus(n-1,k,i-1))%n;
}
bool ok(int M,long sample)
{
    int i;
	for(i=1;i<=M;i++)
       if(Josephus(2*M,sample,i)<M)
		   return false;
	 return true;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator