现在的位置: 首页 > 综合 > 正文

快速幂取余算法

2014年07月22日 ⁄ 综合 ⁄ 共 217字 ⁄ 字号 评论关闭

问题描述:

求 B ^ P % M = ?

代码实现:

#include <cstdio>
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("%d\n",ans);
    }
}

抱歉!评论已关闭.