【nyist 102 次方求模】

次方求模

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

求a的b次方对c取余的值

 
输入
第一行输入一个整数n表示测试数据的组数(n<100)
每组测试只有一行,其中有三个正整数a,b,c(1=<a,b,c<=1000000000)
输出
输出a的b次方对c取余之后的结果
样例输入
3
2 3 5
3 100 10
11 12345 12345
样例输出
3
1
10481
来源
[张云聪]原创
上传者
张云聪
 1 // Project name : 102
 2 // File name    : main.cpp
 3 // Author       : Izumu
 4 // Date & Time  : Wed Jul 18 10:45:51 2012
 5 
 6 
 7 #include <iostream>
 8 #include <stdio.h>
 9 #include <string>
10 #include <cmath>
11 #include <algorithm>
12 using namespace std;
13 
14 int mod(int k, int x, int c)
15 {
16     int a = 1;
17     long long int r = k;
18     while (x)
19     {
20         if (x & 1)
21         {
22             a = (a * r) % c;
23         }
24         r = ((r % c) * (r % c)) % c;
25         x = x >> 1;
26     }
27     return a;
28 }
29 
30 int main()
31 {
32     int n, a, b, c;
33     cin >> n;
34     while (n--)
35     {
36         cin >> a >> b >> c;
37         cout << mod(a, b, c) << endl;
38     }
39     
40     return 0;
41 }
42 
43 // end 
44 // ism 
原文地址:https://www.cnblogs.com/ismdeep/p/2597016.html