X - A Game Between Alice and Bob

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

题意:Alice和Bob两个人玩游戏,最开始有n个数,Alice先手。可以选一个数将其变为约数,谁先将数字的乘积变为1,谁就赢。

我们要仔细想一下,每个数的后继是多少个呢,就是它们的约数的个数。就可以转化为NIM游戏了,
然后异或起来就可以了。

对任意的一个局面,如果A1 xor A2 xor … An=x!=0,设x的二进制表示下最高位的1在第
k位,那么至少存在一堆石子Ai,它的第k位是1,显然Ai xor x<Ai,我们就从Ai堆中取走若干石子

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int  N=1e5+10;
const int MAXN=5e6+10;
int a[N];
int prime[MAXN],cnt;
bool isprime[MAXN];
int n;
int sg[MAXN];
bool vis[MAXN],x;
void init()
{
  memset(isprime,true,sizeof(isprime));
  for(int i=2;i<MAXN;i++)
  {
    if(isprime[i])
    {
      prime[cnt++]=i;
      for(int j=2;j*i<MAXN;j++) isprime[i*j]=false;
    }
  }
}
int  get_sg(int x)
{
  int tmp=x;
//  cout<<x<<"+++++"<<endl;
  int res=0;
  if(sg[x]!=-1) return sg[x];  //已经分解过后继
  sg[x]=0;
  for(int i=0;i<cnt&&prime[i]*prime[i]<=x;i++)
  {
    while(x%prime[i]==0)
    {
      x/=prime[i];
      res++;
    }
  }
  if(x>1)  res++;
  return sg[tmp]=res;
}
int main()
{
  init(); 
  int k=0;
  memset(sg,-1,sizeof(sg));
  while(~scanf("%d",&n))
  {
      memset(sg,-1,sizeof(sg));
    int ans=0;
    for(int i=0;i<n;i++)
    {
      scanf("%d",&a[i]);
      ans^=get_sg(a[i]);
    }
    if(ans==0 ) printf("Test #%d: Bob\n",++k);
    else
    {
      for(int i=0;i<n;i++)
      {
        if((ans^get_sg(a[i]))<get_sg(a[i]))
        {
          printf("Test #%d: Alice %d\n",++k,i+1);
          break;
        }
      }
    }
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值