ZOJ 3529 - A Game Between Alice and Bob

Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%lld
& %llu

Description

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

题目大意:

给定n个数字。两个人轮流玩游戏。把这n个数字变成n的因子(不包括本身),最后全变成1就赢了,问你谁会赢,假设Alice赢了,把第一步选择的那一个数字的序号输出,假设有多种方案,输出序号小的。

解题思路:

sg[1]=0;

sg[2]=mex{sg[1]}=1;

sg[3]=mex{sg[1]}=1;

sg[4]=mex{sg[2],sg[1]}=2;

sg[5]=mex{sg[1]}=1;

sg[6]=mex{sg[2],sg[3]}=2;

sg[7]=mex{sg[1],sg[7]}=1;

sg[8]=mex{sg[1],sg[2],sg[4]}=3;

..........................

发现 sg[x]为 x因子的个数。

证明:由于 假设一个为a, 则:sg[x]=sg[(x/a)*a]=mex{sg[1]....,sg[(x/a)]}=sg[(x/a)]+1;

求出每一个数字的sg后,仅仅须要看sg的异或和,假设==0 和明显,输出Bob

否则。输出Alice,可是要输出Alice先走了哪一步,这里实用到了一个性质:仅仅须要用合并后的SG值与每一堆SG值分别异或,看得到的结果是否小于原来该堆的SG值。假设小则能够取该堆。

这个性质临时不是非常懂,先用上,以后了解了会给出证明和过程。

解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std; const int maxn=5000010;
int n,sg[maxn];
int d[110000]; vector <int> prime;
bool isprime[maxn]; void ini(){
isprime[2]=true;
for(int i=3;i<maxn;i+=2) isprime[i]=true;
for(int i=3;i<maxn;i+=2){
for(int j=i;j<maxn/i;j+=2){
isprime[i*j]=false;
}
}
for(int i=2;i<=maxn;i++){
if(isprime[i]) prime.push_back(i);
}
} int SG(int x){
if(sg[x]!=-1) return sg[x];
int ret=0,size=prime.size(),tmp=x;
for(int i=0;i<size && prime[i]*prime[i]<=x;i++){
while(x%prime[i]==0){
x/=prime[i];
ret++;
}
}
if(x>1) ret++;
return sg[tmp]=ret;
} int main(){
ini();
memset(sg,-1,sizeof(sg));
int casen=0;
while(scanf("%d",&n)!=EOF){
int ans=0;
for(int i=0;i<n;i++){
scanf("%d",&d[i]);
ans^=SG(d[i]);
}
if(ans==0) printf("Test #%d: Bob\n",++casen);
else{
for(int i=0;i<n;i++){
if( ( ans^SG(d[i]) ) < SG(d[i]) ){
printf("Test #%d: Alice %d\n",++casen,i+1);
break;
}
}
}
}
return 0;
}

ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)的更多相关文章

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

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

  2. ZOJ 3529 A Game Between Alice and Bob 博弈好题

    A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play t ...

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

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

  4. hdu 4111 Alice and Bob 博弈论

    这里有2种方法: 方法一:求SG函数 sg[i][j]:i表示1的个数,j表示合并操作的步数. 这共有4种操作: 1.消除一个1: 2.减掉一个1: 3.合并2个1: 4.把1合并到另外不是1中. 代 ...

  5. ZOJ 3057 Beans Game 博弈论 sg函数

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3057 典型的sg函数,数据范围卡得真好啊 代码 #include<c ...

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

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

  7. 2014 Super Training #6 A Alice and Bob --SG函数

    原题: ZOJ 3666 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 博弈问题. 题意:给你1~N个位置,N是最 ...

  8. 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. bzoj4730: Alice和Bob又在玩游戏

    Description Alice和Bob在玩游戏.有n个节点,m条边(0<=m<=n-1),构成若干棵有根树,每棵树的根节点是该连通块内编号最 小的点.Alice和Bob轮流操作,每回合 ...

随机推荐

  1. 你知道hover、active这四个伪类为什么要按顺序写吗

    刨根问底,你知道:hover等4个伪类为什么要按顺序排列吗 引言 :link,:visited,:hover,:active这4个伪类大家都不陌生,4个伪类要按照LvHa这个爱恨原则来排(外国友人起的 ...

  2. 探究绑定事件的this指向以及event传参的小问题

    this指向 直接上代码,凑合着看 <input type="button" onclick="page()" value="点我"& ...

  3. android中关于ListView的卡位说明

    需求:做项目的时候,需要将ListView的header的一个Tab卡位,即listView滑动后,Header的某一个部分可以定在ListView的顶端卡住. 解决的方案:监听ListView的滑动 ...

  4. Xtrabackup全量备份与恢复mysql数据库

    一.Xtrabackup简单概述: Percona  Xtrabackup是开源免费的MySQL数据库热备份软件,它能对InnoDB和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同 ...

  5. 如何查看VS中预设的路径变量

    类似"$(VCInstallDir)"之类的变量查询方法为:打开VS命令行提示窗口,输入 Set 命令. VS中“Tool” - “Visual Studio Command Pr ...

  6. dump json 显示中文问题

    [root@dr-mysql01 ~]# cat a2.pl my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone ...

  7. 高焕堂《android从程序员到架构师之路》 YY讲坛直面大师学习架构设计

    <android从程序员到架构师之路>YY讲坛活动:  sundy携手高焕堂老师全程YY答疑 与大师一起,分享android技术 时间:7月21日下午2:00   报名联系QQ:22243 ...

  8. 求助(VC++) 隐藏Console窗体无效

    [逝去的100~~ 2014/10/07 20: 20] 程序想要实现控制台窗体的隐藏,可是窗体每次执行总会弹出来.为什么呢? 代码例如以下: // Mini.cpp : 定义控制台应用程序的入口点. ...

  9. 我的Android开发相关文�

    Pro Android学习笔记: Pro Android学习笔记(一一七):Location(3):获取位置更新 2014.8.25 Pro Android学习笔记(一一六):Location(2): ...

  10. 恢复PasswordChar 默认值、取消密码框设置

    //三种都是清空 this.textBox1.PasswordChar = new char(); this.textBox1.PasswordChar = '\0'; this.textBox1.P ...