Recuento de todos los valores posibles de X tales que A % X = B

Dados dos enteros A y B . La tarea es encontrar el conteo de todos los valores posibles X tal que A % X = B . Si hay un número infinito de valores posibles, imprima -1 .
Ejemplos: 
 

Entrada: A = 21, B = 5 
Salida:
8 y 16 son los únicos valores válidos para X.
Entrada: A = 5, B = 5 
Salida: -1 
X puede tener cualquier valor > 5 
 

Enfoque: Hay tres casos posibles: 
 

  1. Si A < B , ningún valor de X puede satisfacer la condición dada.
  2. Si A = B entonces son posibles infinitas soluciones. Entonces, imprima -1 como X puede ser cualquier valor mayor que A .
  3. Si A > B , entonces el número de divisores de (A – B) que son mayores que B es el conteo requerido.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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..

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the count
// of all possible values for x
// such that (A % x) = B
function countX(a, b)
{
    // Case 1
    if (b > a)
        return 0;
 
    // Case 2
    else if (a == b)
        return -1;
 
    // Case 3
    else {
        let x = a - b, ans = 0;
 
        // Find the number of divisors of x
        // which are greater than b
        for (let i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                let d1 = i, d2 = b - 1;
                if (i * i != x)
                    d2 = parseInt(x / i);
                if (d1 > b)
                    ans++;
                if (d2 > b)
                    ans++;
            }
        }
        return ans;
    }
}
 
// Driver code
    let a = 21, b = 5;
 
    document.write(countX(a, b));
 
</script>
Producción: 

2

 

Complejidad del tiempo: O(sqrt(a – b))

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *