快速幂取余算法例题

问题描述:
求 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);
    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注