Given two numbers X and Y, the task is to find the probability that an arbitrary positive divisor of 10X is an integral multiple of 10Y.
Note: Y should be <= X.
Examples:
Input: X = 2, Y = 1
Output: 4/9
Explanation:
Positive divisors of 102 are 1, 2, 4, 5, 10, 20, 25, 50, 100. A total of 9.
Multiples of 101 upto 102 are 10, 20, 50, 100. A total of 4.
P(divisor of 102 is a multiple of 101) = 4/9.
Input: X = 99, Y = 88
Output: 9/625
Explanation:
A = 1099, B = 1088
P(divisor of 1099 is a multiple of 1088) = 9/625
Pre-requisites: Total number of divisors of a number
Approach:
In order to solve the problem, we need to observe the steps below:
- All divisors of 10X will be of the form:
(2 P * 5 Q), where 0 <= P, Q <= X
- Find the number of Prime factors of 10X
10X = 2X * 5X
- Hence, total number of divisors of 10X will be: ( X + 1 ) * ( X + 1 ).
- Now, consider all multiples of 10Y which can be potential divisors of 10X. They are also of the form:
( 2 A * 5 B ), where Y <= A, B <= X.
- So, count of potential divisors of 10X which are also multiples of 10Y is ( X – Y + 1 ) * ( X – Y + 1 ).
- Hence, required probability is (( X – Y + 1 ) * ( X – Y + 1 )) / (( X + 1 ) * ( X + 1 )). Computing the value of the expression for given values of X and Y gives us the required probability.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long int
void prob( int x, int y)
{
int num = abs (x - y + 1)
* abs (x - y + 1);
int den = (x + 1) * (x + 1);
int gcd = __gcd(num, den);
cout << num / gcd << "/"
<< den / gcd << endl;
}
signed main()
{
int X = 2, Y = 1;
prob(X, Y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void prob( int x, int y)
{
int num = Math.abs(x - y + 1 ) *
Math.abs(x - y + 1 );
int den = (x + 1 ) * (x + 1 );
int gcd = __gcd(num, den);
System.out.print(num / gcd + "/" +
den / gcd + "\n" );
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void main(String[] args)
{
int X = 2 , Y = 1 ;
prob(X, Y);
}
}
|
Python3
from math import *
def prob(x, y):
num = abs (x - y + 1 ) * abs (x - y + 1 )
den = (x + 1 ) * (x + 1 )
gcd1 = gcd(num, den)
print (num / / gcd1, end = "")
print ( "/" , end = "")
print (den / / gcd1)
if __name__ = = '__main__' :
X = 2
Y = 1
prob(X, Y)
|
C#
using System;
class GFG{
static void prob( int x, int y)
{
int num = Math.Abs(x - y + 1) *
Math.Abs(x - y + 1);
int den = (x + 1) * (x + 1);
int gcd = __gcd(num, den);
Console.Write(num / gcd + "/" +
den / gcd + "\n" );
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void Main( string [] args)
{
int X = 2, Y = 1;
prob(X, Y);
}
}
|
Javascript
<script>
function prob(x, y)
{
var num = Math.abs(x - y + 1) *
Math.abs(x - y + 1);
var den = (x + 1) * (x + 1);
var gcd = __gcd(num, den);
document.write(num / gcd + "/" +
den / gcd + "\n" );
}
function __gcd(a, b)
{
return b == 0 ? a : __gcd(b, a % b);
}
var X = 2, Y = 1;
prob(X, Y);
</script>
|
Time Complexity: O(log(N))