📌  相关文章
📜  排列计数,以使给定范围内的K个数之和为偶数

📅  最后修改于: 2021-05-14 00:54:21             🧑  作者: 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和previouse奇数和作为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


输出:
4

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