📜  从1到n的模乘逆

📅  最后修改于: 2021-05-04 20:42:31             🧑  作者: Mango

给出一个正整数n,相对于一个大质数,例如’素数’,找到从1到n的所有整数的模乘逆。

a的模乘法逆是一个整数“ x”,使得。

a x ≡ 1 (mod prime) 

例子 :

Input : n = 10, prime = 17
Output : 1 9 6 13 7 3 5 15 2 12
Explanation :
For 1, modular inverse is 1 as (1 * 1)%17 is 1
For 2, modular inverse is 9 as (2 * 9)%17 is 1
For 3, modular inverse is 6 as (3 * 6)%17 is 1
....... 

Input : n = 5, prime = 7
Output : 1 4 5 2 3

一个简单的解决方案是为每个数字一个一个地找到模块化逆。

C++
// C++ program to find modular inverse of
// all numbers from 1 to n using naive
// method
#include
using namespace std;
  
// A naive method to find modular
// multiplicative inverse of 'a'
// under modulo 'prime'
int modInverse(int a, int prime)
{
    a = a % prime;
    for (int x=1; x


Java
// Java program to find modular inverse of
// all numbers from 1 to n using naive
// method
import java.io.*;
 
class GFG {
     
    // A naive method to find modular
    // multiplicative inverse of 'a'
    // under modulo 'prime'
    static int modInverse(int a, int prime)
    {
        a = a % prime;
        for (int x = 1; x  


Python3
# Python 3 program to find
# modular inverse of
# all numbers from 1
# to n using naive
# method
 
 
# A naive method to find modular
# multiplicative inverse of 'a'
# under modulo 'prime'
 
def modInverse(a, prime) :
    a = a % prime
    for x in range(1,prime) :
        if ((a*x) % prime == 1) :
            return x
       
    return -1
     
  
def printModIverses(n, prime) :
    for i in range(1,n+1) :
        print( modInverse(i, prime) ,end= " ")
   
# Driver Program
n = 10
prime = 17
 
printModIverses(n, prime)
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to find modular inverse of
// all numbers from 1 to n using naive
// method
using System;
 
class GFG {
     
    // A naive method to find modular
    // multiplicative inverse of 'a'
    // under modulo 'prime'
    static int modInverse(int a, int prime)
    {
        a = a % prime;
        for (int x = 1; x 


PHP


Javascript


C++
// CPP code to find modular inverse
// from 1 to n w.r.t a big prime number
#include 
using namespace std;
 
// Function to calculate modular
// inverse using D.P
void modularInverse(int n, int prime)
{
    int dp[n + 1];
    dp[0] = dp[1] = 1;
    for (int i = 2; i <= n; i++)
        dp[i] = dp[prime % i] *
               (prime - prime / i) % prime;   
 
    for (int i = 1; i <= n; i++)
        cout << dp[i] << ' ';   
}
 
// Driver code
int main()
{
    int n = 10, prime = 17;
    modularInverse(n, prime);
    return 0;
}


Java
// Java code to find modular inverse
// from 1 to n w.r.t a big prime number
import java.io.*;
 
class GFG {
 
    // Function to calculate modular
    // inverse using D.P
    static void modularInverse(int n, int prime)
    {
        int dp[]=new int[n + 1];
        dp[0] = dp[1] = 1;
        for (int i = 2; i <= n; i++)
            dp[i] = dp[prime % i] *
                (prime - prime / i) % prime;
     
        for (int i = 1; i <= n; i++)
            System.out.print(dp[i] + " ");
    }
 
    // Driver Program
    public static void main(String args[])
    {
        int n = 10, prime = 17;
        modularInverse(n, prime);
    }
}
 
 
// This code is contributed by Nikita Tiwari.


Python3
# Python 3 code to find
# modular inverse
# from 1 to n w.r.t a
# big prime number
 
# Function to calculate modular
# inverse using D.P
def modularInverse( n, prime) :
 
    dp =[0]*(n+1)
    dp[0] = dp[1] = 1
    for i in range( 2, n+1) :
        dp[i] = dp[prime % i] *(prime - prime // i) % prime
  
    for i in range( 1, n+1) :
        print(dp[i] ,end=" ")
         
  
# Driver code
n = 10
prime = 17
 
modularInverse(n, prime)
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# code to find modular inverse
// from 1 to n w.r.t a big prime number
using System;
 
class GFG {
 
    // Function to calculate modular
    // inverse using D.P
    static void modularInverse(int n, int prime)
    {
        int []dp=new int[n + 1];
        dp[0] = dp[1] = 1;
         
        for (int i = 2; i <= n; i++)
            dp[i] = dp[prime % i] *
                (prime - prime / i) % prime;
     
        for (int i = 1; i <= n; i++)
            Console.Write(dp[i] + " ");
    }
 
    // Driver Program
    public static void Main()
    {
        int n = 10, prime = 17;
         
        modularInverse(n, prime);
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

1 9 6 13 7 3 5 15 2 12

一个有效的解决方案基于扩展的Euclid算法。
扩展的欧几里得算法找到整数系数x和y,使得:

ax + by = gcd(a, b)

Let us put b = prime, we get
  ax + prime * y = gcd(a, prime)

We know gcd(a, prime) = 1 because
on of the numbers is prime. So we
know
  ax + prime * y = 1

Since prime * y is a multiple of prime,
x is modular multiplicative inverse of a.

 ax  ≡ 1 (mod prime) 
 

我们可以使用以下表达式来递归地找到x(有关详细信息,请参见扩展的Euclid算法)。
扩展的欧几里得算法使用递归调用gcd(b%a,a)计算的结果来更新gcd(a,b)的结果。令通过递归调用计算的x和y的值分别为x prev和y prev 。 x和y使用以下表达式更新。

x = yprev - ⌊prime/a⌋ * xprev
y = xprev

我们使用上面的关系来使用先前计算的值来计算逆。

inverse(a) = (inverse(prime % a) *
              (prime - prime/a)) % prime

我们使用使用上述递归结构的动态编程方法。
动态方法:
dp [1] = 1
dp [2] = dp [17%2] *(17-17 / 2)%17 = 9
dp [3] = dp [17%3] *(17-17 / 3)%17 = 6
等等..

C++

// CPP code to find modular inverse
// from 1 to n w.r.t a big prime number
#include 
using namespace std;
 
// Function to calculate modular
// inverse using D.P
void modularInverse(int n, int prime)
{
    int dp[n + 1];
    dp[0] = dp[1] = 1;
    for (int i = 2; i <= n; i++)
        dp[i] = dp[prime % i] *
               (prime - prime / i) % prime;   
 
    for (int i = 1; i <= n; i++)
        cout << dp[i] << ' ';   
}
 
// Driver code
int main()
{
    int n = 10, prime = 17;
    modularInverse(n, prime);
    return 0;
}

Java

// Java code to find modular inverse
// from 1 to n w.r.t a big prime number
import java.io.*;
 
class GFG {
 
    // Function to calculate modular
    // inverse using D.P
    static void modularInverse(int n, int prime)
    {
        int dp[]=new int[n + 1];
        dp[0] = dp[1] = 1;
        for (int i = 2; i <= n; i++)
            dp[i] = dp[prime % i] *
                (prime - prime / i) % prime;
     
        for (int i = 1; i <= n; i++)
            System.out.print(dp[i] + " ");
    }
 
    // Driver Program
    public static void main(String args[])
    {
        int n = 10, prime = 17;
        modularInverse(n, prime);
    }
}
 
 
// This code is contributed by Nikita Tiwari.

Python3

# Python 3 code to find
# modular inverse
# from 1 to n w.r.t a
# big prime number
 
# Function to calculate modular
# inverse using D.P
def modularInverse( n, prime) :
 
    dp =[0]*(n+1)
    dp[0] = dp[1] = 1
    for i in range( 2, n+1) :
        dp[i] = dp[prime % i] *(prime - prime // i) % prime
  
    for i in range( 1, n+1) :
        print(dp[i] ,end=" ")
         
  
# Driver code
n = 10
prime = 17
 
modularInverse(n, prime)
 
# This code is contributed
# by Nikita Tiwari.

C#

// C# code to find modular inverse
// from 1 to n w.r.t a big prime number
using System;
 
class GFG {
 
    // Function to calculate modular
    // inverse using D.P
    static void modularInverse(int n, int prime)
    {
        int []dp=new int[n + 1];
        dp[0] = dp[1] = 1;
         
        for (int i = 2; i <= n; i++)
            dp[i] = dp[prime % i] *
                (prime - prime / i) % prime;
     
        for (int i = 1; i <= n; i++)
            Console.Write(dp[i] + " ");
    }
 
    // Driver Program
    public static void Main()
    {
        int n = 10, prime = 17;
         
        modularInverse(n, prime);
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

1 9 6 13 7 3 5 15 2 12

时间复杂度: O(n)
辅助空间: O(n)