计算 K 的可能值,使得 A%K = B%K
给定两个整数A和B ,任务是计算K的可能值,使得A%K = B%K 。如果计数是无限的,则打印-1 。
例子:
Input: A = 2, B = 4
Output: 2
Explanation: The set of all possible values of K is {1, 2}.
As 2%1 = 4%1 = 0 and 2%2 = 4%2 = 0.
Input: A = 5, B = 5
Output: -1
Explanation: There are infinite values of K as all possible integer value of K satisfies the given condition.
方法:给定问题可以通过以下观察来解决,即A和B的所有值都可以分为以下两种情况:
- A = B的情况。在这种情况下,所有可能的K整数值都是有效答案。因此,所需计数的值是无限的。
- A > B的情况。在这种情况下,可以观察到,如果K除以(A – B) ,则K的值是有效的。对于B > A 的情况,只需交换A和B的值。
因此,计算K的所有可能值,使其完全除以(A – B) ,即所需值。
下面是上述方法的实现:
C++14
// C++ Program of the above approach
#include
using namespace std;
// Function to find the count of the
// values of K such that A%K = B%K
int countInt(int A, int B)
{
// If the count is Infinite
if (A == B)
return -1;
int diff = abs(A - B);
// Stores the required count
int count = 0;
// Loop to calculate all the
// divisors of diff
for (int i = 1; i * i <= diff; i++) {
if (diff % i == 0) {
// Increment count for i
if (diff == i * i)
count++;
// Increment count for i
// and diff / i both
else
count += 2;
}
}
// Return Answer
return count;
}
// Driver code
int main()
{
int A = 2, B = 4;
cout << countInt(A, B);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the count of the
// values of K such that A%K = B%K
static int countInt(int A, int B)
{
// If the count is Infinite
if (A == B)
return -1;
int diff = Math.abs(A - B);
// Stores the required count
int count = 0;
// Loop to calculate all the
// divisors of diff
for (int i = 1; i * i <= diff; i++) {
if (diff % i == 0) {
// Increment count for i
if (diff == i * i)
count++;
// Increment count for i
// and diff / i both
else
count += 2;
}
}
// Return Answer
return count;
}
// Driver code
public static void main (String[] args) {
int A = 2, B = 4;
System.out.print(countInt(A, B));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code for the above approach
# Function to find the count of the
# values of K such that A%K = B%K
def countInt(A, B):
# If the count is Infinite
if (A == B):
return -1;
diff = abs(A - B);
# Stores the required count
count = 0;
# Loop to calculate all the
# divisors of diff
i = 1;
while((i * i) <= diff):
if (diff % i == 0):
# Increment count for i
if (diff == i * i):
count += 1
# Increment count for i
# and diff / i both
else:
count += 2;
i += 1
# Return Answer
return count;
# Driver code
A = 2
B = 4
print(countInt(A, B));
# This code is contributed by Saurabh Jaiswal
C#
// C# Program of the above approach
using System;
class GFG {
// Function to find the count of the
// values of K such that A%K = B%K
static int countInt(int A, int B)
{
// If the count is Infinite
if (A == B)
return -1;
int diff = Math.Abs(A - B);
// Stores the required count
int count = 0;
// Loop to calculate all the
// divisors of diff
for (int i = 1; i * i <= diff; i++) {
if (diff % i == 0) {
// Increment count for i
if (diff == i * i)
count++;
// Increment count for i
// and diff / i both
else
count += 2;
}
}
// Return Answer
return count;
}
// Driver code
public static int Main()
{
int A = 2, B = 4;
Console.Write(countInt(A, B));
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
输出
2
时间复杂度: O(√(A – B))
辅助空间: O(1)