Tower Breakers

Two players are playing a game of Tower Breakers! Player 1 always moves first, and both players always play optimally. The rules of the game are as follows:

  • Initially, there are n towers.
  • Each tower is of height m.
  • The players move in alternating turns.
  • In each turn, a player can choose a tower of height x and reduce its height to y, where  and evenly divides x.
  • If the current player is unable to make a move, they lose the game.

Given the values of n and m, determine which player will win. If the first player wins, return 1. Otherwise, return 2.

Example:

Input:  n = 2, m = 2
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int towerBreakers(int nint m)
{
    if (m == 1 || n % 2 == 0)
        return 2;
    return 1;
}

int main()
{
    int n = 2;
    int m = 2;
    cout << towerBreakers(nm<< "\n";

    return 0;
}


No comments:

Post a Comment