原文:

给定一个表示骰子点数的整数 n ,任务是找出出现在 n 掷出的骰子的顶面上的数字乘积为的。所有 n 骰子必须同时投掷。

示例:

输入 : n = 2 输出: 6 / 36 解释: 在同时投掷 n(=2)个骰子时,乘积等于素数的 n(=2)个骰子顶面上的可能结果为:{(1,2),(1,3),(1,5),(2,1),(3,1),(1),(3,1),(5,1)}。 因此,有利结果的计数= 6,样本空间的计数= 36 因此,所需的输出为(6 / 36)

输入:n = 3 t3】输出: 9 / 216

天真方法:解决这个问题最简单的方法是通过同时投掷 n 个骰子,在 n 个骰子的顶面上生成所有可能的结果,并针对每个可能的结果检查顶面上的数字乘积是否为。如果发现为真,则递增计数器。最后,把顶面上的数字乘积作为素数打印出来。

时间复杂度:o(6n n)* 辅助空间: o(1)

高效方法:优化上述方法的思路是,仅当(n–1)数为 1 且剩余数为素数时,利用 n 数的乘积为素数的事实。以下是观察结果:

如果 n 个数的乘积是素数,那么(n–1)个数的值必须是 1,剩余的数必须是素数。 范围[1,6]内素数的总数为 3。 因此,顶面上的 n 个数的乘积为素数的结果总数= 3 * n . p(e)= n(e)/n(s) p(e)=得到 n 个骰子顶面上的数的乘积为素数的概率。 n(e) =有利结果总数= 3 * n n(s) =样本空间中事件总数= 6nt8】

按照以下步骤解决此问题:

  • 初始化一个变量,比如 n_e 来存储有利结果的计数。
  • 初始化一个变量,比如 n_s 来存储样本空间的计数。
  • 更新 n_e = 3 * n
  • 更新n _ s = 6nt3】。
  • 最后打印 (n_e / n_s) 的值。

下面是上述方法的实现

c

// c   program to implement
// the above approach
#include 
using namespace std;
// function to find the value
// of power(x, n)
long long int power(long long int x,
                    long long int n)
{
    // stores the value
    // of (x ^ n)
    long long int res = 1;
    // calculate the value of
    // power(x, n)
    while (n > 0) {
       // if n is odd
       if(n & 1) {
           //update res
           res = (res * x);
       }
       //update x
       x = (x * x);
       //update n
       n = n >> 1;
    }
    return res;
}
// function to find the probability of
// obtaining a prime number as the
// product of n thrown dices
void probablityprimeprod(long long int n)
{
    // stores count of favorable outcomes
    long long int n_e = 3 * n;
    // stores count of sample space
    long long int n_s = power(6, n);
    // print the required probability
    cout<

java 语言(一种计算机语言,尤用于创建网站)

// java program to implement
// the above approach
import java.util.*;
class gfg{
// function to find the value
// of power(x, n)
static int power(int x, int n)
{
    // stores the value
    // of (x ^ n)
    int res = 1;
    // calculate the value of
    // power(x, n)
    while (n > 0)
    {
        // if n is odd
        if (n % 2 == 1)
        {
            // update res
            res = (res * x);
        }
        // update x
        x = (x * x);
        // update n
        n = n >> 1;
    }
    return res;
}
// function to find the probability of
// obtaining a prime number as the
// product of n thrown dices
static void probablityprimeprod(int n)
{
    // stores count of favorable outcomes
    int n_e = 3 * n;
    // stores count of sample space
    int n_s = power(6, n);
    // print the required probability
    system.out.print(n_e   " / "   n_s);
}
// driver code
public static void main(string[] args)
{
    int n = 2;
    probablityprimeprod(n);
}
}
// this code is contributed by amit katiyar

python 3

# python3 program to implement
# the above approach
# function to find the value
# of power(x, n)
def power(x, n):
    # stores the value
    # of (x ^ n)
    res = 1
    # calculate the value of
    # power(x, n)
    while (n > 0):
        # if n is odd
        if (n % 2 == 1):
            # update res
            res = (res * x)
        # update x
        x = (x * x)
        # update n
        n = n >> 1
    return res
# function to find the probability of
# obtaining a prime number as the
# product of n thrown dices
def probablityprimeprod(n):
    # stores count of favorable outcomes
    n_e = 3 * n
    # stores count of sample space
    n_s = power(6, n)
    # print required probability
    print(n_e, " / ", n_s)
# driver code
if __name__ == '__main__':
    n = 2
    probablityprimeprod(n)
# this code is contributed by 29ajaykumar

c

// c# program to implement
// the above approach
using system;
class gfg{
// function to find the
// value of power(x, n)
static int power(int x,
                 int n)
{   
  // stores the value
  // of (x ^ n)
  int res = 1;
  // calculate the value
  // of power(x, n)
  while (n > 0)
  {
    // if n is odd
    if (n % 2 == 1)
    {
      // update res
      res = (res * x);
    }
    // update x
    x = (x * x);
    // update n
    n = n >> 1;
  }
  return res;
}
// function to find the probability
// of obtaining a prime number as
// the product of n thrown dices
static void probablityprimeprod(int n)
{   
  // stores count of favorable
  // outcomes
  int n_e = 3 * n;
  // stores count of sample
  // space
  int n_s = power(6, n);
  // print the required
  // probability
  console.write(n_e   " / "   n_s);
}
// driver code
public static void main(string[] args)
{
  int n = 2;
  probablityprimeprod(n);
}
}
// this code is contributed by princi singh

java 描述语言


output: 

6 / 36

时间复杂度:o(log2n) t8】辅助空间: o( 1 )