Wizards and Numbers (Codeforces 168E)
Tags: Math, games

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 a, b (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 cin, cout 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. 

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.

Sample Inputs:
4
10 21
31 10
0 1
10 30

Sample Outputs:
First
Second
Second
First

Solution (Python):
import math
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
N = int(readline())

def main(a, b):
    if a > b:
        (a, b) = (b, a)
    if 0 in (a, b):
        retu = False
    elif not main(b % a, a):
        retu = True
    elif a % 2:
        if b // a % 2:
            retu = False
        else:
            retu = True
    else:
        r = b // a
        if r % (a + 1) % 2 == 1:
            retu = False
        else:
            retu = True
    return retu
for _ in range(N):
    (a, b) = map(int, readline().split())
    if main(a, b):
        ans = 'First'
    else:
        ans = 'Second'
    print(ans)

Solution (Python):
import math
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
N = int(readline())

def main(a, b):
    if a > b:
        (a, b) = (b, a)
    if 0 in (a, b):
        retu = False
    elif not main(b % a, a):
        retu = True
    elif a % 2:
        if b // a % 2:
            retu = False
        else:
            retu = True
    else:
        r = b // a
        if r % (a + 1) % 2 == 1:
            retu = False
        else:
            retu = True
    return retu
for _ in range(N):
    (a, b) = map(int, readline().split())
    if main(a, b):
        ans = 'First'
    else:
        ans = 'Second'
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd

def sol():
    (n, m) = map(int, input().split())
    if m > n:
        (n, m) = (m, n)
    ord = [(n, m)]
    while n != 0 and m != 0:
        if n > m:
            (n, m) = (m, n)
        m %= n
        ord.append((n, m))
    ord = ord[::-1]
    w = [0]
    for i in range(1, len(ord)):
        (a, b) = ord[i]
        (x, y) = ord[i - 1]
        if w[-1] == 0:
            w.append(1)
        else:
            cnt = (a - y) // b
            if cnt % (b + 1) % 2 == 0:
                w.append(1)
            else:
                w.append(0)
    if w[-1] == 1:
        print('First')
    else:
        print('Second')
    return
T = int(input())
for i in range(T):
    sol()