给定两个整数L和R ,任务是找到(i * j)%2019的最小可能值,其中L≤i
例子:
Input: L = 2020, R = 2040
Output: 2
(2020*2021)%2019 = 2
Input: L = 3, R = 4
Output: 12
方法:
- 如果R – L≥2019,则答案为0,因为我们将得到一个可以被2019整除的数字,该数字为0。
- 如果R – L <2019,则我们可以运行嵌套循环并找到最小值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MOD = 2019;
// Function to return the minimum
// possible value of (i * j) % 2019
int min_modulo(int l, int r)
{
// If we can get a number
// divisible by 2019
if (r - l >= MOD)
return 0;
else {
// Find the minimum value
// by running nested loops
int ans = MOD - 1;
for (int i = l; i <= r; i++) {
for (int j = i + 1; j <= r; j++) {
ans = min(ans, (i * j) % MOD);
}
}
return ans;
}
}
// Driver code
int main()
{
int l = 2020, r = 2040;
cout << min_modulo(l, r);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MOD = 2019;
// Function to return the minimum
// possible value of (i * j) % 2019
static int min_modulo(int l, int r)
{
// If we can get a number
// divisible by 2019
if (r - l >= MOD)
return 0;
else
{
// Find the minimum value
// by running nested loops
int ans = MOD - 1;
for (int i = l; i <= r; i++)
{
for (int j = i + 1; j <= r; j++)
{
ans = Math.min(ans, (i * j) % MOD);
}
}
return ans;
}
}
// Driver code
public static void main(String []args)
{
int l = 2020, r = 2040;
System.out.println(min_modulo(l, r));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
MOD = 2019;
# Function to return the minimum
# possible value of (i * j) % 2019
def min_modulo(l, r) :
# If we can get a number
# divisible by 2019
if (r - l >= MOD) :
return 0;
else :
# Find the minimum value
# by running nested loops
ans = MOD - 1;
for i in range(l, r + 1) :
for j in range(i + 1, r + 1) :
ans = min(ans, (i * j) % MOD);
return ans;
# Driver code
if __name__ == "__main__" :
l = 2020; r = 2040;
print(min_modulo(l, r));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int MOD = 2019;
// Function to return the minimum
// possible value of (i * j) % 2019
static int min_modulo(int l, int r)
{
// If we can get a number
// divisible by 2019
if (r - l >= MOD)
return 0;
else
{
// Find the minimum value
// by running nested loops
int ans = MOD - 1;
for (int i = l; i <= r; i++)
{
for (int j = i + 1; j <= r; j++)
{
ans = Math.Min(ans, (i * j) % MOD);
}
}
return ans;
}
}
// Driver code
public static void Main(String []args)
{
int l = 2020, r = 2040;
Console.WriteLine(min_modulo(l, r));
}
}
// This code is contributed by Rajput-Ji
输出:
2