📜  10 ^ X的任意正因数是10 ^ Y的整数倍的概率

📅  最后修改于: 2021-04-23 20:07:41             🧑  作者: Mango

鉴于两个数字XY,任务是找到10 X的任意正除数为10Y的整数倍的概率。

注意: Y应该<=X。
例子:

先决条件:一个数的除数总数

方法:
为了解决该问题,我们需要遵循以下步骤:

  • 10 X的所有除数将采用以下形式:
  • 求出10 X的素数个数
  • 因此,除数的总数为10 X将为: (X +1)*(X +1)
  • 现在,考虑10 Y的所有倍数,它们可能是10 X的可能除数。它们也具有以下形式:
  • 因此, 10 X的潜在除数(也是10 Y的倍数)的计数为(X – Y + 1)*(X – Y + 1)
  • 因此,所需概率为((X – Y + 1)*(X – Y + 1))/((X + 1)*(X + 1)) 。对于给定的XY值,计算表达式的值可得出所需的概率。

下面是上述方法的实现:

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))