ZOJ 3529 A Game Between Alice and Bob

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3529

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 number N (1<= N <= 100000) representing there are N numbers on the blackboard. The second line contains N 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: “, where c 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

乍一看,博弈问题,如果计算出每个数因子,果断TLE。不过思想还算正确。

我们就要首先用筛选法打出素数表,然后再计算因子的时候,加入记忆化的过程就不会TLE 了

代码如下

#include<iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const ll N = 5000009;
ll a[100009], b[N];
bool flag[N];
vector <int> prime;

void PRime()
{
    memset(flag, 1, sizeof(flag));
    flag[1] = false;
    flag[2] = true;
    for (ll i = 2; i < N; i++)
    {
        for (ll j = 2; flag[i] && j*i<N; j++)
        {
            flag[i*j] = 0;
        }
    }
    for (ll i = 2; i < N; i++)
    {
        if (flag[i])
        {
            prime.push_back(i);
        }
    }

}
int fun(ll x)
{
    if (b[x] != -1)     return b[x];
    int num = prime.size(), temp = x, ans = 0;
    for(ll i = 0; i < num && prime[i]*prime[i] <= x; i++)
    {
        while(x % prime[i] == 0)
        {
            x /= prime[i] ;
            ans++;
        }
    }
    if (x > 1)
        ans++;
    return b[temp] = ans;
}

int main()
{
#ifndef ONLINE_JUDGE 
    freopen("1.txt", "r", stdin);
#endif
    int n, i, ans, count = 0, t1, t2;
    PRime();
    memset(b, -1, sizeof(b));
    while(~scanf("%d", &n))
    {
        ans = 0;
        for (i = 0; i < n; i++)
        {
            scanf("%lld", &a[i]);
            ans ^= fun(a[i]);
        }
        printf("Test #%d: ", ++count);
        if (ans)
        {
            cout << "Alice ";
            for (i = 0; i < n; i++)
            {
                if ((ans^fun(a[i]))  < fun(a[i]))
                {
                    cout << i + 1 << endl;
                    break;
                }
            }
        }
        else
        {
            cout << "Bob\n";
        }
    }
    return 0;
}

你可能感兴趣的:(ZOJ)