counter game Algorithm

The counter game algorithm is a popular computational problem-solving technique, often employed in competitive programming and algorithmic challenges. The game is played between two players, where they start with a single non-negative integer counter. The objective of the game is to perform bitwise operations on the counter, with each player taking turns, until the counter reaches zero, ultimately determining the winner. The operations allowed are: reducing the counter by a power of 2, or performing an XOR operation with a power of 2 smaller than the current counter value. The game is designed to challenge players' understanding of bitwise operations and strategies for reducing the counter to zero. The algorithm for solving the counter game involves analyzing the binary representation of the counter and determining the optimal moves for each player. A common approach is to utilize dynamic programming and memoization to store the results of previously computed subproblems, thus reducing the overall complexity of the problem. The algorithm begins by identifying the highest power of 2 less than or equal to the counter, and performing the necessary operation to reduce the counter optimally. Each player aims to minimize the counter faster than their opponent, forcing them to make suboptimal moves. By analyzing the position of the most significant bit and the distribution of other bits in the binary representation of the counter, players can devise winning strategies to outsmart their opponent and reach zero first.

/*
Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations.

If N is not a power of 2, reduce the counter by the largest power of 2 less than N.
If N is a power of 2, reduce the counter by half of N.
The resultant value is the new N which is again used for subsequent operations.
The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins.

Given N, your task is to find the winner of the game.
If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.

Input Format 
The first line contains an integer T, the number of testcases. 
T lines follow. Each line contains N, the initial number set in the counter.

*/



#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

bool isPowerOf2(unsigned long long int x) {
    return ( x && ((x &(x-1)) == 0));
}

unsigned long long int lowerPowerof2( unsigned long long int x) {
     if (x == 0) {
        return 0;
    }
    x--;
    x |= (x >> 1);
    x |= (x >> 2);
    x |= (x >> 4);
    x |= (x >> 8);
    x |= (x >> 16);
    x |= (x >> 32);
    return x - (x >> 1);
}

std::string winner( bool win ) {
    if (win) {
        return std::string("Louise");
    } else {
        return std::string("Richard");
    }
}

int main() {
    
    int T;
    unsigned long long int N;
    cin >> T;
    
    while(T) {
        cin >> N;
        if (N == 1) {
            std::cout << "Richard\n";
            continue;
        }
        bool win = false;
        while(N > 1) {
            if (isPowerOf2(N)) {
                N = N/2;
            } else {
                N = N - lowerPowerof2(N);
            }
            win = !win;
        }
        std::cout << winner(win) << std::endl;
        --T;
    }
    
    return 0;
}


LANGUAGE:

DARK MODE: