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 lasttwo times XD).