Abstract: This paper mainly introduces the Euler's Theorem in number theory, and then introduces the extension and application of the Euler's theorem. Combined with examples, it shows how to use the extended Euler's Theorem to implement power reduction modulo. In number theory,
Euler's theorem,(Also called ferma-
Euler's Theorem) Is a property theorem about the same remainder. Before learning about Euler's theorem, let's take a look at the ferma's theorem:
A is a positive integer that cannot be divisible by the prime number P. Then there is a ^ (p-1) 1_1 (mod P)
Euler gave the promotion form
If N, A is a positive integer and is of mutual quality, here, Phi (n) indicates the number of numbers that are less than or equal to M and N. We can see that ferma's theorem is a special case of Euler's theorem.
First, let's look at a basic example. Make a = 3, n = 5, and the two numbers are mutually unique. In a positive integer smaller than 5, the numbers of the five elements are 1, 2, 3, and 4, so Phi (5) = 4. Calculation: A ^ {PHI (n)} = 3 ^ 4 = 81, while 81 = 80 + 1 limit 1 (mod 5 ). The result is consistent with the theorem.
Then use the Euler's Theorem to simplify the modulo operation of the power. For example, to calculate the single digit of 7 ^ {222}, the remainder of 7 ^ {222} is actually calculated. 7 and 10 [vegetarian], and PHI (10) = 4. We know 7 ^ 4 then 1 (mod 10) by Euler's theorem ). So 7 ^ {222} = (7 ^ 4) ^ 55*(7 ^ 2) then 1 ^ {55} * 7 ^ 2 then 49 then 9 (mod 10 ). Therefore, the number of digits for the 7 ^ {222} is 9.
Finally, we extend the Euler's theorem to the situation where a and m are not mutually qualitative.
The following is a method for solving the Phi (n) value:
1 ll euler_phi(ll n) { 2 ll k = (ll)sqrt(n + 0.5); 3 ll ans = n; 4 for(int i = 2; i <= k; i++) { 5 if(n % i == 0) { 6 ans = ans / i * (i - 1); 7 while(n % i == 0) n /= i; 8 } 9 }10 if(n > 1) ans = ans / n * (n - 1);11 return ans;12 }
Calculation of PHI (1), Phi (2), Phi (3),... PHI (n) using a method similar to the screening method)
1 const int maxn = 100001; 2 LL Phi [maxn]; 3 void phi_table (ll n) {// calculate the Euler's function value from 1 to n 4 for (ll I = 2; I <= N; I ++) 5 Phi [I] = 0; 6 Phi [1] = 1; 7 for (ll I = 2; I <= N; I ++) {8 If (! Phi [I]) {9 for (ll j = I; j <= N; j + = I) {10 if (! Phi [J]) Phi [J] = J; 11 Phi [J] = Phi [J]/I * (I-1); 12} 13} 14} 15}
You can use the following code to print the Phi function values from 1 to 10:
1 # include <iostream> 2 # include <cstdio> 3 # include <cmath> 4 using namespace STD; 5 6 typedef long ll; 7 8 LL euler_phi (ll n) {9 LL k = (LL) SQRT (n + 0.5); 10 LL ans = N; 11 for (INT I = 2; I <= K; I ++) {12 if (N % I = 0) {13 ans = ANS/I * (I-1); 14 While (N % I = 0) N/= I; 15} 16} 17 if (n> 1) ans = ANS/N * (N-1); 18 return ans; 19} 20 21 const int maxn = 100001; 22 ll Phi [maxn]; 23 void pH I _table (ll n) {// calculate the Euler's function value from 1 to n 24 for (ll I = 2; I <= N; I ++) 25 Phi [I] = 0; 26 Phi [1] = 1; 27 for (ll I = 2; I <= N; I ++) {28 If (! Phi [I]) {29 for (ll j = I; j <= N; j + = I) {30 if (! Phi [J]) Phi [J] = J; 31 Phi [J] = Phi [J]/I * (I-1 ); 32} 33} 34} 35} 36 37 int main () 38 {39 40 for (ll I = 1; I <= 10; I ++) 41 printf ("% i64d", euler_phi (I); 42 puts (""); 43 44 phi_table (10); 45 for (ll I = 1; I <= 10; I ++) 46 printf ("% i64d", Phi [I]); 47 puts (""); 48 return 0; 49}
View code
Take an example: fzu 1759 super a ^ B mod C
Question
Calculate a ^ B mod C, where 1 <= A, C <= 000000000,1 <= B <= 10 ^ 1000000.
Solutions
Read data using arrays and perform remainder operation cyclically without division.
1 #include <bits/stdc++.h> 2 #define ll __int64 3 using namespace std; 4 5 char a[1000006]; 6 ll x, z; 7 ll quickpow(ll x, ll y, ll z) 8 { 9 ll ans = 1;10 while(y)11 {12 if(y&1)13 ans = ans * x % z;14 x = x * x % z;15 y >>= 1;16 }17 return ans;18 }19 ll phi(ll n)20 {21 ll i, rea = n;22 for(i = 2; i * i <= n; i++)23 {24 if(n % i == 0)25 {26 rea = rea - rea / i;27 while(n % i == 0)28 n /= i;29 }30 }31 if(n > 1)32 rea = rea-rea/n;33 return rea;34 }35 int main()36 {37 while(scanf("%lld %s %lld",&x,a,&z) != EOF)38 {39 ll len = strlen(a);40 ll p = phi(z);41 ll ans = 0;42 for(ll i = 0;i < len; i++)43 ans = (ans*10 + a[i]-‘0‘)%p;44 ans += p;45 printf("%lld\n", quickpow(x, ans, z));46 }47 return 0;48 }
Euler's theorem, extended Euler's Theorem and Its Application (Euler's power reduction method)