UVA, 374 Big Mod

题意:给你三个数b,p,m,求b^p%m的值

思路:快速幂取模算法,分治法

下面代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <math.h>
 4 using namespace std;
 5 int a,b,c;
 6 long res;
 7 bool datecin()
 8 {
 9     if(scanf("%d%d%d",&a,&b,&c)!=EOF)
10         return true;
11     return false;
12 }
13 
14 void x_mmode()
15 {
16     res=1;
17     long long temp=a;
18 
19     while(b!=0)
20     {
21         if(b%2==1)
22             res=(res*temp)%c;
23         b/=2;
24         temp=(temp*temp)%c;
25     }
26     printf("%ld\n",res);
27 }
28 
29 int main()
30 {
31     while(datecin())
32     {
33         x_mmode();
34     }
35     return 0;
36 }

 

posted on 2016-04-19 11:59  八云紫是小loli  阅读(151)  评论(0编辑  收藏  举报

导航