1037-模运算

描述

 

给定整数a,b,n,要求计算(a^b)mod n

输入

 

多组数据,每组数据一行,为三个用空格隔开的整数a,b,n

1<=a<=5000,0<=b<=10^8,1<=n<=5000000

输出

 

每组数据输出一行,为所求值

样例输入

2 3 5

2 2 4

样例输出

3

0

#include<iostream>
using namespace std;
__int64 mod(__int64 a,__int64 b,__int64 c) 
{ 
    __int64 m=1; 
    while(b>=1) 
    { 
        if(b%2==1) 
            m=a*m%c; 
        a=a*a%c; 
        b=b/2; 
    } 
    return m; 
}
int main()
{
    __int64 a,b,c,result;
    while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
    {
        if(b==0) printf("%I64d
",1%c);
        else 
        {
            result=mod(a,b,c);
            printf("%I64d
",result);
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/Rosanna/p/3436624.html