Problem Description
输入b,p,k的值,求b^p mod k的值。
Input
输入有多组数据,每组数据为一行三个数b,p,k,其中b,p,k*k为长整型数。
Output
对于每组数据输出b^p mod k的值。
Sample Input
2 10 9
Sample Output
2^10 mod 9=7
//关键字:快速幂取模
//标程:
#include<stdio.h> #include<string.h> __int64 f(__int64 a,__int64 b,__int64 c) { __int64 sum=1; while(b) { if(b%2==1) sum=(sum*a)%c; a=(a*a)%c; b/=2; } return sum; } int main() { //freopen("a.txt","r",stdin); __int64 a,b,c; while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF) printf("%I64d^%I64d mod %I64d=%I64d\n",a,b,c,f(a,b,c)); return 0; }