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 :
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
Largest Rectangle
Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle
View Solution →Simple Text Editor
In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,
View Solution →Poisonous Plants
There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan
View Solution →AND xor OR
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value
View Solution →Waiter
You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the
View Solution →Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →