Recuento de pares que satisfacen la condición dada

Dados dos enteros A y B , la tarea es calcular el número de pares (a, b) tales que 1 ≤ a ≤ A, 1 ≤ b ≤ B y la ecuación (a * b) + a + b = concat(a , b) es verdadero donde conc(a, b) es la concatenación de a y b (por ejemplo, conc(12, 23) = 1223, conc(100, 11) = 10011). Tenga en cuenta que a y b no deben contener ceros a la izquierda.

Ejemplos: 

Entrada: A = 1, B = 12 
Salida:
Solo existe un par (1, 9) que satisface 
la ecuación ((1 * 9) + 1 + 9 = 19)

Entrada: A = 2, B = 8 
Salida:
No existe ningún par que satisfaga la ecuación. 
 

Planteamiento: Se puede observar que lo anterior (a * b + a + b = conc(a, b)) solo se cumplirá cuando los dígitos de un entero ≤ b contengan solo 9 . Simplemente, calcule la cantidad de dígitos (≤ b) que contienen solo 9 y multiplíquelos con el número entero a .

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 number of
// pairs satisfying the equation
int countPair(int a, int b)
{
    // Converting integer b to string
    // by using to_string function
    string s = to_string(b);
 
    // Loop to check if all the digits
    // of b are 9 or not
    int i;
    for (i = 0; i < s.length(); i++) {
 
        // If '9' doesn't appear
        // then break the loop
        if (s[i] != '9')
            break;
    }
 
    int result;
 
    // If all the digits of b contain 9
    // then multiply a with string length
    // else multiply a with string length - 1
    if (i == s.length())
        result = a * s.length();
    else
        result = a * (s.length() - 1);
 
    // Return the number of pairs
    return result;
}
 
// Driver code
int main()
{
    int a = 5, b = 101;
 
    cout << countPair(a, b);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
 
// Function to return the number of
// pairs satisfying the equation
static int countPair(int a, int b)
{
    // Converting integer b to String
    // by using to_String function
    String s = String.valueOf(b);
 
    // Loop to check if all the digits
    // of b are 9 or not
    int i;
    for (i = 0; i < s.length(); i++)
    {
 
        // If '9' doesn't appear
        // then break the loop
        if (s.charAt(i) != '9')
            break;
    }
 
    int result;
 
    // If all the digits of b contain 9
    // then multiply a with String length
    // else multiply a with String length - 1
    if (i == s.length())
        result = a * s.length();
    else
        result = a * (s.length() - 1);
 
    // Return the number of pairs
    return result;
}
 
// Driver code
public static void main(String[] args)
{
    int a = 5, b = 101;
 
    System.out.print(countPair(a, b));
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 implementation of the approach
 
# Function to return the number of
# pairs satisfying the equation
def countPair(a, b):
     
    # Converting integer b to string
    # by using to_function
    s = str(b)
 
    # Loop to check if all the digits
    # of b are 9 or not
    i = 0
    while i < (len(s)):
 
        # If '9' doesn't appear
        # then break the loop
        if (s[i] != '9'):
            break
        i += 1
 
    result = 0
 
    # If all the digits of b contain 9
    # then multiply a with length
    # else multiply a with length - 1
    if (i == len(s)):
        result = a * len(s)
    else:
        result = a * (len(s) - 1)
 
    # Return the number of pairs
    return result
 
# Driver code
a = 5
b = 101
 
print(countPair(a, b))
 
# This code is contributed by mohit kumar 29

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the number of
// pairs satisfying the equation
static int countPair(int a, int b)
{
    // Converting integer b to String
    // by using to_String function
    String s = String.Join("", b);
 
    // Loop to check if all the digits
    // of b are 9 or not
    int i;
    for (i = 0; i < s.Length; i++)
    {
 
        // If '9' doesn't appear
        // then break the loop
        if (s[i] != '9')
            break;
    }
 
    int result;
 
    // If all the digits of b contain 9
    // then multiply a with String length
    // else multiply a with String length - 1
    if (i == s.Length)
        result = a * s.Length;
    else
        result = a * (s.Length - 1);
 
    // Return the number of pairs
    return result;
}
 
// Driver code
public static void Main(String[] args)
{
    int a = 5, b = 101;
 
    Console.Write(countPair(a, b));
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the number of
// pairs satisfying the equation
function countPair(a, b)
{
     
    // Converting integer b to string
    // by using to_string function
    var s = (b.toString());
 
    // Loop to check if all the digits
    // of b are 9 or not
    var i;
    for(i = 0; i < s.length; i++)
    {
         
        // If '9' doesn't appear
        // then break the loop
        if (s[i] != '9')
            break;
    }
 
    var result;
 
    // If all the digits of b contain 9
    // then multiply a with string length
    // else multiply a with string length - 1
    if (i == s.length)
        result = a * s.length;
    else
        result = a * (s.length - 1);
 
    // Return the number of pairs
    return result;
}
 
// Driver code
var a = 5, b = 101;
document.write(countPair(a, b));
 
// This code is contributed by rutvik_56
 
</script>
Producción: 

10

 

Complejidad temporal: O(b)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por nitinkr8991 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 *