鉴于两个数字X和Y,任务是找到10 X的任意正除数为10的Y的整数倍的概率。
注意: Y应该<=X。
例子:
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
先决条件:一个数的除数总数
方法:
为了解决该问题,我们需要遵循以下步骤:
- 10 X的所有除数将采用以下形式:
(2 P * 5 Q), where 0 <= P, Q <= X
- 求出10 X的素数个数
10X = 2X * 5X
- 因此,除数的总数为10 X将为: (X +1)*(X +1) 。
- 现在,考虑10 Y的所有倍数,它们可能是10 X的可能除数。它们也具有以下形式:
( 2 A * 5 B ), where Y <= A, B <= X.
- 因此, 10 X的潜在除数(也是10 Y的倍数)的计数为(X – Y + 1)*(X – Y + 1) 。
- 因此,所需概率为((X – Y + 1)*(X – Y + 1))/((X + 1)*(X + 1)) 。对于给定的X和Y值,计算表达式的值可得出所需的概率。
下面是上述方法的实现:
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
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
输出:
4/9
时间复杂度: O(log(N))