Multiplication Game(博弈+唯一分解定理)

#15场中石油的比赛:

问题 E: Multiplication Game

时间限制: 5 Sec   内存限制: 128 MB
提交: 175   解决: 43
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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?

输入

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.

输出

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

样例输入

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

样例输出

Bob
Bob
tie
tie
Alice
tie
tie
tie
tie
Alice

提示

[ 提交][ 状态]

题意:

该题的意思是给你一个N,M=1;

A和B可以选取N的质因子,

(注意:N可能自身就是一个质数,所以质因子只有它本身).

然后            M=M×质因子;

若A或B            能先把M==N的人赢。 且先手给你输入了。


题解:

扫描二维码关注公众号,回复: 63567 查看本文章

题目很明显就是提醒你需要唯一分解定理,

(所有的数都可以表示成    质数的乘积    如10=2×5,100=2×2×5×5);

当你把示例列出来时,你就会发现。

一、只要质因数的种类  大于等于    >=3时:tie               如:30=2×3×5

二、只要质因数的种类    小于等于    <=2才有胜负????

                                        40=2×2×2×5;

仔细观察40为什么输入的是tie呢,

和    质数的个数有关,质数1    和     质数2    的个数相差    1 或  0   才有胜负。

                           要是相差的个数为:  先手赢;                                       

                           要是相差的个数为 0 :  后手赢:

三、要是N本身就是质数,必定是先手赢。

#include<bits/stdc++.h>
using namespace std;
#define maxn 50000
typedef long long ll;
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
int prime[maxn],x[maxn],cnt;
void is_prime(){
    int t=0;
    for(int i=2;i<=maxn;i++){
        if(x[i]==0){
            prime[t++]=i;
            x[i]=1;
            for(int j=i*2;j<=maxn;j+=i){
                x[j]=2;
            }
        }
    }
    cnt=t;
}
int main()
{
        is_prime();
        ll t;
        t=read();
        while(t--){
            ll n,a[1000]={0},b[1000]={0},k,num=0,flag=0;
            char name[100];
            n=read();
            scanf("%s",name);
            k=n;
            for(int i=0;prime[i]*prime[i]<=k&&i<cnt;i++){
                if(k%prime[i]==0){
                    a[++num]=prime[i];
                    if(num>=3){
                        flag=1;break;
                    }
                    while(k%prime[i]==0){
                        k/=prime[i];
                        b[num]++;
                    }
                }
            }
            if(flag){
                printf("tie\n");
                continue;
            }
            if(k!=1){
                a[++num]=k;
                b[num]=1;
            }
            if(num==1){
                if(b[1]%2==0){
                    printf("%s\n",name[0]=='A'?"Bob":"Alice");
                }else{
                    printf("%s\n",name[0]=='A'?"Alice":"Bob");
                }
            }else if(num==2){
                if(abs(b[1]-b[2])<=1){
                    if(abs(b[1]-b[2])==1){
                        printf("%s\n",name[0]=='A'?"Alice":"Bob");
                    }else{
                        printf("%s\n",name[0]=='A'?"Bob":"Alice");
                    }
                }else {
                    printf("tie\n");
                }
            }else{
                printf("tie\n");
            }
        }
        return 0;
}





猜你喜欢

转载自blog.csdn.net/Z_sea/article/details/80077821