-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter Game Updated.cpp
71 lines (45 loc) · 1.81 KB
/
Counter Game Updated.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Task: 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 . 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.
As an example, let the initial value n=132. It's Louise's turn so she first 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. It should return the winner's name, either Richard or Louise.
counterGame has the following parameter(s):
n: an integer to initialize the game counter
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 the game.
Constraints
1<=t<=10
1<=n<=2^64-1
Output Format
For each test case, print the winner's name on a new line in the form Louise or Richard.
Sample Input 0
1
6
Sample Output 0
Richard
Solution(Taken help from google):
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int setBits(unsigned long long int n) {
int count = 0 ;
while(n) {
n &= (n-1) ;
count ++ ;
}
return count ;
}
int main() {
int t ;
cin>>t ;
while(t--) {
unsigned long long int n ;
cin>>n;
if (setBits(n-1) & 1) cout<<"Louise\n";
else cout<<"Richard\n";
}
return 0;
}