Counter game

Sort by

recency

|

86 Discussions

|

  • + 0 comments

    This Python 3 solution has some overhead, but I think it's reasonably readable:

    import itertools
    import typing
    
    
    def counterGame(n: int) -> typing.Literal["Louise", "Richard"]:
        other_player = {"Louise": "Richard", "Richard": "Louise"}
        for player in itertools.cycle(("Louise", "Richard")):
            if n == 1:
                return other_player[player]
            power = n.bit_length() - 1
            if f"{n:b}".endswith(zeros := power * "0", 1):
                return player if power % 2 else other_player[player]
            n -= int(f"1{zeros}", 2)
    
  • + 0 comments
    // Problem Understanding:
    // Players: Two players take turns, starting with Player 1.
    // Objective: Start with a number n and on each turn, a player can either:
    // Halve the number (if it's even).
    // Subtract 1 from the number (if it's odd).
    // The game continues until the number becomes 1. The player who makes the number reach 1 wins.
    // The goal is to determine the winner, assuming both players play optimally.
    // Steps to Solve:
    // Input and Output:
    
    // Input: A number n.
    // Output: The winner (either "Richard" or "Louise").
    // Alternating Moves:
    
    // Players alternate turns, starting with Player 1 (often called "Louise").
    // If the number is even, the optimal move is to halve it.
    // If the number is odd, the optimal move is to subtract 1 to make it even, so the next player can halve it.
    // Determining the Winner:
    
    // Keep track of the number of moves.
    // The game ends when n becomes 1. The player who makes the last move is the winner.
    // Approach:
    // Game Simulation: Simulate the game with a while loop until n becomes 1.
    // If n is even, halve it.
    // If n is odd, subtract 1.
    // Count the number of moves.
    // Decide the Winner: The winner depends on the number of moves:
    // If the number of moves is odd, the first player ("Louise") wins.
    // If the number of moves is even, the second player ("Richard") wins.
    #include <stdio.h>
    
    int popcount(unsigned long long int n) {
      int count = 0;
      while (n) {
        n &= (n - 1);
        count++;
      }
      return count;
    }
    
    int main() {
      int t;
      scanf("%d\n", &t);
      while (t--) {
        unsigned long long int n;
        scanf("%llu\n", &n);
        printf("%s\n", popcount(n - 1) & 1 ? "Louise" : "Richard");
      }
      return 0;
    }
    
  • + 0 comments
    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    public class Solution {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int T = scan.nextInt();
            while (T-- > 0) {
                BigInteger N = new BigInteger(scan.next());
                N = N.subtract(BigInteger.ONE);
                System.out.println(N.bitCount() % 2 == 0 ? "Richard" : "Louise");
            }
            scan.close();
        }
    }
    
  • + 1 comment

    Python3 1 liner:

    return ('Louise', 'Richard')[(n.bit_count() + int(math.log2(n & -n))) % 2]
    #or
    return ('Richard', 'Louise')[(n.bit_count() + (n & -n).bit_length()) % 2]
    
  • + 0 comments

    Python:

    def counterGame(n):
        moves = 0
        
        while n != 1:
    
            exponent = math.log2(n)
            if exponent.is_integer():
                n = n/2
            else:
                n = n - 2 ** int(exponent)
            moves += 1