Kattis - multiplicationgame Multiplication Game 博弈因子相乘是否等于N

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    Sample Output 1
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

题意:A B两个人玩游戏,双方轮流用数字N的因子*M,判断那一方可以必胜,如果都不可以则为平局

思路:因为因子不是只可以用一次,所以当因子的个数大于等于3的时候一定是平局,所以细致的讨论小于等于2的情况,且因子在变成N的时候贡献了几次

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=5e+5;
typedef long long ll;

int a[maxn],b[maxn];
int prime[maxn],x[maxn],cnt=0;
char s[100];
void getprime()
{
    for(int i=2; i<=maxn; i++) {
        if(x[i]==0) {
            prime[cnt++]=i;
            x[i]=1;
            for(int j=i*2; j<=maxn; j+=i) {
                x[j]=2;
            }
        }
    }
}
int main()
{
    getprime();
    int  t;
    scanf("%d",&t);
    while(t--) {
        int n,k,num=0,flag=0;
        scanf("%d %s",&n,s);
        k=n;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        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",s[0]=='A'?"Bob":"Alice");
            else
                printf("%s\n",s[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",s[0]=='A'?"Alice":"Bob");
                else
                    printf("%s\n",s[0]=='A'?"Bob":"Alice");
            } else
                printf("tie\n");

        } else
            printf("tie\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/81281514