给定三个整数L 、 R和N ,任务是找到(i * j) % N的最小可能值,其中L ≤ i < j ≤ R 。
例子:
Input: L = 2020, R = 2040, N = 2019
Output: 2
Explanation: (2020 * 2021) % 2019 = 2
Input: L = 15, R = 30, N = 15
Output: 0
Explanation: If one of the elements of the pair is 15, then the product of all such pairs will be divisible by 15. Therefore, the remainder will be 0, which is minimum possible.
方法:给定的问题可以通过找到L和R之间的差异来解决。如果差异至少为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)