📌  相关文章
📜  X的所有可能值的计数,使得A%X = B

📅  最后修改于: 2021-04-22 06:14:38             🧑  作者: Mango

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

例子:

方法:有三种可能的情况:

  1. 如果A X的任何值都不能满足给定条件。
  2. 如果A = B,则无限解是可能的。因此,将-1打印为X可以是大于A的任何值。
  3. 如果A> B,(A – B)的除数大于B的数目是必需的计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count
// of all possible values for x
// such that (A % x) = B
int countX(int a, int b)
{
    // Case 1
    if (b > a)
        return 0;
  
    // Case 2
    else if (a == b)
        return -1;
  
    // Case 3
    else {
        int x = a - b, ans = 0;
  
        // Find the number of divisors of x
        // which are greater than b
        for (int i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                int d1 = i, d2 = b - 1;
                if (i * i != x)
                    d2 = x / i;
                if (d1 > b)
                    ans++;
                if (d2 > b)
                    ans++;
            }
        }
        return ans;
    }
}
  
// Driver code
int main()
{
    int a = 21, b = 5;
  
    cout << countX(a, b);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
    // Function to return the count 
    // of all possible values for x 
    // such that (A % x) = B 
    static int countX(int a, int b) 
    { 
        // Case 1 
        if (b > a) 
            return 0; 
      
        // Case 2 
        else if (a == b) 
            return -1; 
      
        // Case 3 
        else
        { 
            int x = a - b, ans = 0; 
      
            // Find the number of divisors of x 
            // which are greater than b 
            for (int i = 1; i * i <= x; i++)
            { 
                if (x % i == 0)
                { 
                    int d1 = i, d2 = b - 1; 
                    if (i * i != x) 
                        d2 = x / i; 
                    if (d1 > b) 
                        ans++; 
                    if (d2 > b) 
                        ans++; 
                } 
            } 
            return ans; 
        } 
    } 
  
    // Driver code 
    static public void main (String args[]) 
    { 
        int a = 21, b = 5; 
      
        System.out.println(countX(a, b)); 
      
    } 
}
  
// This code is contributed by AnkitRai01


Python 3
# Python 3 implementation of the approach
  
# Function to return the count
# of all possible values for x
# such that (A % x) = B
def countX( a, b):
    # Case 1
    if (b > a):
        return 0
  
    # Case 2
    elif (a == b):
        return -1
  
    # Case 3
    else:
        x = a - b
        ans = 0
  
        # Find the number of divisors of x
        # which are greater than b
        i = 1
        while i * i <= x:
            if (x % i == 0):
                d1 = i
                d2 = b - 1
                if (i * i != x):
                    d2 = x // i
                if (d1 > b):
                    ans+=1
                if (d2 > b):
                    ans+=1
            i+=1
        return ans
  
# Driver code
if __name__ == "__main__":
    a = 21
    b = 5
  
    print(countX(a, b))
      
    # This code is contributed by ChitraNayal


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
    // Function to return the count 
    // of all possible values for x 
    // such that (A % x) = B 
    static int countX(int a, int b) 
    { 
        // Case 1 
        if (b > a) 
            return 0; 
      
        // Case 2 
        else if (a == b) 
            return -1; 
      
        // Case 3 
        else
        { 
            int x = a - b, ans = 0; 
      
            // Find the number of divisors of x 
            // which are greater than b 
            for (int i = 1; i * i <= x; i++)
            { 
                if (x % i == 0)
                { 
                    int d1 = i, d2 = b - 1; 
                    if (i * i != x) 
                        d2 = x / i; 
                    if (d1 > b) 
                        ans++; 
                    if (d2 > b) 
                        ans++; 
                } 
            } 
            return ans; 
        } 
    } 
  
    // Driver code 
    static public void Main () 
    { 
        int a = 21, b = 5; 
      
        Console.WriteLine(countX(a, b)); 
      
    } 
}
  
// This code is contributed by anuj_67..


输出:
2