Multiplication Game

题目描述

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来说,所有的质因数的乘积一定等于他本身,这道题的意思就变成了每个人取一个质因数谁先把所有的质因数取尽谁就赢。这里的质因数的数量是指总数,如18的质因数有2,3,3,有3个。如果其本身就是一个质因数,则其质因数的数量为1。答案很明显了,其个数为奇数,先手必胜,偶数,先手必败。但是有一个关键的问题,这个游戏的操作者可以使游戏成为平局状态,也就是可以使所得的M比N大,比如30,其质因数为2,3,5,无论先手取哪一个,后手只要和先手取一样的数,都可以使游戏成为平局状态,对于必败方而言,只要自己的可操作的总数比出现次数最少的那个质因数多的话,败方就可以把游戏状态从失败变成平局“tie”,但胜方不会这么做,所以这个处理一定是对败方才进行的,而且胜方为了阻止败方把游戏变成平局,一定会选取出现次数最多的那个质数,比如18,如果先手(胜方)选取2的话,后手再拿一个2就变成平局了,这不是胜方希望看到的。而对于先手操作状态即是初始状态,但后手操作状态一定是先手拿过一次后的状态。回到刚才所说的败方要把状态变成平局的问题,先手操作就是对初始状态操作即可,但后手就要对先手拿过一次后的状态进行处理,N的质因数为奇数时先手必胜,后手要把游戏变成平局,N为偶数时,先手要把游戏状态变成平局。双方的可操作数都是(质因数总数/2),只要比一下可操作数与质数最小的出现次数,谁更大,即可判断是否可能平局。

#include <bits/stdc++.h>
#include <map>
using namespace std;
map <string,int> A;
map<int,int> B;
int prime[100005];
int s,num;
void PrimeDecomposition(int k)
{
    if(k<2) ;
    else if(k==2)
    {
        prime[s]=2;
        B[2]=1;
        num++;
    }
    else
    {

        for(int i=2; i<=sqrt(k); i++)
        {
            while(k>=i)
            {
                if(k%i==0)
                {
                    num++;
                    if(!B[i]) prime[s++]=i;
                    B[i]++;
                    k=k/i;
                }
                else
                    break;
            }
        }
        if(k!=1)
        {
            if(!B[k])
            {
                num++;
                prime[s++]=k;
            }
            B[k]++;
        }
    }
}
int main()
{
    A["Alice"]=1;
    A["Bob"]=2;
    A["tie"]=3;
    int t,n;
    string tmp;
    cin>>t;
    while(t--)
    {
        cin>>n>>tmp;
        int mark=A[tmp];
        memset(prime,0,sizeof(prime));
        B.clear();
        s=0;
        num=0;
        PrimeDecomposition(n);
        //cout<<num<<‘ ‘<<s<<endl;
        //for(int i=0; i<s; i++)
        //cout<<prime[i]<<‘ ‘<<B[prime[i]]<<endl;
        int mini=100005,ans,maxi=0,m;
        if(num%2==1){
        for(int i=0;i<s;i++)
        {
            if(B[prime[i]]>maxi)
            {
                maxi=B[prime[i]];
                m=prime[i];
            }
        }
        B[m]--;
        }
        for(int i=0; i<s; i++)
        {
            if(B[prime[i]]<mini)
                mini=B[prime[i]];
        }
        ans=num/2;
        //cout<<mini<<‘ ‘<<ans<<endl;
        if(mini<ans)
        {
            cout<<"tie"<<endl;
            continue;
        }
        if(num%2==0)
        {
            if(mark==1)
            {
                cout<<"Bob"<<endl;
                continue;
            }
            if(mark==2)
            {
                cout<<"Alice"<<endl;
                continue;
            }
        }
        if(num%2==1)
        {
            if(mark==2)
            {
                cout<<"Bob"<<endl;
                continue;
            }
            if(mark==1)
            {
                cout<<"Alice"<<endl;
                continue;
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/shadowlink/p/8942901.html

时间: 2024-10-14 10:52:53

Multiplication Game的相关文章

HDU 1517 A Multiplication Game 博弈

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6202    Accepted Submission(s): 3532 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

ACDream 1213 Matrix Multiplication (01矩阵处理)

Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of

POJ1651——Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: 3964 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

A Multiplication Game

A Multiplication Game http://acm.hdu.edu.cn/showproblem.php?pid=1517 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3761    Accepted Submission(s): 2136 Problem Description Stan and Ollie play

UVAoj 348 - Optimal Array Multiplication Sequence

1 /* 2 题意:矩阵相乘的最少的步数 3 dp[i][j]=min(dp[i][j], dp[i][k]+dp[k+1][j]+num[i-1]*num[k]*num[j]); 4 表示的是第i个矩阵到第j个矩阵相乘的最少步数 5 sign[i][j]表示的是第i个矩阵到第j个矩阵相乘的最少步数是由第i个矩阵到第sign[i][j]个矩阵相乘最少步数 6 和第sign[i][j]+1个矩阵到第j个矩阵相乘最少步数的得到的最小值! 7 */ 8 #include<iostream> 9 #i

Matrix multiplication

题目链接 题意: 给两个n*n的矩阵,求乘积后对3取摸的结果(1≤n≤800) 分析: 考虑一下为什么给3呢,对3取摸只可能得到0.1.2,都可以看作两位的,那么在乘法的时候我们可以用分配率将原来的矩阵乘法分成四个矩阵乘法,每个矩阵都只包括0和1.对于0/1矩阵的乘法,可以使用bitset来快速运算 const int MAXN = 801; bitset<MAXN> r1[MAXN], r2[MAXN], c1[MAXN], c2[MAXN]; int a[MAXN][MAXN]; int

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

uva442-Matrix Chain Multiplication

 Matrix Chain Multiplication  Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number o

poj 3318 Matrix Multiplication

http://poj.org/problem?id=3318 矩阵A*矩阵B是否等于矩阵C 1 #include <cstdio> 2 #include <cstring> 3 #include <time.h> 4 #include <algorithm> 5 #define maxn 1010 6 using namespace std; 7 8 int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];