Variation in Nim Game | CPP program for the variation in nim game ; Function to return final grundy Number ( G ) of game ; if pile size is odd ; We XOR pile size + 1 ; We XOR pile size - 1 ; driver program ; Game with 3 piles ; pile with different sizes ; Function to return result of game ; if ( res == 0 ) if G is zero ; else if G is non zero
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int p [ ] , int n ) { int G = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] & 1 ) G ^= ( p [ i ] + 1 ) ; G ^= ( p [ i ] - 1 ) ; } return G ; } int main ( ) { int n = 3 ; int p [ 3 ] = { 32 , 49 , 58 } ; int res = solve ( p , n ) ; cout << " Player ▁ 2 ▁ wins " ; cout << " Player ▁ 1 ▁ wins " ; return 0 ; }