传送门:http://acm.fzu.edu.cn/problem.php?pid=1759

Accept: 1161    Submit: 3892
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).

Input

There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.

Output

For each testcase, output an integer, denotes the result of A^B mod C.

Sample Input

3 2 4
2 10 1000

Sample Output

1 24
 
 
题意:
给出a,b,c,求a的b次方对c取模的结果
 
思路:
题意好理解,但是主要是a,b,c的范围比较大,所以这里面需要一个降幂公式:
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 typedef long long LL;
 4 
 5 const int MAXX = 1e6;
 6 LL c,d;
 7 char b[MAXX+10];
 8 
 9 LL quick_pow(LL a, LL b, LL mod) {
10     LL ans = 1;
11     while(b > 0) {
12         if(b&1) {
13             b--;
14             ans = ans *a %mod;
15         }
16         b>>=1;
17         a=a*a%mod;
18     }
19     return ans;
20 }
21 
22 LL Euler (LL n) {  
23     LL rea=n;  
24     for(int i=2; i*i<=n; i++)  
25         if(n%i==0)
26         {  
27             rea=rea-rea/i;  
28             do  
29                 n/=i;
30             while(n%i==0);  
31         }  
32     if(n>1)  
33         rea=rea-rea/n;  
34     return rea;  
35 }
36 LL jieguo(LL a,char b[],LL c,LL mod) {
37     LL sum = 0;
38     sum = (b[0] - '0') % mod;
39     LL len = strlen(b);
40     for(int i = 1;i<len;i++) {
41         sum*=10;
42         sum=(sum+b[i]-'0')%mod;
43     }
44     return quick_pow(a,sum+mod,c);
45 }
46 int main() {
47 
48     
49     while(scanf("%lld%s%lld", &c, b, &d)!=EOF){
50         LL ola=Euler(d);
51         printf("%lld\n",jieguo(c,b,d,ola));
52     }
53     return 0;
54 }