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 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 博弈好题的更多相关文章
- 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 ...
- zoj 3529 A Game Between Alice and Bob 博弈论
思路:每个数的SG值就是其质因子个数,在进行nim博弈 代码如下: #include<iostream> #include<cstdio> #include<cmath& ...
- Zoj 3529 A Game Between Alice and Bob 数论+博弈Nim 快速求数中有多少个素数因子
本题涉及博弈论中的Nim游戏博弈. Nim游戏博弈详解链接: http://www.cnblogs.com/exponent/articles/2141477.html 本题解题报告详解链接: htt ...
- UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)
题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...
- HDU 4111 Alice and Bob (博弈+记忆化搜索)
题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...
- ACdream 1112 Alice and Bob (博弈&&素数筛选优化)
题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x ...
- BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)
转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...
- hdu4111 Alice and Bob
Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- ZOJ 3757 Alice and Bob and Cue Sports(模拟)
题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白 ...
随机推荐
- 在 MVC6 中创建 Web API
ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ...
- C# 获取与解析枚举类型的 DescriptionAttribute
原文:C# 获取与解析枚举类型的 DescriptionAttribute System.ComponentModel.DescriptionAttribute 这个 Attribute,经常被用来为 ...
- JavaScript中的单引号和双引号报错的解决方法
在使用JavaScript显示消息或者传递字符数据的时候,经常会碰到数据中夹杂单引号(')或者双引号("),这种语句往往会造成JavaScript报错.对此一般采用/'或者/"的解 ...
- Require.JS 2.0
就在前天晚上RequireJS发布了一个大版本,直接从version1.0.8升级到了2.0.随后的几小时James Burke又迅速的将版本调整为2.0.1,当然其配套的打包压缩工具r.js也同时升 ...
- Chrome控制台 JS调试
Chrome控制台 JS调试的一些小技巧 $ $_命令返回最近一次表达式执行的结果,功能跟按向上的方向键再回车是一样的,但它可以做为一个变量使用在你接下来的表达式中. $0~$4则代表了最近5个你选择 ...
- C#使用COM搜索本地word文档关键字
/// <summary> /// 检索根目录下的子目录及其所有文件,并在datagridview中显示文档名称及路径--递归调用 /// </summary> /// < ...
- 常见ActiveX控件下载大全
ActiveX是微软对于一系列策略性面向对象程序技术和工具的称呼,ActiveX控件可以在Windows窗体和Web程序上使用,所以不管是什么语 言开发的应用程序只要在windows窗体和html页面 ...
- rpt水晶报表制作过程
原文:rpt水晶报表制作过程 最近公司安排一个以前的项目,里面需要用到水晶报表,由于原来做这个项目的同事离职,所在公司的同事报表做成了rdlc类型的,而这类报表在加载的时候很难动态的从数据库加载数据, ...
- jQuery插件的编写相关技术 设计总结和最佳实践
原文:http://www.itzhai.com/jquery-plug-in-the-preparation-of-related-technical-design-summary-and-best ...
- Apache Httpd Server 2.2升级2.4
Apache Httpd Server 2.2升级2.4 (2 votes, average: 5.00 out of 5) 2,302 views 2012 年 3 月 20 日Web服务器.服务器 ...