FZU-1759 Super A^B mod C---欧拉降幂&指数循环节
题目链接:
https://cn.vjudge.net/problem/FZU-1759
题目大意:
求A^B%C
解题思路:
注意,这里long long需要用%I64读入,不能用%lld
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 using namespace std; 6 typedef long long ll; 7 const int maxn = 2e6 + 10; 8 int euler_phi(int n)//求单个 9 { 10 int m = (int)sqrt(n + 0.5); 11 int ans = n; 12 for(int i = 2; i <= m; i++)if(n % i == 0) 13 { 14 ans = ans / i * (i - 1); 15 while(n % i == 0)n /= i; 16 } 17 if(n > 1)ans = ans / n * (n - 1); 18 return ans; 19 } 20 ll mul(ll a, ll b, ll m) 21 //求a*b%m 22 { 23 ll ans = 0; 24 a %= m; 25 while(b) 26 { 27 if(b & 1)ans = (ans + a) % m; 28 b /= 2; 29 a = (a + a) % m; 30 } 31 return ans; 32 } 33 ll pow(ll a, ll b, ll m) 34 { 35 ll ans = 1; 36 a %= m; 37 while(b) 38 { 39 if(b & 1)ans = mul(a, ans, m); 40 b /= 2; 41 a = mul(a, a, m); 42 } 43 ans %= m; 44 return ans; 45 } 46 ll super_pow(ll a, char b[], ll c) 47 { 48 ll t = euler_phi(c), n = strlen(b), tot = 0; 49 for(int i = 0; i < n; i++) 50 { 51 tot = tot * 10 + b[i] - '0'; 52 tot %= t; 53 } 54 tot += t; 55 return pow(a, tot, c); 56 } 57 char b[maxn]; 58 int main() 59 { 60 ll a, c; 61 while(~scanf("%I64d%s%I64d", &a, b, &c)) 62 { 63 printf("%I64d\n", super_pow(a, b, c)); 64 } 65 return 0; 66 }
越努力,越幸运