转自:http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html快速幂介绍的很详细,贴上了,还是理解原理最重要。
#include <iostream>
#include <cstdio>
using namespace std;
int mod_exp(int a, int b, int c) //快速幂取余a^b%c
{
int res, t;
res = 1 % c;
t = a % c;
while (b)
{
if (b & 1)
{
res = res * t % c;
}
t = t * t % c;
b >>= 1;
}
return res;
}
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
cout << mod_exp(n, n, 10) << endl;
}
system("pause");
return 0;
}
先贴一个秦九韶算法(Horner算法)的原理:
设有 id="iframe_0.1435775590282773" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/4/0/b/40b85027598d87611b1c8d5d11e46812.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1435775590282773',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 45px; height: 16px;">项的 id="iframe_0.8371983490806065" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/7/b/8/7b8b965ad4bca0e41ab51de7b31363a1.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8371983490806065',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 12px; height: 9px;">次函数
id="iframe_0.6408741371112883" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/d/2/5/d25290170053cbfe1d467c061e2a3e38.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6408741371112883',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 515px; height: 23px;">
将前 id="iframe_0.1437582034495415" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/7/b/8/7b8b965ad4bca0e41ab51de7b31363a1.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1437582034495415',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">项提取公因子 id="iframe_0.09913748292002755" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/9/d/d/9dd4e461268c8034f5c8564e155c67a6.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.09913748292002755',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 11px; height: 9px;">,得
id="iframe_0.712640671790018" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/1/b/3/1b3a8380ce8c0fbfd89e70455b6f4599.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.712640671790018',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 541px; height: 23px;">
再将括号内的前 id="iframe_0.07212250019805055" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/a/4/3/a438673491daae8148eae77373b6a467.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.07212250019805055',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 45px; height: 15px;">项提取公因子 id="iframe_0.15470733582773777" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/9/d/d/9dd4e461268c8034f5c8564e155c67a6.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.15470733582773777',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">,得
id="iframe_0.6672549748031109" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/9/1/0/9106deecdf6f9650479ada204d0be4c6.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6672549748031109',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 557px; height: 23px;">
如此反复提取公因子 id="iframe_0.37541268686446405" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/9/d/d/9dd4e461268c8034f5c8564e155c67a6.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.37541268686446405',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 0px; height: 0px;">,最后将函数化为
id="iframe_0.5420219777285358" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/7/5/1/7514d392c909ec6ba2fec54d5e4a7b29.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5420219777285358',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 435px; height: 21px;">
令
id="iframe_0.075915077938703" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/4/d/f/4df75f8df58d00083250447c478b3dfc.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.075915077938703',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 134px; height: 18px;">
id="iframe_0.7214338990712672" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/9/4/0/940a757f22c49940f29963e17b29d7e6.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7214338990712672',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 132px; height: 18px;">
id="iframe_0.28973411293757745" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/5/4/c/54ce6f5286383e81762bff4a05be0560.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.28973411293757745',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 132px; height: 18px;">
......
id="iframe_0.6610864938964944" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/b/e/f/befbb543102492503dd0a37fc2555193.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6610864938964944',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 133px; height: 18px;">
则 id="iframe_0.12234231869370094" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://upload.wikimedia.org/math/5/6/6/566b85f27e5486bb63c0d15775fbd769.png?_=2958150%22%20style=%22border:none;max-width:915px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.12234231869370094',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="box-sizing: border-box; border-width: initial; border-style: none; width: 18px; height: 18px;">即为所求
下面是讲解快速幂的:(By 夜せ︱深 感谢作者)
快速幂取模算法
在网站上一直没有找到有关于快速幂算法的一个详细的描述和解释,这里,我给出快速幂算法的完整解释,用的是C语言,不同语言的读者只好换个位啦,毕竟读C的人较多~
所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模(余)。在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快、计算范围更大的算法,产生了快速幂取模算法。[有读者反映在讲快速幂部分时有点含糊,所以在这里对本文进行了修改,作了更详细的补充,争取让更多的读者一目了然]
我们先从简单的例子入手:求a^b % c = ?
算法1.首先直接地来设计这个算法:
int ans = 1;
for(int i = 1;i<=b;i++)
{
ans = ans * a;
}
ans = ans % c;
这个算法的时间复杂度体现在for循环中,为O(b).这个算法存在着明显的问题,如果a和b过大,很容易就会溢出。
那么,我们先来看看第一个改进方案:在讲这个方案之前,要先有这样一个公式:a^b%c=(a%c)^b%c.这个公式大家在离散数学或者数论当中应该学过,不过这里为了方便大家的阅读,还是给出证明:
引理1:a^b%c = (a%c)^b%c
上面公式为下面公式的引理,即积的取余等于取余的积的取余。
证明了以上的公式以后,我们可以先让a关于c取余,这样可以大大减少a的大小,
于是不用思考的进行了改进:
算法2:
int ans = 1;
a = a % c; //加上这一句
for(int i = 1;i<=b;i++)
{
ans = ans * a;
}
ans = ans % c;
聪明的读者应该可以想到,既然某个因子取余之后相乘再取余保持余数不变,那么新算得的ans也可以进行取余,所以得到比较良好的改进版本。
算法3:
int ans = 1;
a = a % c; //加上这一句
for(int i = 1;i<=b;i++)
{
ans = (ans * a) % c;//这里再取了一次余
}
ans = ans % c;
这个算法在时间复杂度上没有改进,仍为O(b),不过已经好很多的,但是在c过大的条件下,还是很有可能超时,所以,我们推出以下的快速幂算法。
快速幂算法依赖于以下明显的公式,我就不证明了。
那么我们可以得到以下算法:
算法4:
int ans = 1;
a = a % c;
if(b%2==1)
ans = (ans * a) mod c; //如果是奇数,要多求一步,可以提前算到ans中
k = (a*a) % c; //我们取a2而不是a
for(int i = 1;i<=b/2;i++)
{
ans = (ans * k) % c;
}
ans = ans % c;
我们可以看到,我们把时间复杂度变成了O(b/2).当然,这样子治标不治本。但我们可以看到,当我们令k = (a * a) mod c时,状态已经发生了变化,我们所要求的最终结果即为(k)b/2 mod c而不是原来的ab mod c,所以我们发现这个过程是可以迭代下去的。当然,对于奇数的情形会多出一项a mod c,所以为了完成迭代,当b是奇数时,我们通过
ans = (ans * a) % c;来弥补多出来的这一项,此时剩余的部分就可以进行迭代了。
形如上式的迭代下去后,当b=0时,所有的因子都已经相乘,算法结束。于是便可以在O(log b)的时间内完成了。于是,有了最终的算法:快速幂算法。
算法5:快速幂算法
int ans = 1;
a = a % c;
while(b>0)
{
if(b % 2 == 1)
ans = (ans * a) % c;
b = b/2;
a = (a * a) % c;
}
将上述的代码结构化,也就是写成函数:
int PowerMod(int a, int b, int c)
{
int ans = 1;
a = a % c;
while(b>0)
{
if(b % 2 = = 1)
ans = (ans * a) % c;
b = b/2;
a = (a * a) % c;
}
return ans;
}
本算法的时间复杂度为O(logb),能在几乎所有的程序设计(竞赛)过程中通过,是目前最常用的算法之一。
以下内容仅供参考:
扩展:有关于快速幂的算法的推导,还可以从另一个角度来想。
=? 求解这个问题,我们也可以从进制转换来考虑:
将10进制的b转化成2进制的表达式:
注意此处的要么为0,要么为1,如果某一项,那么这一项就是1,这个对应了上面算法过程中b是偶数的情况,为1对应了b是奇数的情况[不要搞反了,读者自己好好分析,可以联系10进制转2进制的方法],我们从依次乘到。对于每一项的计算,计算后一项的结果时用前一项的结果的平方取余。对于要求的结果而言,为时ans不用把它乘起来,[因为这一项值为1],为1项时要乘以此项再取余。这个算法和上面的算法在本质上是一样的,读者可以自行分析,这里我说不多说了,希望本文有助于读者掌握快速幂算法的知识点,当然,要真正的掌握,不多练习是不行的。