Solution to a problem P3811 [[template] multiplicative inverse]

First unscrupulous propaganda about the blog wwwwww
Article list - nuclear fusion reactor core - Luo Gubo off

  • Multiplicative inverse:

    For a modulus \ (p \) and a divisor \ (the X-\) , you can often find a special number \ (the y-\) ,
    the \ (\ div x \) instead \ (\ the y-Times \) , you can instead.

    The number \ (the y-\) , called \ (x \) of "inverse", denoted by \ (inv (the X-) \) .

    Example:
    \ (. 3 \ div. 3 \ 4 Times (\ MOD. 11) \)
    \ (=. 3 \ 4 Times \ 4 Times (\ MOD. 11) \)
    \ (= 4 \)
    wherein \ (4 \) is the modulus \ (11 \) when \ (3 \) of the inverse element.

    By definition the example, is easy to see the relationship between the inverse number of the original:
    \ (\ Large X \ INV Times (X) \ equiv. 1 (\ MOD P) \)


  • Solving the inverse mode:

P3811 [template] multiplicative inverse

There \ (4 \) methods can be determined inverse mode
  1. According to the extended Euclid:

    Modulus may not be a prime number

    \ (x \ times inv (x ) \ equiv 1 (\ mod p) \) where \ (x, p \) are known.

    There are not very familiar?

    This is a
    unknowns \ (inv (the X-) \) , the right side of the equation \ (c = 1 \) congruence equation,

    Seeking \ (inv (x) \) values, solving this equation can be congruent.

    About Extended Euclid and Solution of a congruence equation ,
    see: Euclid and Euclid expansion - nuclear fusion reactor core - Luo Gubo off


  1. According to Fermat's little theorem:

    #### only applies to the case of modulo a prime number

    The Fermat's little theorem:
    \ (\ Large X-P. 1 ^ {} \ equiv. 1 \ P PMOD \)

    So it is: $ {X ^}. 1-P \ equiv X \ INV Times (X) \ P $ PMOD

    In addition both sides of the equation with x, there are: \ (INV (X) \ equiv P-X ^ {2} \ PMOD P \)

    After I can find by flash power, determined \ ((x ^ {p- 2}) \% p \) values
    can be obtained directly \ (inv (x) \) values


  1. Recursive method :

    This applies only in modulo of a prime number

    If you want the solution of inverse mode number, lot number, but continuous, then how to do?

    You can use the recursive method.

    \ (i \) mode \ (p \) inverse in the sense of yuan \ (inv (i) \) can be expressed as: 

    \(\large inv(i) = -\lfloor \frac{p}{i}\rfloor \times inv(p\% i)\% p\)

    • prove:

      $p=k\times i + r $ , \((k,r \in Z)\)

      Because: \ (the p-\ equiv 0 (\ the p-MOD) \) ,

      则 : \((k\times i + r) \equiv 0 (\mod p)\)

      So that both sides of the equation by the same: \ (INV (I) \ Times INV (R & lt) \) ,

      Depending on the nature of the inverse, then:

      \((inv(i) \times i )\% p=1\)\((inv(r) \times r )\% p=1\) ;

      Original equation becomes:

      \(k \times 1 \times inv(r) + 1\times inv(i) \equiv 0 (\mod p)\)

      \(inv(i) \equiv -k\times inv(r)\)

      And because: $ P = K \ I + R & lt Times $

      Original equation becomes: \ (INV (I) \ equiv - \ lfloor \ FRAC {P} I {} \ rfloor \ Times INV (P \ I%) (\ MOD P) \)

      即 : \(inv(i) = -\lfloor \frac{p}{i}\rfloor \times inv(p\% i) \% p\)

      And because: \ ((the p-\% i) <i \) ,

      Then: \ (INV (P \% I) \) in obtaining \ (inv (i) \) already before the request may be made recursive

      Original style proved.

      Obviously, obtained \ (inv (i) \) is not necessarily the smallest integer solution to

      To obtain the smallest positive integer solution, we need to add this step operation:

      So \ (- \ lfloor \ frac { p} {i} \ rfloor \) to add a \ (P \) , then its off mode.

      即 : \(inv(i) = (p - \lfloor \frac{p}{i}\rfloor)\times inv(p \% i) \% p;\)


  1. Inverse factorial method:

    #### only applies to the case of modulo a prime number

    \(f(i)=inv(i!)\) , $  g(i)=i! $

    则: \(f(i-1) = f(i)\times i\)

    • prove:
      \(f(i-1)=\frac{1}{\ (i-1)\ !}=\frac{1}{i\ !}\times i =f(i)\times i\)

    Suppose requires \ ([1, n] \ ) in all of the number of inverse

    To obtain \ ([1, n] \ ) factorial the number of all

    Then Fermat's Little Theorem to obtain \ (f (n) \) values

    After delivery to launch \ (f (1 \ sim n ) \) values

    But \ (inv (1! \ Sim n!) \) Is not what we want answers

    The need to continue the conversion.

    Kachi: (! I) $ inv (i) = inv \ times (i-1) $!

    • Proof:
      \(inv(i)=\frac{1}{i}=\frac{1}{i\ !}\times (i-1)\ ! = inv(i!)\times (i-1)!\)

    Conversion as described above,
    can be obtained:

    \(inv(i)=f(i)\times (i-1)!\)

    That was the answer.


  • Return to this topic

Look at a range Data:
\ (. 1 \ n-leqslant \ leqslant. 3 \. 6 Times 10 ^ \) , \ (n-<P <20,000,528 \)
input guaranteed \ (P \) is a prime number.

Obviously, the expansion of Europe and France and fast power method are card.

Due to the continuous section, and modulus \ (P \) is a prime,
can be linear recursion

Mentioned above principle is very clear.

code show as below:

### recursive method :

#include<cstdio>
using namespace std;
long long n,p;
long long ans[5000010]={0,1};
int main()
{
   scanf("%lld%lld",&n,&p);
   printf("1\n");
   for(long long i=2;i<=n;i++) //线性递推
     {
       ans[i]=(long long)(p-p/i)*ans[p%i]%p;
       printf("%lld\n",ans[i]); 
     }
}

### factorial inverse method:

#include<cstdio>
#define ll long long
using namespace std;
ll mul(ll a,ll b,ll mod) //快速幂模板
{
  ll ans=1;
  while(b)
    {
      if(b&1) ans=ans*a%mod;
      a=(a*a)%mod;
      b>>=1;
    }
  return ans%mod;
}
ll n,p;
ll c[5000010]={1};
ll f[5000010];
int main()
{
  scanf("%lld%lld",&n,&p);
  for(int i=1;i<=n;i++)
    c[i]=(c[i-1]*i)%p;
    
  f[n]=mul(c[n],p-2,p); //获得inv(n!)
  
  for(int i=n-1;i>=1;i--) //递推阶乘的逆元
    f[i]=(f[i+1]*(i+1))%p;
    
  for(int j=1;j<=n;j++) //转化并输出
    printf("%lld\n",(f[j]*c[j-1])%p);
}

Guess you like

Origin www.cnblogs.com/luckyblock/p/11456256.html