Dados dos números X e Y , la tarea es encontrar la probabilidad de que un divisor positivo arbitrario de 10 X sea un múltiplo entero de 10 Y.
Nota: Y debe ser <= X.
Ejemplos:
Entrada: X = 2, Y = 1
Salida: 4/9
Explicación:
Los divisores positivos de 10 2 son 1, 2, 4, 5, 10, 20, 25, 50, 100. Un total de 9.
Múltiplos de 10 1 hasta 10 2 son 10, 20, 50, 100. Un total de 4.
P(divisor de 10 2 es múltiplo de 10 1 ) = 4/9.
Entrada: X = 99, Y = 88
Salida: 9/625
Explicación:
A = 10 99 , B = 10 88
P(divisor de 10 99 es múltiplo de 10 88 ) = 9/625
Prerrequisitos: Número total de divisores de un número
Enfoque:
Para resolver el problema, necesitamos observar los siguientes pasos:
- Todos los divisores de 10 X serán de la forma:
(2 P * 5 Q ), donde 0 <= P, Q <= X
- Encuentra el número de factores primos de 10 X
10 X = 2 X * 5 X
- Por lo tanto, el número total de divisores de 10 X será: ( X + 1 ) * ( X + 1 ) .
- Ahora, considera todos los múltiplos de 10 Y que pueden ser divisores potenciales de 10 X . También son de la forma:
( 2 A * 5 B ), donde Y <= A, B <= X.
- Entonces, el conteo de divisores potenciales de 10 X que también son múltiplos de 10 Y es ( X – Y + 1 ) * ( X – Y + 1 ) .
- Por lo tanto, la probabilidad requerida es (( X – Y + 1 ) * ( X – Y + 1 )) / (( X + 1 ) * ( X + 1 )) . Calcular el valor de la expresión para valores dados de X e Y nos da la probabilidad requerida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the probability // of an arbitrary positive divisor of // Xth power of 10 to be a multiple of // Yth power of 10 #include <bits/stdc++.h> using namespace std; #define int long long int // Function to calculate and print // the required probability void prob(int x, int y) { // Count of potential divisors // of X-th power of 10 which are // also multiples of Y-th power // of 10 int num = abs(x - y + 1) * abs(x - y + 1); // Count of divisors of X-th // power of 10 int den = (x + 1) * (x + 1); // Calculate GCD int gcd = __gcd(num, den); // Print the reduced // fraction probability cout << num / gcd << "/" << den / gcd << endl; } // Driver Code signed main() { int X = 2, Y = 1; prob(X, Y); return 0; }
Java
// Java program to find the probability // of an arbitrary positive divisor of // Xth power of 10 to be a multiple of // Yth power of 10 import java.util.*; class GFG{ // Function to calculate and print // the required probability static void prob(int x, int y) { // Count of potential divisors // of X-th power of 10 which are // also multiples of Y-th power // of 10 int num = Math.abs(x - y + 1) * Math.abs(x - y + 1); // Count of divisors of X-th // power of 10 int den = (x + 1) * (x + 1); // Calculate GCD int gcd = __gcd(num, den); // Print the reduced // fraction probability System.out.print(num / gcd + "/" + den / gcd + "\n"); } static int __gcd(int a, int b) { return b == 0 ? a : __gcd(b, a % b); } // Driver code public static void main(String[] args) { int X = 2, Y = 1; prob(X, Y); } } // This code is contributed by amal kumar choubey
Python3
# Python3 program to find the probability # of an arbitrary positive divisor of # Xth power of 10 to be a multiple of # Yth power of 10 from math import * # Function to calculate and print # the required probability def prob(x, y): # Count of potential divisors # of X-th power of 10 which are # also multiples of Y-th power # of 10 num = abs(x - y + 1) * abs(x - y + 1) # Count of divisors of X-th # power of 10 den = (x + 1) * (x + 1) # Calculate GCD gcd1 = gcd(num, den) # Print the reduced # fraction probability print(num // gcd1, end = "") print("/", end = "") print(den // gcd1) # Driver Code if __name__ == '__main__': X = 2 Y = 1 prob(X, Y) # This code is contributed by Surendra_Gangwar
C#
// C# program to find the probability // of an arbitrary positive divisor of // Xth power of 10 to be a multiple of // Yth power of 10 using System; class GFG{ // Function to calculate and print // the required probability static void prob(int x, int y) { // Count of potential divisors // of X-th power of 10 which are // also multiples of Y-th power // of 10 int num = Math.Abs(x - y + 1) * Math.Abs(x - y + 1); // Count of divisors of X-th // power of 10 int den = (x + 1) * (x + 1); // Calculate GCD int gcd = __gcd(num, den); // Print the reduced // fraction probability Console.Write(num / gcd + "/" + den / gcd + "\n"); } static int __gcd(int a, int b) { return b == 0 ? a : __gcd(b, a % b); } // Driver code public static void Main(string[] args) { int X = 2, Y = 1; prob(X, Y); } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript program to find the probability // of an arbitrary positive divisor of // Xth power of 10 to be a multiple of // Yth power of 10 // Function to calculate and print // the required probability function prob(x, y) { // Count of potential divisors // of X-th power of 10 which are // also multiples of Y-th power // of 10 var num = Math.abs(x - y + 1) * Math.abs(x - y + 1); // Count of divisors of X-th // power of 10 var den = (x + 1) * (x + 1); // Calculate GCD var gcd = __gcd(num, den); // Print the reduced // fraction probability document.write(num / gcd + "/" + den / gcd + "\n"); } function __gcd(a, b) { return b == 0 ? a : __gcd(b, a % b); } // Driver Code var X = 2, Y = 1; prob(X, Y); // This code is contributed by Khushboogoyal499 </script>
4/9
Complejidad del tiempo: O(log(N))
Publicación traducida automáticamente
Artículo escrito por greenindia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA