Game problem (Bash game, Wyzhov game, Nim game, Fibonacci game) Conclusion

Bash game

There is a pile of n stones to take from it, 1~m stones are taken from it at a time, and the one who finishes them at the end wins.

Conclusion: If n%(m+1)==0 The first mover will lose n%(m+1)! =0 means the first mover will win.

HDU 4764

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;if((n-1)%(m+1)!=0)cout<<"Tang"<<endl;elsecout<<"Jiang"<<endl;} } 








Wyzhov game

There are two piles of stones, each time either take any stone from one pile, at least 1; or take the same stone from the two piles, and the last one wins.

Conclusion: subtract the value of the smallest pile from the largest pile*(1+sqrt(5)/2) If it is equal to the number of the smaller pile, the first mover will lose, otherwise the first mover will win.

Game of Nimes

Arbitrary pile of stones, take any stones from it each time, at least one, and the one who finishes at the end wins.

Conclusion: XOR all the stones in the pile, if it is equal to 0, the first mover will lose, otherwise the first mover will win.

HDU2176

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,a[10005];
while(cin>>n)
{
int k,tmp=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
tmp^=a[i];}if(tmp==0)cout<<"No"<<endl;else{cout<<"Yes"<<endl;for(int i=0;i<n;i++){int t=tmp^a[i];    if(t<a[i]){cout<<a[i]<<" "<<t<<endl; }    }}}return 0;

























 } 

Fibonacci game

A pile of items is taken in turn, at least one at a time and no upper limit at most, the last one is at most twice the previous one, at least one, and the one who finishes at the end wins.

Conclusion: When n is not a Fibonacci number, the first one wins, and the latter one is reversed.

HDU2516

#include <iostream>  
#include <string.h>  
#include <stdio.h>  
using namespace std;  
const int N = 55;    
int f[N];   
void Init()  
{  
    f[0] = f[1] = 1;  
    for(int i=2;i<N;i++)  
        f[i] = f[i-1] + f[i-2];  
}    
int main()  
{  
    Init();  
    int n;  
    while(cin>>n)  
    {  
        if(n == 0) break;  
        bool flag = 0;  
        for(int i=0;i<N;i++)  
        {  
            if(f[i] == n)  
            {  
                flag = 1;  
                break;  
            }  
        }  
        if(flag) puts("Second win");  
        else     puts("First win");  
    }  
    return 0;  


Guess you like

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