ACdream 1112 Alice and Bob(素筛+博弈SG函数)

本文介绍了一种基于博弈论的取石子游戏算法。游戏中玩家轮流操作,通过分解或减少石子堆数量来赢得比赛。文章详细解析了游戏规则,并提供了一种计算策略,通过分析每个数字的质因数个数来决定最佳行动方案。
Alice and Bob
Time Limit:3000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu

Description

Here  is Alice and Bob again !

Alice and Bob are playing a game. There are several numbers.

First, Alice choose a number n.

Then he can replace n (n > 1) with one of its positive factor but not itself or he can replace n with a and b. Here a*b = n and a > 1 and b > 1.

For example, Alice can replace 6 with 2 or 3 or (2, 3).

But he can't replace 6 with 6 or (1, 6). But you can replace 6 with 1.

After Alice's turn, it’s Bob's turn. Alice and Bob take turns to do so. Who can’t do any replace lose the game.

Alice and Bob are both clever enough. Who is the winner?

Input

This problem contains multiple test cases. The first line contains one number n(1 ≤ n ≤ 100000).

The second line contains n numbers.

All the numbers are positive and less than of equal to 5000000.

Output

For each test case, if Alice can win, output “Alice”, otherwise output “Bob”.

Sample Input

2
2 2
3
2 2 4

Sample Output

Bob
Alice

题意:给定n堆石子,每次可以按照规则将该堆石子的个数分为两堆或者将该堆石子的个数减少,

   谁不能继续操作了谁就输。

题解:规则如下,若n=a*b且a>1,b>1,可以将石子分为a,b两堆;或者可以减少为a或b。

   比如n=6,可以分为(2,3),2,3这三种情况,但是不能分为(1,6),1,6这三种情况。

   和HDU3032是一个问题,就是一个取石子问题,这里的8=2*2*2就是那道题里的3,这里的

   6=2*3就是那道题里的2。如果对这道题理解感到困难可以先阅读我的上上篇博客:

   http://www.cnblogs.com/Ritchie/p/5627242.html

   也就是说这道题是对每个数的素因子个数进行操作,即素数可以看做是一个1。

   设一个数x=a1^r1*a2^r2*...*an^rn;设sum = r1+r2+...+rn.

   然后所有的情况就可以表示为:

   (1,sum-1),(2,sum-2),...(sum/2,sum-sum/2)或者(1),(2),...(n-1)

   然后在计算sum的时候我们可以这样计算。

   设一个数为x,他的最小的素因子为y.则sum[x] = sum[x/y] + 1;

   所以我们在素筛打表的时候要记录每个数的最小素因子以用于上述公式。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 5000050
int prime[maxn],cnt=0;
bool isprime[maxn];
int m[maxn];
int a[maxn];
int sg[105];
void getprime()
{
    memset(isprime,0,sizeof(isprime));
    memset(m,0,sizeof(m));
    memset(a,0,sizeof(a));
    for(int i=2; i<=maxn; i++)
    {
        if(!isprime[i])
        {
            prime[cnt++]=i;
            for(int j=i+i; j<=maxn; j+=i)
            {
                isprime[j]=1;
                if(!m[j]) m[j]=i;
            }
            m[i]=i;
        }
    }
    for(int i=2; i<=maxn; i++)
        a[i]=a[i/m[i]]+1;
}
void getsg()
{
    memset(sg,0,sizeof(sg));
    sg[0]=0;
    sg[1]=1;
    bool vis[105]; //放到循环里定义也一样需要每次都初始化
    for(int i=2; i<=100; i++)
    {
        memset(vis,0,sizeof(vis)); //注意每次都要初始化
        for(int j=0; j<i; j++) //取石子
            vis[sg[j]]=1;
        for(int j=1; j<i; j++) //拆分
            vis[sg[j]^sg[i-j]]=1;
        for(int x=0;; x++)
            if(!vis[x])
            {
                sg[i]=x;
                break;
            }
    }
}
void get()
{
    getsg();
    getprime();
}
void solve()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int data,ans=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&data);
            ans^=sg[a[data]];
        }
        if(ans)
            printf("Alice\n");
        else
            printf("Bob\n");
    }
}
int main()
{
    get();
    solve();
}

 

转载于:https://www.cnblogs.com/Ritchie/p/5635085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值