Blue Bridge Cup - monks battle of wits (Game)

topic:

Here Insert Picture Description

input Output:

Here Insert Picture Description

analysis:

This title belongs to the game of Nim game, we analyze the position in which novices can be found in such a situation: Suppose index starts at 1, regardless of the position of the even novices to move, in a young monk before this even bits will be able to move the same number of steps, so that the distance of the monk even bit remains unchanged, then we can have all the combinations of two novices, for example, there are a total of a1 a2 a3 a4 a5 monk 5, can be divided is (a1, a2), (a3, a4), a5, a2 a4 because regardless of movement, A1 and a3 are always moved by the same number of steps, so long as the different between the case upper hand or a combination of 0 to win At this time it is converted to common Nim game.

Code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 105;
int num[MAXN],ind;
int nim[MAXN],indx,sum;

int main()
{
    while(cin >> num[++ind])
        if(cin.get()=='\n') break;
    sort(num+1,num+ind+1);
    for(int i=1;i<ind;i++)
        nim[i] = num[i+1] - num[i]-1;
    for(int i=1;i<ind;i+=2)
        sum^=nim[i];
    if(sum == 0)
    {
        printf("-1\n");
        return 0;
    }
    for(int i=1;i<ind;++i)
        for(int j=1;num[i]+j<num[i+1];++j)
        {
            sum = 0;
            nim[i] -= j;
            nim[i-1] += j;
            for(int k=1;k<ind;k+=2)
                sum^=nim[k];
            if(sum == 0)
            {
                printf("%d %d\n",num[i],num[i]+j);
                return 0;
            }
            nim[i] += j;
            nim[i-1] -= j;
        }
    return 0;
}

Published 61 original articles · won praise 7 · views 3636

Guess you like

Origin blog.csdn.net/weixin_42469716/article/details/104614010