Counter game


Problem Statement :


Louise and Richard have developed a numbers game. They pick a number and check to see if it is a power of 2. If it is, they divide it by 2. If not, they reduce it by the next lower number which is a power of 2. Whoever reduces the number to 1 wins the game. Louise always starts.

Given an initial value, determine who wins the game.

Example
n = 132
It's Louise's turn first. She determines that 132 is not a power of 2. The next lower power of 2 is 128, so she subtracts that from 132 and passes 4 to Richard. 4 is a power of 2, so Richard divides it by 2 and passes 2 to Louise. Likewise, 2 is a power so she divides it by 2 and reaches 1. She wins the game.

Update If they initially set counter to 1, Richard wins. Louise cannot make a move so she loses.

Function Description

Complete the counterGame function in the editor below.

counterGame has the following parameter(s):

int n: the initial game counter value
Returns

string: either Richard or Louise
Input Format

The first line contains an integer t, the number of testcases.
Each of the next t lines contains an integer n, the initial value for each game.

Constraints
1 <= t <= 10
1 <= n <= 2^64-1



Solution :



title-img


                            Solution in C :

In C++ :





#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
    {
    int t;
    unsigned long long int n;
    cin>>t;
    while(t--)
        {
            int c=0;
            cin>>n;
            //if(n==1) {cout<<"Louise";continue;}
            while(n>1)
                {
                    if((n&(n-1))==0)
                    {    n/=2;
                        c++;
                    }
                    else
                    {    
                        unsigned long long int i=n;
                        int size=0;
                        while(i>0)
                        {    i=i>>1;size++;
                        }
                        n=n-(1ULL<<(size-1));
                        c++;
                    }
            }
            if(c%2!=0) cout<<"Louise\n";
            else cout<<"Richard\n";
    }
    return 0;
}








In Java :





import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Solution {

	static BufferedReader reader;
	static StringTokenizer tokenizer = null;
	static PrintWriter writer;

	static String nextToken() throws IOException {
		while (tokenizer == null || (!tokenizer.hasMoreTokens())) {
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	static double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	static long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}

	public static void main(String[] args) throws IOException {
		reader = new BufferedReader(new InputStreamReader(System.in));
		writer = new PrintWriter(System.out);
		cherry();
		reader.close();
		writer.close();
	}

	static BigInteger two = new BigInteger("2");

	static boolean isWin(BigInteger bi) {
		if (bi.equals(BigInteger.ONE)) {
			return false;
		}
		if (bi.bitCount() == 1) {
			return !isWin(bi.divide(two));
		} else {
			return !isWin(bi.clearBit(bi.bitLength() - 1));
		}
	}

	static void cherry() throws NumberFormatException, IOException {
		int T = nextInt();
		for (int t = 0; t < T; t++) {
			BigInteger bi = new BigInteger(nextToken());
			writer.println(isWin(bi) ? "Louise" : "Richard");
		}
	}
}








In C :





#include<stdio.h>
#include<math.h>
int main(){
    int nt;
    scanf("%d",&nt);
    int x,y;
    long long unsigned int n;
    for(x=0;x<nt;x++){
        scanf("%llu",&n);
        int bitcount = 0;
        long long unsigned int value=n;
        while (value != 0){
            bitcount++;
            value &= value - 1;
        }
        bitcount=bitcount-1;
        int loop_count=0;
        long long unsigned int temp=1;
        while(!(n&temp)){
            loop_count++;
            temp=pow(2,loop_count);
        }
        if((bitcount+loop_count)%2==0){
            printf("Richard\n");
        }
        else{
            printf("Louise\n");
        }
    }
    return 0;
}








In Python3 :





import math
from math import log2
from math import floor
def genlist(x):
    if x==1:
        return False
    elif (log2(x))%1==0:
        return not genlist(int(x/2))
    else:
        return not genlist(x-2**floor(log2(x)))

tcs=int(input())
for tc in range(tcs):
    sa=int(input())
    if genlist(sa):
        print("Louise")
    else:
        print("Richard")
                        








View More Similar Problems

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →

Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →