I have been at this for more than an hour and I still can't figure out why this function returns 0.
Hopefully an extra pair of eyes can help me figure out what is going wrong.
#include <bits/stdc++.h>
using namespace std;
int power(int x, int y, int p)
{
// cout << x << " " << y << " " << p << " " << res << endl;
if(x==0)
return 0;
if(y==0)
return 0;
long res = 1;
if(y%2 == 0){
res = power(x, y/2, p);
res = (res*res)%p;
}
else{
res = x%p;
res = (res*power(x, y-1, p) % p) % p;
}
cout << x << " " << y << " " << p << " " << res << endl;
return int((res+p)%p);
}
int main() {
cout << power(2, 100000, 1000000007);
return 0;
}
This should be changed:
if(y==0)
return 1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.