原文:

给定两个数字 xy ,任务是求10xt7】的任意正除数是10yt11】的整数倍的概率。 注: y 应为< = x. 例:****

输入: x = 2,y = 1 输出: 4/9 说明: 102的正因子为 1,2,4,5,10,20,25,50,100。一共 9 个。 101到 10 2 的倍数为 10,20,50,100。一共 4 个。 p(102的除数是 10 1 的倍数)= 4/9。 输入: x = 99,y = 88 输出: 9/625 解释:t30】a = 1099,b = 1088t35】p(10 的除数 99 是 10 的倍数 88 ) = 9/625

先决条件: 方法: 为了解决问题,我们需要观察以下步骤:

  • 10 x 的所有除数将为:

    (2p 5q*),其中 0 < = p,q<= x

    = t14】

  • 求**10xt3 t5

    10x= 2x* 5x

    t14】的质因数数**

  • 因此,10 x 的除数总数将为: ( x 1 ) * ( x 1 )

  • 现在,考虑 10 y 的所有倍数,它们可以是 10 x 的潜在约数。它们的形式还有:

    (2a* 5b,其中 y < = a,b<= x

    = t12】

  • 所以10xt3【也是10yt7】的倍数的潜在约数为(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

python 3

# 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 

java 描述语言


output: 

4/9

时间复杂度: o(log(n))