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 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: ", 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

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

题意:

题目大意:给定n个数,每一步都可以将某个数替换为它的因子,但不能替换为本身,两个人轮流走,直到某个人走不了他就输了。问最后谁能赢,如果先手胜输出第一步。n<=10万,每个数<=5000000.

思路:

x = p1^a1*p2^a2...pk^ak,pi为质数,这样每个数都由(a1+a2+..ak)个质数组成,然后就转换成若干堆质数,每次可以取走某堆的某些个质数,拿走一些质数后就变成了其因子。问最后谁取无可取。

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
const int A=5000005;
int prime[3000];
bool use[3000];
int sg[A/10];
int m[100005];
int pos;
int getsg(int j)
{
int ans=0;
for(int i=0;i<pos&&prime[i]*prime[i]<=j;i++)
{
int k=0;
while(j%prime[i]==0)
{
k++;
j/=prime[i];
}
ans+=k;
}
if(j!=1)ans++;
return ans;
} void get_prime()
{
memset(use,0,sizeof(use));
for(int i=2;i<sqrt(A+1.0);i++)
{
if(!use[i])
for(int j=i*2;j<sqrt(A+1.0);j+=i)
{
use[j]=true;
}
}
pos=0;
for(int i=2;i<sqrt(A+1.0);i++)
{
if(!use[i])
prime[pos++]=i;
}
}
int main()
{
get_prime();
int n,t=0;
while(~scanf("%d",&n))
{
t++;
int tp=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&m[i]);
m[i]=getsg(m[i]);
tp^=m[i];
//printf("%d\n",m[i]);
}
if(tp)
{
for(int i=1;i<=n;i++)
if((tp^m[i])<m[i])
{
printf("Test #%d: Alice %d\n",t,i);
break;
}
}
else{
printf("Test #%d: Bob\n",t);
}
} }

参考 woshi250hua

ZOJ 3529 A Game Between Alice and Bob 博弈好题的更多相关文章

  1. ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)

    ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS     Memory Limit:262144KB     64bit IO For ...

  2. zoj 3529 A Game Between Alice and Bob 博弈论

    思路:每个数的SG值就是其质因子个数,在进行nim博弈 代码如下: #include<iostream> #include<cstdio> #include<cmath& ...

  3. Zoj 3529 A Game Between Alice and Bob 数论+博弈Nim 快速求数中有多少个素数因子

    本题涉及博弈论中的Nim游戏博弈. Nim游戏博弈详解链接: http://www.cnblogs.com/exponent/articles/2141477.html 本题解题报告详解链接: htt ...

  4. UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)

    题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...

  5. HDU 4111 Alice and Bob (博弈+记忆化搜索)

    题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...

  6. ACdream 1112 Alice and Bob (博弈&amp;&amp;素数筛选优化)

    题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x ...

  7. BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)

    转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...

  8. hdu4111 Alice and Bob

    Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

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

    题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白 ...

随机推荐

  1. 找呀志_通过开源框架引AsyncHttpClient处理get/post要求

    一个.开源参考架构的方法. 方法一 找到下载的文件的源代码,Com中的src文件夹下 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhp ...

  2. centos下mysql 最新版最终成功安装!备份一下几个关键地方

    我本来仅仅是为了搭建简单的LAMP环境,亲自己主动手,却发现有这么多的问题会发生.(by default7#zbphp.com) 非常多地方给的安装Mysql的提示是通过yum一键安装.shell命令 ...

  3. CSS3 选择器读解

    文章资料来自于W3Cfuns CSS3.0 四个基本的结构性伪类选择器:root 此选择器将绑定到页面的根元素中,所谓根元素,是指文档树中最顶层的元素,也就是<html>部 分. < ...

  4. C#获取本机所有用户名

    using System.DirectoryServices; using System.Runtime.InteropServices; (需要添加引用) [StructLayout(LayoutK ...

  5. android简单的计算器

    所使用的算法:表达式求值(中缀表达式转后缀表达式,后缀表达式求值值) 不如何设计接口,有时间来美化! MainActivity.java package com.example.calculator; ...

  6. openSUSE13.2安装Nodejs并更新到最新版

    软件源中直接安装Nodejs即可 sudo zypper in nodejs 查看nodejs版本 sincerefly@linux-utem:~> node --version v0.10.5 ...

  7. DDD分层架构之领域实体(验证篇)

    DDD分层架构之领域实体(验证篇) 在应用程序框架实战十四:DDD分层架构之领域实体(基础篇)一文中,我介绍了领域实体的基础,包括标识.相等性比较.输出实体状态等.本文将介绍领域实体的一个核心内容—— ...

  8. 【转】浏览器DNS 预取读技术的危害

    今天中午在http://news.ycombinator.com/news看到一篇文章标题: Saved 10 billion DNS queries per month by disabling D ...

  9. Oracle数据库面试题

    1.取出表中第31到40行的记录 mysql方案:select * from t order by id limit 30,10 oracle方案: select t2.* from (select ...

  10. vs2012快速将项目托管到github

    vs2012快速将项目托管到github   在VS2012中使用GitHub 注册GitHub账号(DeanZhouLin) https://github.com/ 向GitHub中添加一个仓库(T ...