Multiplication Game(质因数分解,博弈)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44056753/article/details/102763782

Alice and Bob are in their class doing drills on multiplication and division. They quickly get bored and instead decide to play a game they invented.

The game starts with a target integer N≥2, and an integer M=1. Alice and Bob take alternate turns. At each turn, the player chooses a prime divisor p of N, and multiply M by p. If the player’s move makes the value of M equal to the target N, the player wins. If M>N, the game is a tie.

Assuming that both players play optimally, who (if any) is going to win?

Input

The first line of input contains T (1≤T≤10000), the number of cases to follow. Each of the next T lines describe a case. Each case is specified by N (2≤N≤231−1) followed by the name of the player making the first turn. The name is either Alice or Bob.

Output

For each case, print the name of the winner (Alice or Bob) assuming optimal play, or tie if there is no winner.

Sample Input 1

10
10 Alice
20 Bob
30 Alice
40 Bob
50 Alice
60 Bob
70 Alice
80 Bob
90 Alice
100 Bob

Sample Output 1

Bob
Bob
tie
tie
Alice
tie
tie
tie
tie
Alice

思路

先将所给的数进行质因数分解,因为给的数据范围比较大,所以先用欧拉筛进行打表,然后分解出来的质因数种类K,如果K大于等于三,一定平局,K等于1的时候先拿的人必胜,k等于2的时候,如果两种质因数出现的次数相等,则第一个人赢,如果相差为一,则第二个人赢,否则平局

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<iomanip>
using namespace std;
typedef long long ll;
const int maxn=1000001;
int prime[maxn+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=maxn; i++)
    {
        if(!prime[i])
            prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=maxn/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}
ll factor[maxn][2];
int fatCnt;
int getFac(ll x)
{
    fatCnt=0;
    ll tmp=x;
    for(int i=1; prime[i]<=tmp/prime[i]; i++)
    {
        factor[fatCnt][1]=0;
        if(tmp%prime[i]==0)
        {
            factor[fatCnt][0]=prime[i];
            while(tmp%prime[i]==0)
            {
                factor[fatCnt][1]++;
                tmp/=prime[i];
            }
            fatCnt++;
        }
    }
    if(tmp!=1)
    {
        factor[fatCnt][0]=tmp;
        factor[fatCnt++][1]=1;
    }
    return fatCnt;
}
int main()
{   
	getPrime();
    int T;
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        ll x;
        string name;
        cin>>x>>name;
        int k=getFac(x);
        if(k>=3)
            cout<<"tie"<<endl;
        if(k==1)
        {
            if(factor[0][1]%2)
                cout<<name<<endl;
            else
            {
                if(name=="Alice")
                    cout<<"Bob"<<endl;
                else
                    cout<<"Alice"<<endl;
            }
        }
        if(k==2)
        {
            if(factor[0][1]==factor[1][1])
            {
                if(name=="Alice")
                    cout<<"Bob"<<endl;
                else
                    cout<<"Alice"<<endl;
            }
            else if(abs(factor[0][1]-factor[1][1])==1)
                cout<<name<<endl;
            else
                cout<<"tie"<<endl;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44056753/article/details/102763782