[HELP] Explanation for calculate modular inverse of all number from 1 to N in O(n)

Revision en1, by Libraion, 2020-02-22 06:45:32

I suddenly came across these lines of code when looking at others' submissions: (M is a prime)

    inv[1]=1;
    for(i=2;i<N;i++)
        inv[i]=(M-M/i)*inv[M%i]%M;

I tried to google it to understand the formula but ended up finding nothing. So could anyone give me the proof for this?

Thanks a lot <3 <3.

Tags #proof, #help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Libraion 2020-02-22 06:45:32 415 Initial revision (published)