zoj3529

本文介绍了一种基于博弈论的游戏算法实现,通过分析一系列正整数,两名玩家轮流选择一个数并将其替换为其正因子之一(但不能是自身),目标是使所有数的乘积变为1。文章详细阐述了如何通过计算每个数的SG值来确定最优策略,并提供了完整的代码实现。

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

题意:两个人博弈,给出n个数,每次可把一个数变成其因子,直到所有为1,无法再变者输。

思路:一个数可以分解成若干质因子乘积,n=p1^x1*p2^x2……pm^xm,则其至多能取的方案数就等于x1+x2+……xm,这就是n的sg值。

例如12=2*2*3,下面给出几个数据

sg[1]=0

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

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

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

sg[12]=mex{sg[1],sg[2],sg[3],sg[6]}=3

可以看出sg[4]跟sg[6]是一样的,4想当于12取掉一个3变成2*2,6相当于12取掉2变成2*3,而4跟6最多还能取掉两个数才会变成1,因此可以得出质因子他们的个数和就是sg值

而题目后半部分,求第几个数,则根据剩余部分的总的异或值是否小于当前数的亦或值,若当前值大于剩余部分的异或值,相当于你可以把当前数,变成与剩余部分异或值相同,使得对手必败

#include<stdio.h>
#include<string.h>
int sg[5000005];
int NIM[100005];
bool isprime[5000005];
int prime[1000000],top;
void init(){
	memset(sg,-1,sizeof(sg));
	sg[1]=0;
	top=0;
	for(int i=2;i<=5000000;i++){
		if(!isprime[i]){
			prime[top++]=i;
			sg[i]=1;
			for(int j=i+i;j<=5000000;j+=i)
				isprime[j]=true;
		}
	}
}
int getsg(int num){
	if(sg[num]!=-1) return sg[num];
	int ans=0,temp=num;
	for(int i=0;prime[i]*prime[i]<=temp;i++)
		while(temp%prime[i]==0){
			ans++;
			temp/=prime[i];
		}
	if(temp!=1)
		ans++;
	return sg[num]=ans;
}
void slove(){
	int n;
	int ans,ti=1;
	while(~scanf("%d",&n)){
		ans=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&NIM[i]);
			ans^=getsg(NIM[i]);
		}
		printf("Test #%d: ",ti++);
		if(ans==0)
			printf("Bob\n");
		else {
			printf("Alice ");
			for(int i=1;i<=n;i++)
				if((ans^sg[NIM[i]])<sg[NIM[i]]){
					printf("%d\n",i);
					break;
				}
		}
	}
}
int main(void){
	init();
	slove();
	return 0;
} 





乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值