📜  求 N 次迭代后从第 k 列中选择元素的概率

📅  最后修改于: 2021-09-17 07:05:53             🧑  作者: Mango

给定一个 N*M 阶矩阵 M,其中Mij表示 j 在第 i 行中的出现。任务是在应用给定操作后找到给定 k 在最后一行中出现的概率:

  • 从第一行开始,我们从整行的任何列中选择一个元素,并将其添加到下一行的同一列中。我们重复这个直到最后一行。

例子:

Input: k = 1, M[][] = 
{{0, 1},
{1, 1}}   

Output:0.666667

Input: k = 1, M[][] = 
{{0, 1},
{1, 1}}  

Output: 0.333333

方法 :

  • 预先计算一个 sum[] 数组,该数组存储第一行所有元素的总和。
  • 用第一行 M[][] 元素填充 dp[][] 的第一行。
  • 计算从特定行的每一列中选择一个元素的概率,并将其添加到相应的列中。
  • 此外,在更新 dp[][] 矩阵的值的同时更新行的 Sum[]。
  • 最后,给定 k 的 dp[n][k] 值是所有迭代后选择元素 k 的概率所需的值。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
#define n 4
#define m 4
using namespace std;
 
// Function to calculate probability
float calcProbability(int M[][m], int k)
{
    // declare dp[][] and sum[]
    float dp[m][n], sum[n];
 
    // precalculate the first row
    for (int j = 0; j < n; j++) {
        dp[0][j] = M[0][j];
        sum[0] += dp[0][j];
    }
 
    // calculate the probability for
    // each element and update dp table
    for (int i = 1; i < m; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] += dp[i - 1][j] / sum[i - 1] + M[i][j];
            sum[i] += dp[i][j];
        }
    }
 
    // return result
    return dp[n - 1][k - 1] / sum[n - 1];
}
 
// Driver code
int main()
{
 
    int M[m][n] = { { 1, 1, 0, 3 },
                    { 2, 3, 2, 3 },
                    { 9, 3, 0, 2 },
                    { 2, 3, 2, 2 } };
    int k = 3;
 
    cout << calcProbability(M, k);
    return 0;
}


Java
// Java implementation of the above approach
 
class GFG
{
    final static int n = 4 ;
    final static int m = 4 ;
 
    // Function to calculate probability
    static float calcProbability(int M[][], int k)
    {
        // declare dp[][] and sum[]
        float dp[][] = new float[m][n] ;
        float sum[] = new float[n];
     
        // precalculate the first row
        for (int j = 0; j < n; j++)
        {
            dp[0][j] = M[0][j];
            sum[0] = sum[0] + dp[0][j];
        }
     
        // calculate the probability for
        // each element and update dp table
        for (int i = 1; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                dp[i][j] += dp[i - 1][j] / sum[i - 1] +
                            M[i][j];
                sum[i] += dp[i][j];
            }
        }
     
        // return result
        return dp[n - 1][k - 1] / sum[n - 1];
    }
     
    // Driver code
    public static void main(String []args)
    {
        int M[][] = { { 1, 1, 0, 3 },
                      { 2, 3, 2, 3 },
                      { 9, 3, 0, 2 },
                      { 2, 3, 2, 2 } };
        int k = 3;
        System.out.println(calcProbability(M, k));
         
    }
}
 
// This code is contributed by Ryuga


Python3
# Python3 implementation of the
# above approach
 
n = 4
m = 4
 
# Function to calculate probability
def calcProbability(M, k):
 
    # declare dp[][] and sum[]
    dp = [[0 for i in range(n)]
             for i in range(m)]
    Sum = [0 for i in range(n)]
 
    # precalculate the first row
    for j in range(n):
        dp[0][j] = M[0][j]
        Sum[0] += dp[0][j]
     
    # calculate the probability for
    # each element and update dp table
    for i in range(1, m):
        for j in range(n):
            dp[i][j] += (dp[i - 1][j] /
                         Sum[i - 1] + M[i][j])
            Sum[i] += dp[i][j]
         
    # return result
    return dp[n - 1][k - 1] / Sum[n - 1]
 
# Driver code
 
M = [[ 1, 1, 0, 3 ],
     [ 2, 3, 2, 3 ],
     [ 9, 3, 0, 2 ],
     [ 2, 3, 2, 2 ]]
k = 3
 
print(calcProbability(M, k))
 
# This code is contributed
# by mohit kumar 29


C#
// C# implementation of the above approach
using System;
 
class GFG
{
    static int n = 4 ;
    static int m = 4 ;
 
    // Function to calculate probability
    static float calcProbability(int[,] M, int k)
    {
        // declare dp[][] and sum[]
        float[,] dp = new float[m,n] ;
        float[] sum = new float[n];
     
        // precalculate the first row
        for (int j = 0; j < n; j++)
        {
            dp[0, j] = M[0, j];
            sum[0] = sum[0] + dp[0, j];
        }
     
        // calculate the probability for
        // each element and update dp table
        for (int i = 1; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                dp[i, j] += dp[i - 1,j] / sum[i - 1] +
                            M[i, j];
                sum[i] += dp[i, j];
            }
        }
     
        // return result
        return dp[n - 1,k - 1] / sum[n - 1];
    }
     
    // Driver code
    public static void Main()
    {
        int[,] M = { { 1, 1, 0, 3 },
                    { 2, 3, 2, 3 },
                    { 9, 3, 0, 2 },
                    { 2, 3, 2, 2 } };
        int k = 3;
        Console.Write(calcProbability(M, k));
    }
}
 
// This code is contributed by Ita_c.


PHP


Javascript


输出:
0.201212

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程