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;
}
}
}