📜  计算N次幂的奇偶二项式系数

📅  最后修改于: 2021-05-06 19:28:22             🧑  作者: Mango

给定一个整数N ,任务是计算直到N次方的偶数和奇数二项式系数的数量。

例子:

解决方法:解决此问题的想法是使用位操作。在给定的整数N中找到设置的位奇二项式系数计数等于N中置位的2 ^计数。同样,偶数二项式系数的计数等于(N + 1-2 ^ N中的置位计数)

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#include 
using namespace std;
 
// Function to count set bits in
// binary representation of number N
int countSetBits(int N)
{
    int count = 0;
 
    // Count set bits in N
    while (N) {
 
        N = N & (N - 1);
        count++;
    }
 
    // Return the final count
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
 
    int bits = countSetBits(N);
 
    // Print odd Binomial coefficents
    cout << "Odd "
         << ": " << pow(2, bits) << "\n";
 
    // Print even Binomial coefficents
    cout << "Even "
         << ": " << N + 1 - pow(2, bits)
         << "\n";
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
      
// Function to count set bits in
// binary representation of number N
static int countSetBits(int N)
{
    int count = 0;
  
    // Count set bits in N
    while (N != 0)
    {
         
        N = N & (N - 1);
        count++;
    }
  
    // Return the final count
    return count;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 4;
  
    int bits = countSetBits(N);
  
    // Print odd Binomial coefficents
    System.out.println("Odd " + ": " +
               (int)(Math.pow(2, bits)));
  
    // Print even Binomial coefficents
    System.out.println("Even " + ": " +
               (N + 1 - (int)(Math.pow(2, bits))));
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to count set bits in
# binary representation of number N
def countSetBits(N: int) -> int:
 
    count = 0
 
    # Count set bits in N
    while (N):
        N = N & (N - 1)
        count += 1
 
    # Return the final count
    return count
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
 
    bits = countSetBits(N)
 
    # Print odd Binomial coefficents
    print("Odd : {}".format(pow(2, bits)))
 
    # Print even Binomial coefficents
    print("Even : {}".format(N + 1 - pow(2, bits)))
 
# This code is contributed by sanjeev2552


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to count set bits in
// binary representation of number N
static int countSetBits(int N)
{
    int count = 0;
   
    // Count set bits in N
    while (N != 0)
    {
        N = N & (N - 1);
        count++;
    }
   
    // Return the final count
    return count;
}
  
// Driver Code
public static void Main()
{
    int N = 4;
    int bits = countSetBits(N);
   
    // Print odd Binomial coefficents
    Console.WriteLine("Odd " + ": " +
                     (int)(Math.Pow(2, bits)));
   
    // Print even Binomial coefficents
    Console.WriteLine("Even " + ": " +
                     (N + 1 - (int)(Math.Pow(2, bits))));
}
}
 
// This code is contributed by sanjoy_62


输出:
Odd : 2
Even : 3

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