Inversion element method

Fermat's Little Theorem: If p is a prime number, a is a positive integer and can not be divisible by p, then A p-1 == 1 (MOD p)

Expand Fermat's Little Theorem: A the p- == A (the p-MOD)

Euler's theorem: For any prime n and a set [Phi] (n) is the number of positive integers less than n and prime with n, there is a [Phi] (n) ==. 1 (MOD n)

Euler's theorem of expansion: A [Phi] (n-) + 1'd == A (n-MOD)

Seeking multiplicative inverse action: dividing a number of modulo time again, may be multiplied by the number inverse modulo this number again (to be converted to a multiplication division)

Why this is equivalent to: For the (a / b)% mod this formula, is not equivalent to ((a% mod) / ( b% mod))% mod (for example: a = 3, b = 2 , mod = 3), but can be written as (a * b -1 ) MOD%, where b -1 represents the inverse element b. This is the role of the inverse

A reference to the title meter garlic face-off:

What is the multiplicative inverse: a * x = 1 (MOD C), then x is called a multiplicative inverse of the C

EG A =. 4, C = Inverse. 7 membered x = 2;

  4 * 2 = 1 (mod 7) 12/4% 7 = (12 * 2)% 7 (division transfer method)

Getting started with a question to learn three in the template: https://www.luogu.org/problem/P3811

 

A template:

Linear Recursive Method :( play table recursion formulas: INV [I] = (PP / I) * INV [% P I]% P )

This method is the fastest, but it takes more than space

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <ctime>
 6 #include <climits>
 7 #include <utility>
 8 #include <deque>
 9 #include <queue>
10 #include <map>
11 #include <algorithm>
12 #include <functional>
13 using namespace std;
14 typedef long long ll;
15 
16 ll inv[3000006];
17 int main()
18 {
19     ll a, p;
20     while(~scanf("%lld %lld", &a, &p) )
21     {
22         memset(inv, 0, sizeof(inv));
23         inv[0] = 1;
24         inv[1] = 1;
25         for(ll i=2; i<=a; i++ )
26             inv[i] = (p-p/i)*inv[p%i]%p;
27         for(ll i=1; i<=a; i++ )
28             printf("%lld\n", inv[i]);
29     }
30     return 0;
31 }

Template II:

Expand Euclidean algorithm

This question is a TLE I use this method, the slow times

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <ctime>
 6 #include <climits>
 7 #include <utility>
 8 #include <deque>
 9 #include <queue>
10 #include <map>
11 #include <algorithm>
12 #include <functional>
13 using namespace std;
14 typedef long long ll;
15 
16 void exgcd(ll a, ll b, ll &x, ll &y)
17 {
18     if( b==0 )
19     {
20         x=1;
21         y=0;
22         return ;
23     }
24     exgcd(b, a%b, y, x);
25     y -= (a/b)*x;
26     return ;
27 }
28 
29 int main()
30 {
31     ll a, p;
32     ll x, y;
33     while( ~ scanf("%lld %lld", &a, &p) )
34     {
35         for(ll i=1; i<=a; i++ )
36         {
37             exgcd(i, p, x, y);
38             x = (x%p+p)%p;
39             printf("%lld\n", x);
40         }
41     }
42     return 0;
43 }

Template III:

Fermat's little theorem fast algorithm :( power (a, p-2, p)), provided that ap relatively prime, this is also the TLE, the slowest

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <ctime>
 6 #include <climits>
 7 #include <utility>
 8 #include <deque>
 9 #include <queue>
10 #include <map>
11 #include <algorithm>
12 #include <functional>
13 using namespace std;
14 typedef long long ll;
15 
16 ll fpm(ll x, ll power, ll mod)
17 {
18     ll ans = 1;
19     for( ; power; power>>=1,(x*=x)%=mod )
20     if( power&1 )
21         (ans*=x)%=mod;
22     return ans;
23 }
24 
25 int main()
26 {
27     ll a, p;
28     ll x, y;
29     while( ~ scanf("%lld %lld", &a, &p) )
30     {
31         for(ll i=1; i<=a; i++ )
32         {
33             printf("%lld\n", fpm(i,p-2,p));
34         }
35     }
36     return 0;
37 }

 

Guess you like

Origin www.cnblogs.com/wsy107316/p/11520602.html