📌  相关文章
📜  计算 K 的可能值,使得 A%K = B%K

📅  最后修改于: 2022-05-13 01:56:06.365000             🧑  作者: Mango

计算 K 的可能值,使得 A%K = B%K

给定两个整数AB ,任务是计算K的可能值,使得A%K = B%K 。如果计数是无限的,则打印-1

例子:

方法:给定问题可以通过以下观察来解决,即AB的所有值都可以分为以下两种情况:

  • A = B的情况。在这种情况下,所有可能的K整数值都是有效答案。因此,所需计数的值是无限的。
  • A > B的情况。在这种情况下,可以观察到,如果K除以(A – B) ,则K的值是有效的。对于B > A 的情况,只需交换AB的值。

因此,计算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)