📌  相关文章
📜  排列的计数,使得给定范围内的 K 个数字的总和是偶数

📅  最后修改于: 2021-09-22 10:22:35             🧑  作者: Mango

给定一个范围[low, high] ,包括两者和一个整数K ,任务是从范围中选择K 个数字(一个数字可以多次选择),使得这些K 个数字的总和是偶数。打印所有此类排列的数量。

例子:

朴素的方法:这个想法是找到所有大小为 K 的子集,使得子集的总和是偶数,并计算每个所需子集的排列。
时间复杂度: O(K * (2 K ))
辅助空间: O(K)

有效的方法:这个想法是利用两个偶数和奇数的和总是偶数的事实。请按照以下步骤解决问题:

  1. 求给定范围[low, high]中偶数和奇数的总数。
  2. 初始化变量even_sum = 1odd_sum = 0分别存储求偶和和奇和的方式。
  3. 迭代循环K次并将前一个偶数和存储为prev_even = even_sum和前一个奇数和为prev_odd =odd_sum其中even_sum = (prev_even*even_count) + (prev_odd*odd_count)odd_sum = (prev_even*odd_count) + (prev_odd* even_count)
  4. 在最后打印 even_sum,因为有一个奇数和的计数,因为前一个odd_sum 将有助于下一个 even_sum。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to return the number
// of all permutations such that
// sum of K numbers in range is even
int countEvenSum(int low, int high, int k)
{
     
    // Find total count of even and
    // odd number in given range
    int even_count = high / 2 - (low - 1) / 2;
    int odd_count = (high + 1) / 2 - low / 2;
 
    long even_sum = 1;
    long odd_sum = 0;
 
    // Iterate loop k times and update
    // even_sum & odd_sum using
    // previous values
    for(int i = 0; i < k; i++)
    {
         
        // Update the prev_even and
        // odd_sum
        long prev_even = even_sum;
        long prev_odd = odd_sum;
 
        // Even sum
        even_sum = (prev_even * even_count) +
                    (prev_odd * odd_count);
 
        // Odd sum
        odd_sum = (prev_even * odd_count) +
                   (prev_odd * even_count);
    }
 
    // Return even_sum
    cout << (even_sum);
}
 
// Driver Code
int main()
{
     
    // Given ranges
    int low = 4;
    int high = 5;
 
    // Length of permutation
    int K = 3;
     
    // Function call
    countEvenSum(low, high, K);
}
 
// This code is contributed by Stream_Cipher


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to return the number
    // of all permutations such that
    // sum of K numbers in range is even
    public static void
    countEvenSum(int low, int high,
                 int k)
    {
        // Find total count of even and
        // odd number in given range
        int even_count = high / 2 - (low - 1) / 2;
        int odd_count = (high + 1) / 2 - low / 2;
 
        long even_sum = 1;
        long odd_sum = 0;
 
        // Iterate loop k times and update
        // even_sum & odd_sum using
        // previous values
        for (int i = 0; i < k; i++) {
 
            // Update the prev_even and
            // odd_sum
            long prev_even = even_sum;
            long prev_odd = odd_sum;
 
            // Even sum
            even_sum = (prev_even * even_count)
                       + (prev_odd * odd_count);
 
            // Odd sum
            odd_sum = (prev_even * odd_count)
                      + (prev_odd * even_count);
        }
 
        // Return even_sum
        System.out.println(even_sum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given ranges
        int low = 4;
        int high = 5;
 
        // Length of permutation
        int K = 3;
 
        // Function call
        countEvenSum(low, high, K);
    }
}


Python3
# Python3 program for the above approach
 
# Function to return the number
# of all permutations such that
# sum of K numbers in range is even
def countEvenSum(low, high, k):
 
    # Find total count of even and
    # odd number in given range
    even_count = high / 2 - (low - 1) / 2
    odd_count = (high + 1) / 2 - low / 2
 
    even_sum = 1
    odd_sum = 0
 
    # Iterate loop k times and update
    # even_sum & odd_sum using
    # previous values
    for i in range(0, k):
         
        # Update the prev_even and
        # odd_sum
        prev_even = even_sum
        prev_odd = odd_sum
 
        # Even sum
        even_sum = ((prev_even * even_count) +
                     (prev_odd * odd_count))
 
        # Odd sum
        odd_sum = ((prev_even * odd_count) +
                    (prev_odd * even_count))
 
    # Return even_sum
    print(int(even_sum))
 
# Driver Code
 
# Given ranges
low = 4;
high = 5;
 
# Length of permutation
K = 3;
 
# Function call
countEvenSum(low, high, K);
 
# This code is contributed by Stream_Cipher


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to return the number
// of all permutations such that
// sum of K numbers in range is even
public static void countEvenSum(int low,
                                int high, int k)
{
     
    // Find total count of even and
    // odd number in given range
    int even_count = high / 2 - (low - 1) / 2;
    int odd_count = (high + 1) / 2 - low / 2;
 
    long even_sum = 1;
    long odd_sum = 0;
 
    // Iterate loop k times and update
    // even_sum & odd_sum using
    // previous values
    for(int i = 0; i < k; i++)
    {
         
        // Update the prev_even and
        // odd_sum
        long prev_even = even_sum;
        long prev_odd = odd_sum;
 
        // Even sum
        even_sum = (prev_even * even_count) +
                    (prev_odd * odd_count);
 
        // Odd sum
        odd_sum = (prev_even * odd_count) +
                   (prev_odd * even_count);
    }
 
    // Return even_sum
    Console.WriteLine(even_sum);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given ranges
    int low = 4;
    int high = 5;
 
    // Length of permutation
    int K = 3;
 
    // Function call
    countEvenSum(low, high, K);
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
4

时间复杂度: O(K)
辅助空间: O(1)