ACdream 1112 Alice and Bob (SG函数+线性素数筛)

ACdream 1112 Alice and Bob (SG函数+线性素数筛):http://acm.hust.edu.cn/vjudge/contest/view.action?cid=113567#problem/B 传送门:nefu

题面:

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==n的两个数,去取代替n,最后一个进行操作的人获胜。


算法分析:

Alice和Bob两人又干上了,他们不但喜欢在取石子时攀比,而且还喜欢在取数时非得挣个你死我活,但是对我们来说,取数和取石子都差不多,我们只需要把所有情况的SG函数值异或起来即可。

但是道理我们还是要清楚的,首先,考虑一个数data,对于data的替换可能如下:

(a1, data/a1), (a2, data/a2), (a3, data/a3),...,(an,data/an) ;或者(a1), (a2), (a3), ..., (an).

由于数据量比较大,我们这样每一个数都去考虑是不能行得通的,鉴于我们考虑到data的因子,就要联想到如何用data的度因子去解决问题,由于对data进行素因子分解,得到:data=a1^r1*a2^r2*a3^r3*...*an^rn,且sum=r1+r2+r3+...+rn.

则所有的情况可以表示为:

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

即可减小数据的范围,计算sum时我们可以这样计算: 设data的最小因子为d,则sum[data]=sum[data/d]+1(线性素数筛);

代码实现:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int MAXN=5000010;

int prime[MAXN];
bool isprime[MAXN];
int facnum[MAXN];
int facmin[MAXN];
int sg[100];
void getPrime()
{
    int i,j;
    int cnt=0;
    memset(isprime,0,sizeof(isprime));
    memset(facnum,-1,sizeof(facnum));
    memset(facmin,-1,sizeof(facmin));
    for(i=2;i<=5000000;i++)
    {
        if(!isprime[i])
        {
            prime[cnt++]=i;
            for(j=i+i;j<=5000000;j+=i)
            {
                isprime[j]=1;
                if(facmin[j]==-1)
                {
                    facmin[j]=i;
                }
            }
            facmin[i]=i;
        }
    }
}

int getnum(int x)
{
    if(x==1)
        return 0;
    if(facnum[x]!=-1)
    return facnum[x];
    return facnum[x]=getnum(x/facmin[x])+1;
}

int getsg(int x)
{
    if(sg[x]!=-1)
        return sg[x];
    bool vis[100];
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=x;i++)
    {
        vis[getsg(x-i)]=1;
    }
    for(int i=1;i<=x/2;i++)
    {
        vis[getsg(i)^getsg(x-i)]=1;
    }
    for(int i=0;;i++)
    {
        if(!vis[i])
        {
            return sg[x]=i;
        }
    }
}

int main()
{
    int n,data;
    getPrime();
    memset(sg,-1,sizeof(sg));
    sg[0]=0;
    int ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&data);
            ans=ans^getsg(getnum(data));
        }
        if(ans)
            cout<<"Alice"<<endl;
        else cout<<"Bob"<<endl;
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值