FZU1759(SummerTrainingDay04-B 欧拉降幂公式)
Problem 1759 Super A^B mod C
Accept: 1056 Submit: 3444
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
Source
FZU 2009 Summer Training IV--Number Theory1 //2017-08-04 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #define ll long long 7 8 using namespace std; 9 10 const int N = 1000010; 11 char b[N]; 12 ll a, c; 13 14 ll quick_pow(ll a, ll n, ll MOD){ 15 ll ans = 1; 16 while(n){ 17 if(n&1)ans = ans*a%MOD; 18 a = a*a%MOD; 19 n>>=1; 20 } 21 return ans; 22 } 23 24 ll phi(ll n){ 25 ll ans = n; 26 for(ll i = 2; i*i <= n; i++){ 27 if(n%i==0){ 28 ans -= ans/i; 29 while(n%i==0) 30 n /= i; 31 } 32 } 33 if(n > 1)ans = ans - ans/n; 34 return ans; 35 } 36 37 int main() 38 { 39 while(scanf("%lld%s%lld", &a, b, &c)!=EOF){ 40 ll len = strlen(b); 41 ll MOD = phi(c), num = 0; 42 for(ll i = 0; i < len; i++) 43 num = (num*10 + b[i]-'0')%MOD; 44 num += MOD; 45 printf("%lld\n", quick_pow(a, num, c)); 46 } 47 return 0; 48 }