博弈——Wizards and Numbers

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LSC_333/article/details/78178378

In some country live wizards. They love playing with numbers.

The blackboard has two numbers written on it — a and b. The order of the numbers is not important. Let's consider a ≤ b for the sake of definiteness. The players can cast one of the two spells in turns:

  • Replace b with b - ak. Number k can be chosen by the player, considering the limitations that k > 0 and b - ak ≥ 0. Number k is chosen independently each time an active player casts a spell.
  • Replace b with b mod a.

If a > b, similar moves are possible.

If at least one of the numbers equals zero, a player can't make a move, because taking a remainder modulo zero is considered somewhat uncivilized, and it is far too boring to subtract a zero. The player who cannot make a move, loses.

To perform well in the magic totalizator, you need to learn to quickly determine which player wins, if both wizards play optimally: the one that moves first or the one that moves second.

Input

The first line contains a single integer t — the number of input data sets (1 ≤ t ≤ 104). Each of the next t lines contains two integers ab (0 ≤ a, b ≤ 1018). The numbers are separated by a space.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64d specificator.

Output

For any of the t input sets print "First" (without the quotes) if the player who moves first wins. Print "Second" (without the quotes) if the player who moves second wins. Print the answers to different data sets on different lines in the order in which they are given in the input.

Example
Input
4
10 21
31 10
0 1
10 30
Output
First
Second
Second
First
Note

In the first sample, the first player should go to (11,10). Then, after a single move of the second player to (1,10), he will take 10 modulo 1 and win.

In the second sample the first player has two moves to (1,10) and (21,10). After both moves the second player can win.

In the third sample, the first player has no moves.

In the fourth sample, the first player wins in one move, taking 30 modulo 10.


题意:

两个人玩游戏,给a和b两个数,假设a<=b,两人轮流进行操作,不能进行操作的一方判输,游戏规则如下:

1.不能操作的意思是当前局面如果a或b有一个为0。

2.每个人每回合可操作一次,有两种操作,第一种是用b-a^k代替b,其中k为任意数但k>0且(b-a^k)>=0,第二种是用b%a代替b


思路:

输入不保证a<=b, 所以如果a>b就先交换ab。由题意知,两人不断操作直到出现0,那么最后一步应该是b%a==0(b-a^k==0也可以看成是b%a==0)

对于当前ab,如果选第二种操作那下一个状态就是b%a,a,所以先判断b%a,a是胜还是负

1.b%a,a是负,则当前是胜

2.b%a,a是胜,那就不能选第二种操作

(1)假设b<a^2,那就只能用b-a,令k=b/a,如果k是奇数,那就输了,因为对手总会把k为奇数情况让给你,最后和b%a没区别,如果k是偶数,那就把k为奇数的情况让给对手,那么自己会赢

(2)b>a^2,计算k=b/a,将k%(a+1)后和b<a^2情况一样计算


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
int get_sg(ll a, ll b)
{
    if(a==0)
        return 0;
    if(get_sg(b%a, a))
    {
        ll k=b/a;
        if((k%(a+1))&1)
            return 0;
        else
            return 1;
    }
    return 1;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        ll a, b;
        cin>>a>>b;
        if(a>b)
        {
            ll t=a;
            a=b;
            b=t;
        }
        if(get_sg(a, b))
            printf("First\n");
        else
            printf("Second\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/LSC_333/article/details/78178378