ZOJ 3529 A Game Between Alice and Bob(SG)

                                                                         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

Author: ZHOU, Yuchen
Contest: ZOJ Monthly, September 2011

Submit    Status

 

题意:给n个数,每次可以把一个数变成它的因子,当全部变成1的时候游戏结束,无法再进行的人输,判断先手的胜负,如果先手能赢,输出她最先拿的那一堆石头的下标

一开始想着求出所有因子再进行SG的计算,但是超时,写出几个SG值可以发现,一个数的SG值就是它的因子个数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=5000010;
int num[maxn];
int ldj[maxn];
void init()
{
	for(int i=2;i<=5000000;i++)
	{
		if(num[i]==0)
		{
			for(int j=i;j<=5000000;j+=i)
			{
				int tmp=j;
				while(!(tmp%i)&&tmp)
				{
					num[j]++;
					tmp/=i;
				}
			}
		}
	}
	return;
}
int main()
{
	num[1]=0;
	init();
	int cas=1;
	int n;
	while(~scanf("%d",&n))
	{
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&ldj[i]);
			ans^=num[ldj[i]];
		}
		printf("Test #%d: ",cas++);
		if(ans)
		{
			int res;
			for(int i=1;i<=n;i++)
			{
				if(num[ldj[i]]>(ans^(num[ldj[i]])))
				{
					res=i;
					break;
				}
			}
			printf("Alice %d\n",res);
		}
		else puts("Bob");
	}

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值