I was trying to solve a Counter Game problem:
"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, they reduce the counter by the largest power of 2 less than N.
If N is a power of 2, they 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."
To solve the question, I implemented bit manipulation, and got accepted:
#include <iostream>
#include <cstdio>
int main() {
long long unsigned int n, tmp;
int cnt, t;
scanf("%d", &t);
while(t--) {
scanf("%llu", &n), tmp=n;
cnt=0;
while(tmp) tmp&=tmp-1, cnt++;
cnt--;
while((n&1)==0) n>>=1, cnt++;
if(cnt%2==0) printf("Richard\n");
else printf("Louise\n");
}
return 0;
}
However, during the coding i coded while(n&1==false)
instead of while((n&1)==false)
, thus could not get desired result. Coding while(!(n&1))
gave expected result, but that(!a
instead of a==false
) was bad practice due to some sources(I forgot them) I have read online. And I know the difference between while(!n&1)
and while(!(n&1))
, but I did not know while(n&1==false)
and while((n&1)==false)
. Learnt the latter was and is dissimilar, and may I ask the distinction, please?