石子游戏 II

石子游戏 II

\(Alice\)\(Bob\) 正在玩一个关于石头的游戏。

共有 \(n\) 堆石头,其中第 \(i\) 堆最初含有 \(a_i\) 个石子。

他们轮流执行下列操作之一,从 \(Alice\) 开始。

  • 把一堆奇数的石头劈成两堆,两堆都不能空。
  • 把两堆偶数的石头合成一堆。

不能执行任何操作的人将输掉游戏。

假设 \(Alice\)\(Bob\) 都足够聪明,你知道谁会赢得游戏吗?

输入格式

第一行包含一个整数 \(n\) (\(1\leq n \leq 10^6\))

第二行包含 \(n\) 个正整数 \(a_1,\dots,a_n\) (\(1\leq a_1,\dots,a_n \leq 10^9\))

输出格式

AliceBob,表示最终赢家

样例输入

2
2 2

样例输出

Alice

题目分析

ans为操作次数结果。

ans=(偶数数量-1)+奇数数量

奇数数量分解成非1的奇数,会贡献两次ans,不影响取模结果,因此把奇数分解到偶数上即可。

#include<bits/stdc++.h>
using namespace std ;
int main() {
	int n, ans=0,even=0;
	scanf("%d",&n);
	int t;
	while(n--) {
		scanf("%d" ,&t) ;
		if(t==1) continue ;
		even++;
		ans+=t%2;
	}
	ans+=even;
	if(even) 
		ans--;
	if(ans%2) 
		printf("Alice");
	else printf("Bob");
}

posted @ 2022-04-19 19:23  seekerHeron  阅读(82)  评论(0)    收藏  举报