Light OJ 1296:Again Stone Game(SG函数打表找规律)

本文分析了一种石子游戏的策略,游戏由Alice和Bob参与,目标是在遵循特定规则下赢得游戏。通过观察和理解游戏规则,我们发现了当石子数量为偶数时的简化策略,以及如何将奇数石子堆转换为偶数堆进行处理,从而预测胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they alternate moves. In each move, a player has to select any pile and should remove at least one and no more than half stones from that pile. So, for example if a pile contains 10 stones, then a player can take at least 1 and at most 5 stones from that pile. If a pile contains 7 stones; at most 3 stones from that pile can be removed.

Both Alice and Bob play perfectly. The player who cannot make a valid move loses. Now you are given the information of the piles and the number of stones in all the piles, you have to find the player who will win if both play optimally.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1000). The next line contains n space separated integers ranging in [1, 109]. The ith integer in this line denotes the number of stones in the ith pile.

Output

For each case, print the case number and the name of the player who will win the game.

Sample Input

5

1

1

3

10 11 12

5

1 2 3 4 5

2

4 9

3

1 3 9

Sample Output

Case 1: Bob

Case 2: Alice

Case 3: Alice

Case 4: Bob

Case 5: Alice

题意

有n堆石子,每堆ai个,Alice和Bob两人取石子,可以任选一堆取,每次至少一个至多ai/2个(奇数情况向下取整)。最后不能取石子的人失败

思路

数据范围1e9,肯定不能打表计算,但是可以打表来找一下规律。最后会发现,如果ai是偶数的话,可以把这堆石子看成ai/2个石子进行取,如果是奇数,就让ai一直除以2,至到找到偶数,然后再按照石子数是偶数的情况处理一下就可以了

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int SG[1005], mex[1005];
int sg[maxn];
int vis[maxn];
int a[maxn];
inline void getSG()
{
	ms(sg);
	for(register int i=1;i<=maxn;i++)
	{
		ms(vis);
		for(int j=1;2*j<=i;j++)
			vis[sg[i-j]]=1;
		for(register int j=0;j<=maxn;j++)
		{
			if(!vis[j])
			{
				sg[i]=j;
				break;
			}
		}
		cout<<i<<"======="<<sg[i]<<endl;
	}
}
int main(int argc, char const *argv[])
{
	// getSG();
	ios::sync_with_stdio(false);
	register int t;
	register int _=0;
	register int n;
	cin>>t;
	while(t--)
	{
		register int sum=0,x;
		cin>>n;
		while(n--)
		{
			cin>>x;
			while(x%2!=0)
				x/=2;
			x/=2;
			sum^=x;
		}
		cout<<"Case "<<++_<<": ";
		if(sum)
			cout<<"Alice"<<endl;
		else
			cout<<"Bob"<<endl;
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/Friends-A/p/10324351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值