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:终于不打表AC,282ms,放出代码,求更快的解法……

Posted by Adrianhsm at 2009-08-09 13:50:22 on Problem 1012
In Reply To:终于不打表AC,282ms,放出代码,求更快的解法…… Posted by:alexneko at 2008-07-12 16:14:45
47MS, 关键在于要考虑最后剩下k+1个时,这时起始点可能是待删除的那个或者是第一个,那样的话可能是答案的数只可能是t*(k+1)或者(k+2)+(t-1)*(k+1),这里也要谢谢楼主,如果不是楼主,我还不会用(start+m-1)%N+1这个公式来确定点的位置呢,下面是代码,仅供参考。
// 1012_Joseph.cpp : Defines the entry point for the console application.
//

#include "stdio.h"
#include <memory.h>

const int max = 15;
int number[ max ];

int main()
{
	memset( number, 0 , sizeof(int)*max );
	int N = 0;
	while ( true )
	{
		scanf("%d", &N );
		if ( N == 0 )
		{
			break;
		}
		if ( number[ N ] != 0 )
		{
			printf("%d\n", number[N] );
			continue;
		}
		int i = 0;
		
		int a = N+1;
		int b = N+2;
		int aTemp = a;
		int bTemp = b;
		
		
		while ( true )
		{
			int total = 2*N;
			int start=  0;
			for ( i = 0; i < N; ++i )
			{
				start = (start+aTemp-1)%total+1;
				if ( start <= N )
				{
					break;
				}
				--start;
				--total;
			}
			if ( total == N)
			{
				printf("%d\n", aTemp );
				number[ N ] = aTemp;
				break;
			}
			else
			{
				aTemp += (N+1);
			}
			total = 2*N;
			start=  0;
			for ( i = 0; i < N; ++i )
			{
				start = (start+bTemp-1)%total+1;
				if ( start <= N )
				{
					break;
				}
				--start;
				--total;
			}
			if ( total == N )
			{
				printf("%d\n", bTemp );
				number[ N ] = bTemp;
				break;
			}
			else
			{
				bTemp += (N+1);
			}
		}
	}
	return 0;
}


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