快速计算乘方的算法:
如计算2^13,则传统做法需要进行12次乘法。
[cpp] view
plaincopyprint?
-
-
unsigned power(unsigned n,unsigned p)
-
{
-
for(int i = 0; i < p; i++)
-
n *= n;
-
return n;
-
}
优化如下:
把2*2的结果保存起来:4*4*4*4*4*4*2
再把4*4的结果保存起来:16*16*16*2
一共5次运算,分别是2*2、4*4和16*16*16*2
[cpp] view
plaincopyprint?
-
unsigned int power(unsigned int n, unsigned int p)
-
{
-
-
unsigned int tmp = 1;
-
while (p > 1)
-
{
-
-
if (( p & 1 )!=0)
-
{
-
tmp *= n;
-
}
-
n *= n;
-
p >>= 1;
-
}
-
return n * tmp;
-
}
Montgomery 快速幂模算法:
[cpp] view
plaincopyprint?
-
unsigned int Montgomery(unsigned int n, unsigned int p, unsigned int m)
-
{
-
unsigned int r = n % m;
-
unsigned int tmp = 1;
-
while (p > 1)
-
{
-
if ((p & 1)!=0)
-
{
-
tmp = (tmp * r) % m;
-
}
-
r = (r * r) % m;
-
p >>= 1;
-
}
-
return (r * tmp) % m;
-
}