📜  计算获得奇数的方法数

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

给定N对数字。任务是计算从每一对中选择一个数字的方法,使得这些数字的总和为奇数。
例子:

方法:

  • 我们将在这里使用动态规划,其中dp[i][0]将存储获得偶数和到第 i 对的可能方法的数量,而dp[i][1]将存储获得奇数和到第 i 的可能方法的数量’这对。
  • cnt[i][0]将存储第 i 对中偶数的计数,而cnt[i][1]将存储第 i 对中奇数的计数。
  • 众所周知,二偶之和或二奇之和永远是偶数,一偶一奇之和永远是奇数。
  • 我们应用它来将计数存储在 DP 数组中。
  • 获得第 i 对偶数总和的方法是dp[i – 1][0] * cnt[i][0] + dp[i – 1][1] * cnt[i][1]。
  • 获得第 i 对奇数的方法是dp[i – 1][1] * cnt[i][0] + dp[i – 1][0] * cnt[i][1]。

下面是上述方法的实现:

C++
// C++ implementation
#include 
using namespace std;
 
// Count the ways to sum up with odd
// by choosing one element form each
// pair
int CountOfOddSum(int a[][2], int n)
{
    int dp[n][2], cnt[n][2];
 
    // Initialize two array with 0
    memset(dp, 0, sizeof(dp));
    memset(cnt, 0, sizeof(cnt));
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 2; j++) {
 
            // if element is even
            if (a[i][j] % 2 == 0) {
 
                // store count of even
                // number in i'th pair
                cnt[i][0]++;
            }
 
            // if the element is odd
            else {
 
                // store count of odd
                // number in i'th pair
                cnt[i][1]++;
            }
        }
    }
 
    // Initial state of dp array
    dp[0][0] = cnt[0][0], dp[0][1] = cnt[0][1];
 
    for (int i = 1; i < n; i++) {
 
        // dp[i][0] = total number of ways
        // to get even sum upto i'th pair
        dp[i][0] = (dp[i - 1][0] * cnt[i][0]
                    + dp[i - 1][1] * cnt[i][1]);
 
        // dp[i][1] = total number of ways
        // to odd even sum upto i'th pair
        dp[i][1] = (dp[i - 1][0] * cnt[i][1]
                    + dp[i - 1][1] * cnt[i][0]);
    }
 
    // dp[n - 1][1] = total number of ways
    // to get odd sum upto n'th pair
    return dp[n - 1][1];
}
 
// Driver code
int main()
{
 
    int a[][2] = { { 1, 2 }, { 3, 6 } };
    int n = sizeof(a) / sizeof(a[0]);
 
    int ans = CountOfOddSum(a, n);
 
    cout << ans << "\n";
 
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
    // Count the ways to sum up with odd
    // by choosing one element form each
    // pair
    static int CountOfOddSum(int a[][], int n)
    {
        int [][]dp = new int[n][2];
        int [][]cnt = new int[n][2];
     
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 2; j++)
            {
     
                // if element is even
                if (a[i][j] % 2 == 0)
                {
     
                    // store count of even
                    // number in i'th pair
                    cnt[i][0]++;
                }
     
                // if the element is odd
                else
                {
     
                    // store count of odd
                    // number in i'th pair
                    cnt[i][1]++;
                }
            }
        }
     
        // Initial state of dp array
        dp[0][0] = cnt[0][0];
        dp[0][1] = cnt[0][1];
     
        for (int i = 1; i < n; i++)
        {
     
            // dp[i][0] = total number of ways
            // to get even sum upto i'th pair
            dp[i][0] = (dp[i - 1][0] * cnt[i][0] +
                        dp[i - 1][1] * cnt[i][1]);
     
            // dp[i][1] = total number of ways
            // to odd even sum upto i'th pair
            dp[i][1] = (dp[i - 1][0] * cnt[i][1] +
                        dp[i - 1][1] * cnt[i][0]);
        }
     
        // dp[n - 1][1] = total number of ways
        // to get odd sum upto n'th pair
        return dp[n - 1][1];
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a[][] = {{ 1, 2 }, { 3, 6 }};
        int n = a.length;
     
        int ans = CountOfOddSum(a, n);
     
        System.out.println(ans);
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the above approach
 
# Count the ways to sum up with odd
# by choosing one element form each
# pair
def CountOfOddSum(a, n):
 
    dp = [[0 for i in range(2)]
             for i in range(n)]
    cnt = [[0 for i in range(2)]
              for i in range(n)]
 
    # Initialize two array with 0
    for i in range(n):
        for j in range(2):
             
            # if element is even
            if (a[i][j] % 2 == 0):
 
                #store count of even
                #number in i'th pair
                cnt[i][0] += 1
 
            # if the element is odd
            else :
 
                # store count of odd
                # number in i'th pair
                cnt[i][1] += 1
 
    # Initial state of dp array
    dp[0][0] = cnt[0][0]
    dp[0][1] = cnt[0][1]
 
    for i in range(1, n):
 
        # dp[i][0] = total number of ways
        # to get even sum upto i'th pair
        dp[i][0] = (dp[i - 1][0] * cnt[i][0] +
                    dp[i - 1][1] * cnt[i][1])
 
        # dp[i][1] = total number of ways
        # to odd even sum upto i'th pair
        dp[i][1] = (dp[i - 1][0] * cnt[i][1] +
                    dp[i - 1][1] * cnt[i][0])
 
    # dp[n - 1][1] = total number of ways
    # to get odd sum upto n'th pair
    return dp[n - 1][1]
 
# Driver code
a = [[1, 2] , [3, 6] ]
n = len(a)
 
ans = CountOfOddSum(a, n)
 
print(ans)
     
# This code is contributed by Mohit Kumar


C#
// C# implementation of above approach
using System;
 
class GFG
{
    // Count the ways to sum up with odd
    // by choosing one element form each
    // pair
    static int CountOfOddSum(int [ , ] a, int n)
    {
        int [ , ]dp = new int[n, 2];
        int [ , ]cnt = new int[n, 2];
     
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 2; j++)
            {
     
                // if element is even
                if (a[i, j] % 2 == 0)
                {
     
                    // store count of even
                    // number in i'th pair
                    cnt[i, 0]++;
                }
     
                // if the element is odd
                else
                {
     
                    // store count of odd
                    // number in i'th pair
                    cnt[i, 1]++;
                }
            }
        }
     
        // Initial state of dp array
        dp[0, 0] = cnt[0, 0];
        dp[0, 1] = cnt[0, 1];
     
        for (int i = 1; i < n; i++)
        {
     
            // dp[i, 0] = total number of ways
            // to get even sum upto i'th pair
            dp[i, 0] = (dp[i - 1, 0] * cnt[i, 0] +
                        dp[i - 1, 1] * cnt[i, 1]);
     
            // dp[i, 1] = total number of ways
            // to odd even sum upto i'th pair
            dp[i, 1] = (dp[i - 1, 0] * cnt[i, 1] +
                        dp[i - 1, 1] * cnt[i, 0]);
        }
     
        // dp[n - 1, 1] = total number of ways
        // to get odd sum upto n'th pair
        return dp[n - 1, 1];
    }
     
    // Driver code
    public static void Main ()
    {
        int [ , ] a = { { 1, 2 }, { 3, 6 } };
        int n = a.GetLength(1);
     
        int ans = CountOfOddSum(a, n);
     
        Console.WriteLine(ans);
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
2

时间复杂度: O(N)

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