zoj3529 A Game Between Alice and Bob 质因子个数NIM

这是一个关于Alice和Bob玩的游戏,棋盘上写有一系列数字。他们轮流选择一个数字并用它的正因子(但不能是它自己)替换。当所有数字的乘积变为1时,胜者产生。题目给出多组测试数据,每组包含N个不超过5000000的正数。任务是判断赢家,并在Alice获胜时提供她的初始选择数字的索引。
摘要由CSDN通过智能技术生成
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 numberN (1<=N <= 100000) representing there are N numbers on the blackboard. The second line containsN integer numbers specifying theN 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: ", wherec 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
题意:给你n个数,每次可以用某个数的因子替代这个数,最后谁把所有的数的乘积变为1的时候,谁就取胜。
其实就是这n个数的质因子的个数的NIM游戏。
所以先打一个素数表,然后记忆化搜索保存,这样就不会超时。
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
#define N 5000010
using namespace std;
int a[100010];
int prime[N]={0},num_prime=0;
bool isNotPrime[N];
int sg[N];
void sushu()
{
    for(int i=2;i<N;i++)
    {
        if(!isNotPrime[i])
        prime[num_prime++]=i;
        for(int j=0;j<num_prime&&i*prime[j]<N;j++)
        {
            isNotPrime[i*prime[j]]=1;
            if(!(i%prime[j]))
            break;
        }
    }
}
int solve(int x)
{
       if(sg[x]!=-1)  return sg[x];
       int ans=0;
       int q=x;
       for(int j=0;j<num_prime&&prime[j]*prime[j]<=x;j++)
       {
           while(x%prime[j]==0)
           {
               ans++;
               x/=prime[j];
           }
       }
       if(x>1) ans++;
       return sg[q]=ans;
}
int main()
{
    isNotPrime[0]=isNotPrime[1]=1;
    sushu();
    memset(sg,-1,sizeof(sg));
    int n,num;
    int count=1;
    while(scanf("%d",&n)!=EOF)
    {
        int ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            ans^=solve(a[i]);
        }
        printf("Test #%d: ",count++);
        if(ans==0) puts("Bob");
        else
        {
            for(int i=0;i<n;i++)
            {
                int y=ans^sg[a[i]];
                if(y<sg[a[i]])
                {
                    printf("Alice %d\n",i+1);
                    break;
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值