calculate (x^y)%z without pow();
------------------------------
the question here is how we can reduce
the complexity... if we compute the x^y by normal method the complexity is O(y)... to reduce the complexity we can use binary weighted power computation method in which complexity is O(logy)..
ans=1;
square=x;
if(y==0)
return 1;
while(y!=0)
{
if(y%2)
ans=ans*square;
square=(square*square)%z;
y=y/2;
if(ans>z)
ans=ans%z;
}
return ans;

本文介绍了一种使用二进制加权幂运算方法实现的快速幂取模算法,该算法可以将复杂度从O(y)降低到O(log y),特别适用于大数幂运算场景。

被折叠的 条评论
为什么被折叠?



