There are [Math Processing Error] piles of stones, where the [Math Processing Error]-th pile has [Math Processing Error] 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 [Math Processing Error]
([Math Processing Error]) — the number of test cases. Next [Math Processing Error] lines contain descriptions of test cases.
The first line of each test case contains a single integer [Math Processing Error]([Math Processing Error]) — the number of piles.
The second line of each test case contains [Math Processing Error]
integers [Math Processing Error] ([Math Processing Error]) — [Math Processing Error] is equal to the number of stones in the [Math Processing Error]-th pile.
It is guaranteed that the sum of [Math Processing Error] for all test cases does not exceed [Math Processing Error] .
Output
For each test case, if the player who makes the first move will win, output “First”. Otherwise, output “Second”.
Example
Input
7
3
2 5 4
8
1 1 1 1 1 1 1 1
6
1 2 3 4 5 6
6
1 1 2 1 2 2
1
1000000000
5
1 2 2 1 1
3
1 1 1
Output
First
Second
Second
First
First
Second
First
Note
In the first test case, the first player will win the game. His winning strategy is:
The first player should take the stones from the first pile. He will take [Math Processing Error] stone. The numbers of stones in piles will be [Math Processing Error].
The second player should take the stones from the first pile. He will take [Math Processing Error]
stone because he can’t take any other number of stones. The numbers of stones in piles will be [Math Processing Error].
The first player should take the stones from the second pile because the first pile is empty. He will take [Math Processing Error]
stones. The numbers of stones in piles will be [Math Processing Error].
The second player should take the stones from the second pile because the first pile is empty. He will take [Math Processing Error]
stone because he can’t take any other number of stones. The numbers of stones in piles will be [Math Processing Error].
The first player should take the stones from the third pile because the first and second piles are empty. He will take [Math Processing Error]
stones. The numbers of stones in piles will be [Math Processing Error].
The second player will lose the game because all piles will be empty.
题意:
两个人玩游戏,每个人拿任意石子,但必须先把第一堆拿完才能拿下一堆,判断谁是最后的赢家!
思路:
当每一堆的石子都为1时,赢家是固定的,没有扭转形势的机会,如果1的个数为奇数,那么先手赢,若是偶数,则是后手赢。
当每一堆的石子并不是全为1,即有不为1的数,当取到第一个不为1的堆的人,它可以改变当前局势,也就是它可以控制自己的输赢!
所以我们要算的第一就是1的个数,第二就是记录第一个不为1的数的堆的序号 代码很简单,题也不是很难,但是可能就是有点不太容易有思路
代码:
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,i,a,f=0,s=0;
scanf("%d",&n);
{
for(i=1; i<=n; i++)
{
scanf("%d",&a);
if(a==1)
s++;
if(a!=1&&f==0)
{
f=i;
}
}
}
if(f!=0)
{
if(f%2==0)
printf("Second\n");
else
printf("First\n");
}
else
{
if(s%2==0)
printf("Second\n");
else
printf("First\n");
}
}
return 0;
}