The method of taking Modulus + Product function + inverse element of Java higher Power
This article introduces the relevant knowledge of "the method of Java higher power module + product function + inverse". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Meaning of the title: sum of all positive factors of 2004 ^ x (S) to 29; output result
Original title link
Topic resolution: analyze the reference source: click on the open link
Factor sum
The factor of 6 is 1, 2, 3, 6, and the sum of 6 is s (6) = 1, 2, 3, 6, 12.
The factor of 20 is 1, 2, 4, 5, 10, 20, and the sum of 20 is s (20) = 1, 2, 4, 5, 10, 20, 42.
The factor of 2 is 1BI 2; the sum of the factors of 2 is s (2) = 1x 2m 3.
The factor of 3 is 1BI 3; the sum of the factors of 3 is s (3) = 1 million 3 4.
The sum of the factors of 4 is
S (4) = 1 / 2 / 4 / 7
The sum of the factors of 5 is
S (5) = 1 / 5 / 6
S (6) = s (2) * s (3) = 3 / 4 / 12
S (20) = s (4) * s (5) = 7 / 6 / 42
Is that a coincidence?
Then look at s (50) = 1 "2" 5 "10" 25 "50" 93 "3" 31 "s (2) * s (25), s (25) = 1" 5 "25" 31.
This is called a product function in number theory, when gcd (a) b) = 1 s (a) = s (a) * s (b)
If p is a prime
S (p ^ n) = 1 ^ p + p ^ 2 +... + p ^ n = (p ^ (naugh1)-1) / (pmur1) (1)
Example hdu1452 Happy2004
Calculation factor and s (2004 ^ X) mod 29
2004 = 2 ^ 2 * 3 * 167
S (2004 ^ X)) = (s (2 ^ 2X)) * (s (3 ^ X)) * (s (167 ^ X)
167) = 22
S (2004 ^ X)) = (s (2 ^ 2X)) * (s (3 ^ X) * (s (22 ^ X)
Astats (2 ^ 2X) = (2 ^ (2X+1)-1) / / according to (1)
Banners (3 ^ X) = (3 ^ (Xero1)-1) / 2max / according to (1)
Clocks (22 ^ X) = (22 ^ (Xero1)-1) / 21max / according to (1)
% algorithm
1. (aqb)% p = (a% p) * (b% p)
% algorithm
2. (a * b)% p = (a * b ^ (- 1)% p)
B ^ (- 1) Yes
Inverse element of b (% p)
The inverse element of 2 is 15 (), because 2 / 15 / 30 / 29 / 1 / 29
The inverse element of 21 is 18 (), because 21 / 18 / 378% 29 = 1% 29
therefore
A = (powi (2 and 2))-1)% 29
B = (powi (3) X-1 (29)-1) * 15% 29
C = (powi (22 ~ Xerox _ 1 ~ 29)-1) * 18% 29
Ans= (aqb) 293c 29
Data expansion: 1.
High-power fast module link
two。 A product function in number theory: an arithmetic function for a positive integer n
F (n), if f (1) = 1 and f (ab) = f (a) f (b) is called a product function in number theory. If
For a product function f (n), even if an and b are not prime, there is f (ab) = f (a) f (b), then it is called complete product. If n is expressed as a prime factorization
3. Find the inverse element:
When calculating (a _ Mod)% Mod, it is often necessary to first calculate the inverse p of b%Mod (the condition that b has an inverse is that gcd (br _ Mod) = = 1, obviously there must be an inverse of a prime), and then (aplomp)% Mod
Get the result c. this
The inverse p of b satisfies (bounded p)% Mod=1. Let's start with a simple proof:
(a Mod=c;)% Mod=c)% Mod=1; = = "(b) * (b)% Mod=c; = =" (ACPP)% Mod=c
We can see the correctness of the conclusion from the above, of course, here b needs to be a factor of A. Next we need to know how we calculate the inverse p based on b and Mod. The extended Euclidean algorithm, as we all know, is known to be a, b, and to find a set of solutions (x _ ray y) to make a*x+b*y=1. The x obtained here is the inverse of a% b and y is the inverse of b% a (think about why? Put b or an on both sides of the equation.
The following reasons are explained:
Modular m multiplicative inverse
Definition: if there is an integer b satisfying ab ≡ 1 (mod m), then b is the inverse of modular m multiplication of a.
Theorem: an exists multiplicative inverse of modulo m if and only if gcd (a) m = 1.
Adequacy:
Because
Gcd (aPerm) = 1
According to Euler's theorem, there are
A ^ φ (m) ≡ 1 (mod m)
therefore
A * a ^ (φ (m)-1) mod m = 1
So there is a modular m multiplicative inverse of a, that is, a ^ (φ (m)-1).
Necessity:
Suppose that the multiplicative inverse of a module m is b, then
Ab ≡ 1 (mod m)
So
Ab = km + 1
So
1 = ab-km
According to Euclid's theorem, there are
Gcd (aPerm) = 1
It is known by reason:
For ax + by = 1, we can see that x is the multiplicative inverse of a modular b and y is the multiplicative inverse of b modular a.
On the other hand, to calculate the multiplicative inverse of a module b is equivalent to finding the minimum positive integer solution of x of ax + by = 1, which is solved by linear indefinite equation.
Specific reference: http://blog.csdn.net/synapse7/article/details/9901195 calls ExtGcd (bmae Modjie x Magi y), and x is the inverse p of b%Mod.
There is another way to find the inverse p of b%Mod, that is, p = b ^ (Mod-2)% Mod, because b ^ (Mod-1)% Mod=1 (here Mod is a prime). Error analysis: 1:if (yellow1) ans*=x%29;// mistakenly misplaced ans=x*x%292. The data type should be _ _ int64
Code implementation:
# include#includeusing namespace std;typedef _ int64 ll;ll powmol (ll XLY) / / how to find the module x ^ ymod 29 {ll ans=1; x% 29; while (y) {if (y) ans*=x%29;//y is odd While (scanf ("% I64d", & x), x) {a = (powmol (2) powmol (2)-1)% 29; b = (powmol (3)) * 15% 29; c = (powmol (22))-1) * 18% 29; c = (powmol (22))-1) * 18% 29 Printf ("% I64d\ n", (aquib)% 29*c%29);} return 0;} "the method of Java higher power module + product function + inverse" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!