Fast power template (Luo Gu 1226)

Luo Valley 1226

Fast power, calculated b ^ p% k, template int, note that you may want to open long long

1  @ Fast power: P% calculated K ^ B (luogu1226) 
2 #include <cstdio>
 . 3  
. 4  the using  namespace STD;
 . 5  
. 6  int Power ( int B, int P, int K)      // calculate% K P ^ B 
. 7  {
 . 8      int ANS = . 1 , m = B% K;
 . 9      the while (P> 0 )
 10      {
 . 11          IF (P% 2 == . 1 ) ANS ANS = m *% K;
 12 is          P = >> . 1 ; m = m * m%k;
13     }
14     return ans%k;
15 }
16 int main()
17 {
18     int b,p,k;
19     scanf("%d%d%d",&b,&p,&k);
20     printf("%d^%d mod %d=%d",b,p,k,power(b,p,k));
21     return 0;
22 }

 

Guess you like

Origin www.cnblogs.com/Currier/p/11375467.html