• ZOJ 3529 A Game Between Alice and Bob 博弈好题


    A Game Between Alice and Bob

    Time Limit: 5 Seconds      Memory Limit: 262144 KB

    Alice and Bob play the following game. A series of numbers is written on the blackboard. Alice and Bob take turns choosing one of the numbers, and replace it with one of its positive factor but not itself. The one who makes the product of all numbers become 1 wins. You can assume Alice and Bob are intelligent enough and Alice take the first turn. The problem comes, who is the winner and which number is Alice's first choice if she wins?

    Input

    This problem contains multiple test cases. The first line of each case contains only one numberN (1<= N <= 100000) representing there are N numbers on the blackboard. The second line containsN integer numbers specifying the N numbers written on the blackboard. All the numbers are positive and less than or equal to 5000000.

    Output

    Print exactly one line for each test case. The line begins with "Test #c: ", wherec indicates the case number. Then print the name of the winner. If Alice wins, a number indicating her first choice is acquired, print its index after her name, separated by a space. If more than one number can be her first choice, make the index minimal.

    Sample Input

    4
    5 7 9 12
    4
    41503 15991 72 16057 
    

    Sample Output

    Test #1: Alice 1
    Test #2: Bob
    

    Author: ZHOU, Yuchen
    Contest: ZOJ Monthly, September 2011

    题意:

    题目大意:给定n个数,每一步都可以将某个数替换为它的因子,但不能替换为本身,两个人轮流走,直到某个人走不了他就输了。问最后谁能赢,如果先手胜输出第一步。n<=10万,每个数<=5000000.

    思路:

    x = p1^a1*p2^a2...pk^ak,pi为质数,这样每个数都由(a1+a2+..ak)个质数组成,然后就转换成若干堆质数,每次可以取走某堆的某些个质数,拿走一些质数后就变成了其因子。问最后谁取无可取。


     

    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    using namespace std;
    const int A=5000005;
    int prime[3000];
    bool use[3000];
    int sg[A/10];
    int m[100005];
    int pos;
    int getsg(int j)
    {
            int ans=0;
            for(int i=0;i<pos&&prime[i]*prime[i]<=j;i++)
            {
                int k=0;
                while(j%prime[i]==0)
                {
                    k++;
                    j/=prime[i];
                }
                ans+=k;
            }
            if(j!=1)ans++;
            return ans;
    }
    
    void get_prime()
    {
        memset(use,0,sizeof(use));
        for(int i=2;i<sqrt(A+1.0);i++)
        {
            if(!use[i])
            for(int j=i*2;j<sqrt(A+1.0);j+=i)
            {
                use[j]=true;
            }
        }
        pos=0;
        for(int i=2;i<sqrt(A+1.0);i++)
        {
            if(!use[i])
            prime[pos++]=i;
        }
    }
    int main()
    {
        get_prime();
        int n,t=0;
        while(~scanf("%d",&n))
        {
            t++;
            int tp=0;
            for(int i=1;i<=n;i++)
            {
               scanf("%d",&m[i]);
                m[i]=getsg(m[i]);
                tp^=m[i];
                //printf("%d
    ",m[i]);
            }
            if(tp)
            {
                for(int i=1;i<=n;i++)
                    if((tp^m[i])<m[i])
                    {
                        printf("Test #%d: Alice %d
    ",t,i);
                        break;
                    }
            }
            else{
                printf("Test #%d: Bob
    ",t);
            }
        }
    
    }
    


    参考 woshi250hua

  • 相关阅读:
    软工实践练习一——使用Git进行代码管理心得
    作业1.3——Android平台的开发环境的发展演变
    作业1.2——软件工程的实践项目的自我目标
    SVN冲突解决
    sea.js及三种加载方式的异同
    php中AJAX请求中使用post和get请求的区别
    Vue-起步篇:Vue与React、 Angular的区别
    页面常见布局以及实现方法--flex
    requestAnimationFrame之缓动的应用
    bootstrap中table页面做省市区级联效果(级联库见前面级联编辑)(非select下拉框)
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3196561.html
Copyright © 2020-2023  润新知