📌  相关文章
📜  对于给定范围内的任何对,可能的最小乘积模 N

📅  最后修改于: 2021-10-26 05:16:29             🧑  作者: Mango

给定三个整数LRN ,任务是找到(i * j) % N的最小可能值,其中L ≤ i < j ≤ R

例子:

方法:给定的问题可以通过找到LR之间的差异来解决。如果差异至少为N ,则结果将为0 。否则,迭代范围[L, R]并找到最小乘积。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#define ll long long
using namespace std;
 
// Function to return the minimum
// possible value of (i * j) % N
void minModulo(int L, int R, int N)
{
    if (R - L < N) {
 
        // Stores the minimum remainder
        int ans = INT_MAX;
 
        // Iterate from L to R
        for (ll i = L; i <= R; i++)
 
            // Iterate from L to R
            for (ll j = L; j <= R; j++)
                if (i != j)
                    ans = min(0ll + ans,
                              (i * j) % N);
 
        // Print the minimum value
        // of remainder
        cout << ans;
    }
 
    // If R - L >= N
    else {
        cout << 0;
    }
}
 
// Driver Code
int main()
{
    int L = 6, R = 10, N = 2019;
    minModulo(L, R, N);
 
    return 0;
}


Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG
{
 
    // Function to return the minimum
    // possible value of (i * j) % N
    static void minModulo(int L, int R, int N)
    {
        if (R - L < N)
        {
 
            // Stores the minimum remainder
            int ans = Integer.MAX_VALUE;
 
            // Iterate from L to R
            for (int i = L; i <= R; i++)
 
                // Iterate from L to R
                for (int j = L; j <= R; j++)
                    if (i != j)
                        ans = Math.min(ans, (i * j) % N);
 
            // Print the minimum value
            // of remainder
            System.out.println(ans);
        }
 
        // If R - L >= N
        else {
            System.out.println(0);
        }
    }
   
    // Driver Code
    public static void main(String[] args)
    {
 
        int L = 6, R = 10, N = 2019;
        minModulo(L, R, N);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to return the minimum
# possible value of (i * j) % N
def minModulo(L, R, N):
     
    if (R - L < N):
         
        # Stores the minimum remainder
        ans = 10**9
 
        # Iterate from L to R
        for i in range(L, R + 1):
             
            # Iterate from L to R
            for j in range(L, R + 1):
                if (i != j):
                    ans = min(ans, (i * j) % N)
 
        # Print the minimum value
        # of remainder
        print (ans)
 
    # If R - L >= N
    else:
        print (0)
 
# Driver Code
if __name__ == '__main__':
     
    L, R, N = 6, 10, 2019
    minModulo(L, R, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to return the minimum
// possible value of (i * j) % N
static void minModulo(int L, int R, int N)
{
    if (R - L < N)
    {
         
        // Stores the minimum remainder
        int ans = Int32.MaxValue;
 
        // Iterate from L to R
        for(int i = L; i <= R; i++)
 
            // Iterate from L to R
            for(int j = L; j <= R; j++)
                if (i != j)
                    ans = Math.Min(ans, (i * j) % N);
 
        // Print the minimum value
        // of remainder
        Console.WriteLine(ans);
    }
 
    // If R - L >= N
    else
    {
        Console.WriteLine(0);
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    int L = 6, R = 10, N = 2019;
    minModulo(L, R, N);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
42

时间复杂度: O((R – L) 2 )
辅助空间: O(1)