现在的位置: 首页 > 综合 > 正文

Zoj 3529 A Game Between Alice and Bob

2018年01月15日 ⁄ 综合 ⁄ 共 1528字 ⁄ 字号 评论关闭

A Game Between Alice and Bob


Time Limit: 5 Seconds     
Memory Limit:
262144 KB


Alice and Bob play the following game. A series of numbers is written on the blackboard. Alice and Bob take turns choosing one of the numbers, and replace it with one of its positive factor but not itself. The one who makes the product of all numbers become
1 wins. You can assume Alice and Bob are intelligent enough and Alice take the first turn. The problem comes, who is the winner and which number is Alice's first choice if she wins?

Input

This problem contains multiple test cases. The first line of each case contains only one number
N (1<= N <= 100000) representing there are N numbers on the blackboard. The second line contains
N integer numbers specifying the N numbers written on the blackboard. All the numbers are positive and less than or equal to 5000000.

Output

Print exactly one line for each test case. The line begins with "Test #c: ", where
c indicates the case number. Then print the name of the winner. If Alice wins, a number indicating her first choice is acquired, print its index after her name, separated by a space. If more than one number can be her first choice, make the index
minimal.

Sample Input

4
5 7 9 12
4
41503 15991 72 16057 

Sample Output

Test #1: Alice 1
Test #2: Bob

SG函数

#include<cstdio>
#include<cstring>

int SG[5100000];

int num[110000];

void fun()
{
	long long i,j,tmp;
	for(i=2;i<=5000000;i++)
	{
		if(SG[i]==0)
		{
			for(j=i;j<=5000000;j+=i)
			{
				tmp=j;
				while(!(tmp%i) && tmp)
				{
					SG[j]++;
					tmp/=i;
				}
			}
		}
	}
}

int main()
{
	fun();

	int flag,N,t=1,i;
	while(scanf("%d",&N)==1)
	{
		flag=0;
		for(i=1;i<=N;i++)
		{
			scanf("%d",&num[i]);
			flag^=SG[num[i]];
		}
		printf("Test #%d: ",t++);

		if(flag)
		{
			printf("Alice ");
			for(i=1;i<=N;i++)
			{
				if(SG[num[i]]>(flag^SG[num[i]]))
				{
					printf("%d\n",i);
					break;
				}
			}
		}
		else
			printf("Bob\n");
	}
	return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.