Classic game set problem solution

1. The Wyzhov game
There are two piles of stones, the number of which can be different. The game begins with two people taking turns taking stones. The game stipulates that there are two different ways to take each time. One is to take as many stones as you want from any pile; the other is to take the same number of stones from two piles at the same time. In the end, the one who removes all the stones is the winner. Now given the initial number of two piles of stones, if it is your turn to take first, assuming both sides take the best strategy, ask whether you are the winner or the loser in the end.
                    Core: if((int((ba)*c))==a)

POU 1067

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<deque>
#include<algorithm>
using namespace std;
intmain()
{
    int a,b;
    double c;
    c=(sqrt(5.0)+1)/2;
    while(scanf("%d %d",&a,&b)!=EOF)
    {
        if(a>b)
            swap(a,b);
        if((int((b-a)*c))==a)
            printf("0\n");
        else
            printf("1\n");
    }
    return 0;
}

2. Bash Game

There are only a pile of n items, and two people take turns to take items from it. It is stipulated that at least one item is taken at a time, and a maximum of m items are taken at a time.

If n%(m+1)==0 , the second player wins, otherwise the first player wins.


3. Nimm Game

    There are arbitrary piles of items, and the number of items in each pile is arbitrary. Both parties take turns to take items from the pile. Each time, only part or all of the items can be taken from the pile of items. At least one item is taken, and the person who takes the last item wins. .

Use a number to accumulate XOR with the number of items in each pile. If the final result is zero, the first player loses, otherwise the first player wins.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<deque>
#include<algorithm>
using namespace std;
intmain()
{
    int n,ans,t,i;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&n); //The two key lines
        ans^=n; //Key
    }
    if(ans==0)
        printf("The second hand wins\n");
    else
        printf("First mover wins\n");
    return 0;
}

4. Fibonacci game

        There are n stones in a pile, and two people take turns to take them. The first one can take any number of stones for the first time, but they cannot take all of them. The number of stones taken each time thereafter cannot exceed 2 times the number of stones taken last time. Whoever takes the finish wins. Whoever takes first loses and outputs "Second win". Whoever wins first outputs "First win".
If n is a Fibonacci number, the first player loses, otherwise the first player wins.

HDU 2516

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<deque>
#include<algorithm>
using namespace std;
intmain()
{
    long long int n,i,p,j;
    long long int fib[50];
    fib[1]=1;
    fib[2]=1;
    for(i=3;i<=49;i++)
        fib[i]=fib[i-1]+fib[i-2];
    while(scanf("%lld",&n)!=EOF)
    {
        if(n==0)
            break;
        for(i=1;i<=49;i++)
            if(fib[i]==n)
                break;
        if(i<=49)
            printf("Second win\n");
        else
            printf("First win\n");
    }
    return 0;
}
5. Ring Game   
N stones form a circle, and only k (1<=k<=n) adjacent stones can be taken each time.
If k==1, then if n is even, the second hand wins. If n is odd, the first player wins.
If k>1, if k>=n, the first hand wins, otherwise the second hand wins. (For details, please refer to the detailed explanation of the ring game ).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324657193&siteId=291194637