Cerinta completa

There are piles of stones where the ith pile has stones in it. Alice and Bob play the following game:

  1. Alice starts, and they alternate turns.

  2. In a turn, a player can choose any one of the piles of stones and divide the stones in it into any number of unequal piles such that no two of the newly created piles have the same number of stones. For example, if there 8 stones in a pile, it can be divided into one of these set of piles: or

  3. The player who cannot make a move (because all the remaining piles are indivisible) loses the game.

Given the starting set of piles, who wins the game assuming both players play optimally (that means they will not make a move that causes them to lose the game if some better, winning move exists)?

Input Format

The first line contains the number of test cases . test cases follow. The first line for each test case contains , the number of piles initially. The next line contains space delimited numbers, the number of stones in each of the piles.

Constraints

Output Format

Output lines, one corresponding to each test case containing ALICE if Alice wins the game and BOB otherwise.

Sample Input

4  
1  
4  
2  
1 2  
3  
1 3 4  
1  
8

Sample Output

BOB  
BOB  
ALICE  
BOB

Explanation

For the first case, the only possible move for Alice is (4) -> (1,3). Now Bob breaks up the pile with 3 stones into (1,2). At this point Alice cannot make any move and has lost.


Limbajul de programare folosit: cpp14

Cod:

// https://www.hackerrank.com/challenges/stone-piles

#include <iostream>
#include <vector>
#include <set>
#include <string>

using namespace std;

int n, ans[51];
set<int> st;

void dfs(int start, int rest, int result) {
    if (rest == 0)
        st.insert(result);
    else
        for (int i = start + 1; i <= rest && i < n; i ++)
            dfs(i, rest - i, result ^ ans[i]);
}

int main() {
    ans [1] = ans[2] = 0;
    int i, j, t, res = 0;
    string str[2] = {"BOB", "ALICE"};

    for (i = 3; i <= 50; i ++) {
        n = i;
        st.clear();

        dfs(0, i, 0);
        set<int>::iterator it;

        for (j = 0; st.find(j) != st.end(); j ++);
            ans[i] = j;
    }

    cin >> t;

    while (t--) {
        cin >> n;
        res = 0;

        for (i = 0; i < n; i ++) {
            cin >> j;
            res = res ^ ans[j];
        }

        if (res == 0)
            cout << str[0] << endl;
        else
            cout << str[1] << endl;
    }

    return 0;
}

Scor obtinut: 1.0

Submission ID: 464613159

Link challenge: https://www.hackerrank.com/challenges/stone-piles/problem

Stone Piles