落絮飞雁

顺流而下,把梦做完

快速幂取余算法例题

问题描述:
求 B ^ P % M = ?

代码实现:

#include 
using namespace std;

int main()
{
    freopen("input.txt","r",stdin);
    int B,P,M;
    while (~scanf("%d%d%d",&B,&P,&M))
    {
        int ans = 1;
        B = B % M;
        while (P > 0)
        {
            if (P%2)
                ans = ans * B % M;
            P /= 2;
            B = (B*B) % M;
        }
        printf("%dn",ans);
    }
}

原文标题:快速幂取余算法例题|落絮飞雁的个人网站
原文链接:https://www.luoxufeiyan.com/2014/10/21/%e5%bf%ab%e9%80%9f%e5%b9%82%e5%8f%96%e4%bd%99%e7%ae%97%e6%b3%95%e4%be%8b%e9%a2%98/
授权协议:创作共用 署名-非商业性使用 2.5 中国大陆
除注明外,本站文章均为原创;转载时请保留上述链接。