Wrong output for BITTUP problem

i am trying to debug this code since two days still i am not able to understand what is wrong with it.can somebody help

#include <iostream>
 
using namespace std;
long long int power(long long x, long long int y,long long int p)
{
    long long int res = 1; 
 
    x = x % p; 
            
    if(y==1)
    return x;
    
    if(y==0)
    return 1;
    if (x == 0) return 0; 

    while (y > 0)
    {
        
        if (y & 1)
            res = ((res%p)*(x%p)) % p;
 
        
        y = y>>1; // y = y/2
        x = ((x%p)*(x%p)) % p;
    }
    return res;
}

int main() {
	// your code goes here
	long long int t;
	cin>>t;
	while(t--)
	{
	   long long int N,M;
	   cin>>N>>M;
	   
	    cout<<power((2,N,1000000007)-1,M,1000000007)<<endl;;
	    
	}
	return 0;
}

There’s a compiler warning that you should address:

[simon@simon-laptop][08:38:30]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling abhinav_700-BITTUP.cpp
+ g++ -std=c++14 abhinav_700-BITTUP.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
abhinav_700-BITTUP.cpp: In function ‘int main()’:
abhinav_700-BITTUP.cpp:35:21: warning: left operand of comma operator has no effect [-Wunused-value]
      cout<<power((2,N,1000000007)-1,M,1000000007)<<endl;;
                     ^
abhinav_700-BITTUP.cpp:35:23: warning: right operand of comma operator has no effect [-Wunused-value]
      cout<<power((2,N,1000000007)-1,M,1000000007)<<endl;;
                       ^~~~~~~~~~
+ set +x
Successful

It might be a clue as to where you’re going wrong (like the last two times XD).

2 Likes

I suggest you unpack the inline calculations here into intermediate variables to clarify to yourself what is going on, eg:

one_bit = ... ;
m_bits = ... ;
cout << m_bits << endl;

Then if you want to, you can pack them back onto one line, but it doesn’t really save you anything.

“next time before posting on code chef discuss forum have to check warnings on a offline compiler”
Now i have noted it down so i don’t forget it again :sweat_smile: :sweat_smile: :sweat_smile:

thanks for pointing it out

1 Like