On the multiplicative inverse (fast power algorithm, linear algorithm)

Issues into

Is well known, there is a magical thing called "combination number", abbreviated C, often appears in a variety of topics in number
is also well known, the number of combinations is the formula
C (n, m) = n ! / ((Nm)! * M !)
However, since this number can be large, even the legendary __int128 are kept high, so we often need a modulus
so formulas combination becomes
C (n, m) = n ! / ((nm) ! * m!)% p
At this time, the problem came, we all know that (a / b)% p! = a% p / b% p, this time, we need to use the multiplicative inverse

Inverse defined

We require that if a * x mod p = 1, and gcd (a, b) = 1 we define: x is the inverse of a, denoted a -1
we can say x is a reciprocal sense in mod b

Inverse element method

There are many ways the inverse element, such as Euclid, fast power, as well as linear recurrence method of expansion, first introduced here fast power algorithm and linear algorithm

Fast Power Algorithm

It should be a reference to Theorem: Fermat's little theorem (or Euler's theorem)
Let us talk about Fermat's little theorem
when it is time to prime numbers p, A p-1 MOD p = 1
Euler's theorem is a ^ phi (p) mod p 1 =
Phi (I) n represents from 1 to n and the number of prime numbers
, however, when we found that when p is prime number, phi (p) = p- 1, so Fermat's little theorem is Euler's theorem part of
that is, when it is time to prime numbers p, a -1 MOD p = a p-2 MOD p
using fast power to solve it
here by default we will quickly power up

Example: In seeking a inverse mod p meaning
the code:

inline int read(){
	int s=0,w=1;
	char c=gc;
	return s*w;
int n,p;
inline int Qpow(int base,int ind){
	int ans=1;
	return ans;
int main()
	return 0;

It seems like most of them are quick power
of this method can be linked to the number of combinations, as we introduce the same problem, there will be explained later, there is no more first repeat

Linear algorithm

Linear Recursive be used to calculate the inverse number of consecutive string
Here there are just examples, direct reference

Luo Gu P3811 [template] multiplicative inverse

This is a linear recurrence of the problem, in fact, I think he is not good out of this question, because I really have not seen a few channels with linear recursion problem doing
it may be I was too dishes
This question is the use of fast power of o (nlogn) should not pass the
so we can use this method
first of all we have this formula. 1 -1 MOD P =. 1
and provided p = k * i + r ( 0 <r <i <p) that is, k is the quotient p / i, r is the remainder

Then this formula into mod p significance under'll get:

(k*i+r)mod p = 0

Is then multiplied by I -1 R & lt -1

k * r-1 + i-1 mod p = 0

I -1 and * R & lt -k -1 for p congruence

in -1和- (p / i) * (p v i) -1 mod p

The core code is very short


Pressure line Dafa Is Good

to sum up

Multiplicative inverse number theory is a very important piece, many provinces the number of topics selected to be used in multiplicative inverse

We must take control

Published 18 original articles · won praise 3 · Views 1270

Guess you like

Origin blog.csdn.net/devout_/article/details/89414440