【Codeforces】GCD Compression 唯一分解定理 思維

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.

題意:這題題意還是見題面吧

思路:

1.首先拿到非1奇數必贏,這個不用多說。(n爲1直接判斷對方贏就好)
2.
如果拿到的是偶數,如果是2,就直接-1必贏。
如果不是2,那我-1肯定必輸,是不會考慮走-1這條路的。所以看第一條路,除以一個奇數因子。
利用唯一分解定理,可以將一個整數n寫成如下形式:
n = 2m * k
其中2m是n的最大2次冪因子,k即是最大奇數因子
觀察這個式子,如果m>=2的時候,若k>1,我把k拿掉(除以k),那隻剩下一個2m,對方下一步就只能-1,這樣我當前就一定能贏。如果m=1,如果按照我上面步驟拿掉k,這樣會留下一個2,對方-1我就GG了。所以,當m=1的時候,我要讓對方取走最後的k我才能贏。這個時候就看k是否爲質數就行了,如果是,那我只能拿走k,對方必贏。如果不是,我可以把k拿走一部分,留下一個質數,讓對方GG。
所以對上述進行分類討論即可。

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

bool prime(ll x)
{
    ll m = sqrt(x*1.0);
    rep(i,2,m)
    {
        if(x%i==0) return false;
    }
    return true;
}

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n;
        n = read();
        if(n==1) cout<<"FastestFinger"<<endl;
        else if(n&1||n==2) cout<<"Ashishgup"<<endl;
        else
        {
            ll p = 0 ;
            while(n%2==0) n /= 2, p++;
            if(p!=1)
           {
                if(n==1) cout<<"FastestFinger"<<endl;
                else cout<<"Ashishgup"<<endl;
           }
           else
           {
               if(prime(n)) cout<<"FastestFinger"<<endl;
               else cout<<"Ashishgup"<<endl;
           }
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章