ZOJ 3529 A Game Between Alice and Bob (分解质因数+Nim博弈)

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

 

题目的意思就是Alice和Bob玩游戏,从N个数字中,每次每个人选一个数字然后把这个数字变成它的一个因子,最后使得所有乘积为1的那个人获胜。

这道题确实一开始看的时候不好下手,但是想到每个数可以替换成它的因子,那么一个数最多变换的次数就是它的质因子的个数,然后每个数就可以转换成质因子的乘积

每次变换成这个数的因子就相当于从这些质因子中除去某些质因子,这么处理一下之后,这道题不就成了一个Nim博弈了嘛= =

每个数相当于一堆石子,质因子的个数相当于是每一堆的石子的数量,把这个数除到1相当于把所有的石子拿走,就是一个裸Nim了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<ctype.h>
#define LL __int64
using namespace std;
const int MAXN=100000+50;
const int MAX=50000+50;
const int INF=0x3f3f3f3f;
const double EPS=1e-9;
int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}};
int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int n,a[MAXN],prime[MAX],isprime[MAX];
void init()//筛选MAXN范围以内的素数
{
    int sum=0;
    for(int i=2;i<MAX;i++)
    {
        if(!isprime[i])
        {
            prime[sum++]=i;
            for(int j=i;j<MAX;j+=i)
                isprime[j]=1;
        }
    }
}

int change(int num)//分解质因子
{
    int ans=0;
    for(int i=0;prime[i]*prime[i]<=num;i++)
    {
        if(num%prime[i]==0)
        {
            while(num%prime[i]==0)
            {
                num/=prime[i];
                ans++;
            }
        }
    }
    if(num>1) ans++;//考虑到可能是大于MAX的质数
    return ans;
}
int main()
{
    init();
    int cnt=0;
    while(scanf("%d",&n)!=EOF)
    {
        int sum=0,temp;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&temp);
            a[i]=change(temp);
            sum^=a[i];
        }

        if(sum==0) printf("Test #%d: Bob\n",++cnt);
        else
        {
            printf("Test #%d: Alice ",++cnt);
            for(int i=1;i<=n;i++)
                if((sum^a[i])<a[i])
                {
                    printf("%d\n",i);
                    break;
                }
        }
    }
    return 0;
}

时间: 2024-12-08 22:45:54

ZOJ 3529 A Game Between Alice and Bob (分解质因数+Nim博弈)的相关文章

ZOJ 3666 Alice and Bob (SG博弈)

题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 题意: 给一个有向图,然后A和B轮流移动棋子,棋子在每一个位置可以重叠,当某人不能走时,输! 问A和B谁赢 方法: 显然每一局游戏都是独立的,对每一局游戏异或即可 每一局游戏的结果可以用SG求,记忆化搜索之 1 int dfs(int x) 2 { 3 if (sg[x] != -1) return sg[x]; 4 bool vis[maxn]; 5 me

ZOJ 3757 Alice and Bob and Cue Sports(模拟)

题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白球落入洞中,要把白球拿出来放在桌子上,如果是其他的球就不拿哪怕是犯规打进去的.每打一局(每一局代表每人打一杆)时当发生以下三种行为时算是犯规,会将相应的罚分加给对方,自己不减分. 白球没有打中任何球.将目标球的数值加到对方的分数上. 白球没有入洞且至少打中一个球,一开始没有打中目标球或者一开始同时打

codeforces_346A Alice and Bob(数学)

题目链接:http://codeforces.com/problemset/problem/346/A 参考链接:http://blog.csdn.net/loy_184548/article/details/50174615 感受到数学在博弈论中的强大. 考虑最后终止状态的序列-无法取出任意两个数他们的差值不存在这个序列中:那么这必定是个首项等于公差的等差序列 而这个序列是 d 2d 3d....,因此可以通过a[1] a[2] a[3] ...的最大公约数得到 然后计算有几个数没在数组中,判

HDU4268 Alice and Bob 【贪心】

Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2869    Accepted Submission(s): 926 Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this

博弈问题-Alice与Bob拿牌游戏

Description Bob and Alice play a game, and Bob will play first. Here is the rule of the game: 1) There are N stones at first; 2) Bob and Alice take turns to remove stones. Each time, they can remove p^k stones. p is prime number, such as 2, 3, 5, ...

Alice and Bob

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a se

HDU 4111 Alice and Bob (博弈)

Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1799    Accepted Submission(s): 650 Problem Description Alice and Bob are very smart guys and they like to play all kinds of games i

HDU 4268 Alice and Bob(贪心+multiset)

HDU 4268 题意:Alice与Bob在玩卡片游戏,他们每人有n张卡片,若Alice的一张卡片长与宽都不小于Bob的一张卡片,则Bob的卡片就会被盖住,一张卡片只可以使用一次,且不可旋转求Alice最多可以盖住多少张Bob的卡片. 思路:记录两人卡片情况,并按照长度将两人卡片分别降序排序.遍历两人的卡片,将长度小于Alice的卡片长度的Bob卡片的宽度插入multiset中,在multiset中找到小于等于Alice卡片宽度的第一个数,将这个数给消去且答案+1.//贪心法自行发挥即可. co

HDU 4268 Alice and Bob(贪心+Multiset的应用)

 题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形可以覆盖另一个矩形的条件的是,本身长度大于等于另一个矩形,且宽度大于等于另一个矩形,矩形不可旋转,问你Alice最多能覆盖Bob的几个矩形? 思路:贪心,先按照h将Alice和Bob的矩形排序,对于Alice的每个矩形,如果Bob的矩形的h小于Alice的h,将Bob的w插入到集合中. 然后,在集合中找到不大于Alice矩形d的最大的Bob的d,那么这样做肯定是最优的. #include<cstdio> #include<