Multiplication Game

5121: Multiplication Game

时间限制: 5 Sec   内存限制: 128 MB
提交: 165   解决: 35
[ 提交][ 状态][ 讨论版][命题人: 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,现在从N的一个素因子中取出一个P,另M*P(初始时M=1),某个人另M==P则这个人胜。

分析:求一下N的素数的个数num,素数的种类a。
对num分奇偶讨论。

若为奇数(先手必胜):

后手会想办法多拿使得先手无法获胜。那么对于当前状态,先手一定拿素数个数最多的那一种素数,而后手则拿素数个数最小的那一种。这样直接模拟就可以了。

若为偶数(后手必胜):
只要判断一下,先手拿的次数,是不是比所有种类的素数中个数最小那个大,如果大一定tie,如果不大一定后手胜。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e6+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
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;}

ll prime[MAX],vis[MAX],cot,a[MAX];
char s[10];

void primeall()
{
    int i;
    for(i=2;i<MAX;i++)
        if(vis[i]==0)
        {
            prime[++cot]=i;
            for(int j=i*2;j<MAX;j+=i)
                vis[j]=1;
        }
}
int main()
{
    int t,i,j;
    ll n,minn=INF,p,q=0,num=0;
    primeall();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        scanf("%s",s);

        minn=INF;
        q=0;
        num=0;

        for(i=1;prime[i]*prime[i]<=n;i++)
        {
            p=0;
            while(n%prime[i]==0)
            {
                n=n/prime[i];
                p++;
            }
            if(p)
            {
                num=num+p;
                q++;
                a[q]=p;
                minn=min(minn,p);
            }
        }
        if(n>1)
        {
            num++;
            q++;
            minn=min(minn,1LL);
            a[q]=1;
        }
        //printf("%d %d %d\n",num,p,q);
        if(num%2)
        {
            int t1,t2,flag=0;
            t1=num/2;
            t2=num/2+1;
            while(1)
            {
                sort(a+1,a+1+q);
                a[q]--;
                t1--;
                if(t1<0)
                    break;

                sort(a+1,a+1+q);
                a[1]--;
                if(a[1]<0)
                {
                    flag=1;
                    break;
                }
                t2--;
                if(t2<0)
                    break;
            }
            if(flag)
                printf("tie\n");
            else if(t1<0)
                printf("%s",s[0]=='A'?"Alice\n":"Bob\n");
            else if(t2<0)
                printf("%s",s[0]=='B'?"Alice\n":"Bob\n");
        }
        else
        {
            int t=num/2;
            if(t>minn)
                printf("tie\n");
            else
                printf("%s",s[0]=='B'?"Alice\n":"Bob\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值