題目描述
Ashishgup and FastestFinger play a game.
They start with a number n and play in turns. In each turn, a player can make any one of the following moves:
Divide n by any of its odd divisors greater than 1.
Subtract 1 from n if n is greater than 1.
Divisors of a number include the number itself.
The player who is unable to make a move loses the game.
Ashishgup moves first. Determine the winner of the game if both of them play optimally.
Input
The first line contains a single integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.
The only line of each test case contains a single integer — n (1≤n≤109).
Output
For each test case, print “Ashishgup” if he wins, and “FastestFinger” otherwise (without quotes).
Example
input
7
1
2
3
4
5
6
12
output
FastestFinger
Ashishgup
Ashishgup
FastestFinger
Ashishgup
FastestFinger
Ashishgup
Note
In the first test case, n=1, Ashishgup cannot make a move. He loses.
In the second test case, n=2, Ashishgup subtracts 1 on the first move. Now n=1, FastestFinger cannot make a move, so he loses.
In the third test case, n=3, Ashishgup divides by 3 on the first move. Now n=1, FastestFinger cannot make a move, so he loses.
In the last test case, n=12, Ashishgup divides it by 3. Now n=4, FastestFinger is forced to subtract 1, and Ashishgup gets 3, so he wins by dividing it by 3.
題目大意
給定一個正整數 n,有兩種操作:
(1)用 n 的一個大於 1 的奇因子除 n;(2)如果 n>1 將 n 減 1。
兩個人玩遊戲,每次每個人可以選擇一個操作,最後誰不能操作誰就輸。問誰會贏。
題目分析
這是一個博弈論的題目,有兩條分支。
1是一個必敗態,由1可以推出兩個必勝態:2和奇數(這就是兩條分支)。
- 2這條分支:
由2這條分支可以推出,2*一個素數 (素數>2)
是一個必敗態。(先手除以該素數,會給對手留下2這個必勝態;而-1操作則會給對手留下奇數這個必勝態)
進而可以推出,2 * 一個素數^n (素數>2)
是一個必敗態。(先手可以直接除以這個素數的n-1次方,留給對手2*一個素數的必敗態)
分支1:1(0) -> 2(1) -> 2*素數(0) -> 2*素數^n(1)
- 奇數這條分支:
由奇數這條分支可以推出,2^n(n>1)
是必敗態。(因爲2n沒法除以奇數了,只能選擇-1操作,然後就變成了奇數)
進而可以推出,2^n * 某個奇數
是必勝態。(先手可以直接除以這個奇數,然後就給對方留下了2n的必敗態)
分支2:1(0)-> 奇數(1) -> 2^n(0) -> 2^n*奇數(1)
代碼如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e3+5;
bool is_prime(int x)
{
for(int i=2;i<=x/i;i++)
if(x%i==0) return false;
return true;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
if(n==1) {puts("FastestFinger"); continue;}
if(n&1||n==2) {puts("Ashishgup"); continue;}
bool flag;
if(n%4==0) //分支2
{
while((n&1)==0) n>>=1;
if(n==1) flag=false;
else flag=true;
}
else //分支1
{
n>>=1;
if(is_prime(n)) flag=false;
else flag=true;
}
if(flag) puts("Ashishgup");
else puts("FastestFinger");
}
return 0;
}