• + 59 comments

    The problem can be solved in much simpler way. There's no requirement of several fancy functions, "power", "log" etc., just bit-manipulation does the trick. Unfortunately, even "editorial" didn't give best/simplest of the solution. Let me share my approach for guys looking for better solution.


    First of all, there's no need to "actually perform the operations on N". The game just requires counting set-bits in binary representation of N-1. This can be accomplished in complexity, where b=set-bits in (N-1). Here's code in C :

    int setBits(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) ;
            if (setBits(n-1) & 1) printf("Louise\n") ;
            else printf("Richard\n") ;
        }
        return 0;
    }
    

    Obviously, language like Python gives even shorter solution. Hope this simplifies the problem to some extent.

    Any comments/suggestions for further improvement are welcomed :)


    Informative Tweets for Inquisitive Minds