• 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
    ");
            else
                printf("Bob
    ");
        }
    }
    int main()
    {
        get();
        solve();
    }
  • 相关阅读:
    随机数生成程序代码( 伪随机<stdlib.h> )
    C++的学习 (此博客将一直补充更新下去,C++语法方面的内容不开新随笔了, *【语法学习】)
    sdut oj 1510 Contest02-4 Spiral
    POJ 2017 Speed Limit (直叙式的简单模拟 编程题目 动态属性很少,难度小)
    JavaWeb-入门第一课-1.静态web动态web 2.web服务器 3.下载和安装Tomcat-web服务器
    java小知识,驼峰规则
    亚马逊 协同过滤算法 Collaborative filtering
    第一个JSP程序
    物理学步入禅境:缘起性空
    人既然知道努力就可以进步,为什么还是会不努力?
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5635085.html
Copyright © 2020-2023  润新知