📜  在模N等于N的情况下,具有N个乘法逆的N的最接近的较小数

📅  最后修改于: 2021-04-22 09:16:19             🧑  作者: Mango

给定质数N ,任务是找到比N更小的数字,以使模N下的数字的模乘逆数等于该数本身。

例子:

天真的方法:解决此问题的最简单方法是遍历从1到N的所有自然数,并找到最大数,以使模N下的数字的模乘逆数等于数字本身。

时间复杂度: O(N * log N)
辅助空间: O(1)

高效方法:为了优化上述方法,该思想基于以下观察结果:

因此,要解决该问题,只需将N – 1作为要求的答案即可。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the nearest
// smaller number satisfying
// the condition
int clstNum(int N)
{
    return (N - 1);
}
 
// Driver Code
int main()
{
    int N = 11;
    cout << clstNum(N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to find the nearest
// smaller number satisfying
// the condition
static int clstNum(int N){ return (N - 1); }
 
// Driver Code
public static void main(String[] args)
{
    int N = 11;
     
    System.out.println(clstNum(N));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program to implement
# the above approach
 
# Function to find the nearest
# smaller number satisfying
# the condition
def clstNum(N):
  return (N - 1)
 
# Driver Code
if __name__ == '__main__':
   
  N = 11
   
  print(clstNum(N))
     
# This code is contributed by akhilsaini


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find the nearest
// smaller number satisfying
// the condition
static int clstNum(int N){ return (N - 1); }
 
// Driver Code
public static void Main()
{
    int N = 11;
     
    Console.Write(clstNum(N));
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
10

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