📌  相关文章
📜  计算 K 的可能值,使得 X – 1 和 Y – 1 模 K 相同

📅  最后修改于: 2021-10-27 06:21:06             🧑  作者: Mango

给定两个数字XY ,任务是找到满足方程(X – 1) % K = (Y – 1) % K的整数 K 的数量。

例子:

处理方法:按照以下步骤解决问题:

  • 这个想法是找到XY之间的绝对差异
  • 找出计算出的绝对差的除数并将该计数打印为所需答案。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count integers K
// satisfying given equation
int condition(int a, int b)
{
    // Calculate the absoluter
    // difference between a and b
    int d = abs(a - b), count = 0;
 
    // Iterate till sqrt of the difference
    for (int i = 1; i <= sqrt(d); i++) {
        if (d % i == 0) {
            if (d / i == i)
                count += 1;
            else
                count += 2;
        }
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
 
    int x = 2, y = 6;
    cout << condition(x, y) << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
  
// Function to count integers K
// satisfying given equation
static int condition(int a, int b)
{
     
    // Calculate the absoluter
    // difference between a and b
    int d = Math.abs(a - b), count = 0;
  
    // Iterate till sqrt of the difference
    for(int i = 1; i <= Math.sqrt(d); i++)
    {
        if (d % i == 0)
        {
            if (d / i == i)
                count += 1;
            else
                count += 2;
        }
    }
  
    // Return the count
    return count;
}
  
// Driver Code
public static void main (String[] args)
{
    int x = 2, y = 6;
 
    System.out.println(condition(x, y));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to count integers K
# satisfying given equation
def condition(a, b):
     
    # Calculate the absoluter
    # difference between a and b
    d = abs(a - b)
    count = 0
 
    # Iterate till sqrt of the difference
    for i in range(1, d + 1):
        if i * i > d:
            break
        if (d % i == 0):
            if (d // i == i):
                count += 1
            else:
                count += 2
 
    # Return the count
    return count
 
# Driver Code
if __name__ == '__main__':
 
    x = 2
    y = 6
     
    print(condition(x, y))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
class GFG{
  
// Function to count
// integers K satisfying
// given equation
static int condition(int a,
                     int b)
{   
  // Calculate the absoluter
  // difference between a and b
  int d = Math.Abs(a - b), count = 0;
 
  // Iterate till sqrt of
  // the difference
  for(int i = 1;
          i <= Math.Sqrt(d); i++)
  {
    if (d % i == 0)
    {
      if (d / i == i)
        count += 1;
      else
        count += 2;
    }
  }
 
  // Return the count
  return count;
}
  
// Driver Code
public static void Main(String[] args)
{
  int x = 2, y = 6;
  Console.WriteLine(condition(x, y));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
3

时间复杂度: O(√abs(X – Y))
辅助空间: O(1)