Codeforces Round #658 (Div. 2) B. Sequential Nim

There are n piles of stones, where the i-th pile has ai stones. Two people play a game, where they take alternating turns removing stones.

In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Next 2t lines contain descriptions of test cases.

The first line of each test case contains a single integer n (1≤n≤105) — the number of piles.

The second line of each test case contains n integers a1,…,an (1≤ai≤109) — ai is equal to the number of stones in the i-th pile.

It is guaranteed that the sum of n for all test cases does not exceed 105.

Output
For each test case, if the player who makes the first move will win, output “First”. Otherwise, output “Second”.

题意:
两个人玩石子游戏,有这样几堆石子,从第一堆开始取,最多只能拿走这一堆的所有石子,而最少要拿走一颗,不能操作的算输。给出石子的堆数以及每堆石子的个数,两人均使用最佳策略,询问先手胜还是后手胜利。

思路:
简单的博弈论,如果全是1那就看奇偶,反之就看最后谁能先拿到不为1的数谁就赢,因为他能控制下一个人。

#include<bits/stdc++.h>
#define ll long long
using namespace std;    
int main()
{
	ll t,a[100005],x,y,n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int cnt=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			if(a[i]==1)
				cnt++;
		}
		if(cnt==n)
		{
			if(cnt%2==0)
				cout<<"Second"<<endl;
			else
				cout<<"First"<<endl;
		}
		else
		{
			int u;
			for(int i=1;i<=n;i++)
			{
				if(a[i]!=1)
				{
					u=i;
					break;	
				}	
			}
			if(u%2==0)
				cout<<"Second"<<endl;
			else
				cout<<"First"<<endl;
		} 
	}
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

henulmh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值