具有 K 个集合位的 N 位奇数和偶数整数的计数
给定两个正整数N和K ,任务是计算由N位组成的偶数和奇数整数的数量,其中设置了K位。
例子:
Input: N = 5, K = 2
Output: 3 1
Explanation:
The 5 bit even integers having 2 set bits are: 10010(= 18), 10100(= 20), 11000(= 24). So, the count is 3.
The 5 bit odd integer having 2 set bits is 10001(=17). So, the count is 1.
Input: N = 5, K = 5
Output: 0 1
方法:给定的问题可以通过以下观察来解决:
- 对于任何N位整数,第N位(最高有效位)必须是设置位。
- 对于任何N位整数是奇数,第1位(最低有效位)必须是设置位。
- 对于任何N位整数是偶数,第1位(最低有效位)不能是设置位。
因此,通过将第N位固定为1 ,将第1位固定为0 ,并计算剩余位的排列方式数,可以得到偶数的个数。类似地,奇数个数可以通过将第1位和第N位固定为1来获得。
排列具有Y个设置位的X位的不同方式的总数由下式给出:
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
long long factorial(int n)
{
long long ans = 1;
while (n >= 1) {
ans *= n;
n--;
}
return ans;
}
// Function to find the count of odd and
// even integers having N bits and K
// set bits
void Binary_Num(int n, int k)
{
long long num_even, num_odd;
// Find the count of even integers
if (n - k - 1 >= 0 && k - 1 >= 0) {
num_even
= factorial(n - 2)
/ (factorial(k - 1) * factorial(n - k - 1));
}
else {
num_even = 0;
}
// Find the count of odd integers
if (k - 2 >= 0) {
num_odd = factorial(n - 2)
/ (factorial(k - 2) * factorial(n - k));
}
else {
num_odd = 0;
}
// Print the total count
cout << num_even << " " << num_odd << endl;
}
// Driver code
int main()
{
int N = 9, K = 6;
Binary_Num(N, K);
return 0;
}
// This code is contributed by maddler.
Java
// Java program for the above approach
import java.io.*;
class GFG {
static long factorial(int n)
{
long ans = 1;
while (n >= 1) {
ans *= n;
n--;
}
return ans;
}
// Function to find the count of odd and
// even integers having N bits and K
// set bits
static void Binary_Num(int n, int k)
{
long num_even, num_odd;
// Find the count of even integers
if (n - k - 1 >= 0 && k - 1 >= 0) {
num_even
= factorial(n - 2)
/ (factorial(k - 1) * factorial(n - k - 1));
}
else {
num_even = 0;
}
// Find the count of odd integers
if (k - 2 >= 0) {
num_odd = factorial(n - 2)
/ (factorial(k - 2) * factorial(n - k));
}
else {
num_odd = 0;
}
// Print the total count
System.out.println( num_even +" " +num_odd );
}
// Driver Code
public static void main(String[] args)
{
int N = 9, K = 6;
Binary_Num(N, K);
}
}
// This code is contributed by dwivediyash
Python3
# Python program for the above approach
import math
# Function to find the count of odd and
# even integers having N bits and K
# set bits
def Binary_Num(N, K):
# Find the count of even integers
if N-K-1 >= 0 and K-1 >= 0:
num_even = math.factorial(
N-2)/(math.factorial(K-1)
* math.factorial(N-K-1))
else:
num_even = 0
# Find the count of odd integers
if K-2 >= 0:
num_odd = math.factorial(N-2) \
/ (math.factorial(K-2)
* math.factorial(N-K))
else:
num_odd = 0
# Print the total count
print(int(num_even), int(num_odd))
# Driver Code
if __name__ == "__main__":
N = 9
K = 6
Binary_Num(N, K)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static int factorial(int n)
{
int ans = 1;
while (n >= 1) {
ans *= n;
n--;
}
return ans;
}
// Function to find the count of odd and
// even integers having N bits and K
// set bits
static void Binary_Num(int n, int k)
{
int num_even, num_odd;
// Find the count of even integers
if (n - k - 1 >= 0 && k - 1 >= 0) {
num_even
= factorial(n - 2)
/ (factorial(k - 1) * factorial(n - k - 1));
}
else {
num_even = 0;
}
// Find the count of odd integers
if (k - 2 >= 0) {
num_odd = factorial(n - 2)
/ (factorial(k - 2) * factorial(n - k));
}
else {
num_odd = 0;
}
// Print the total count
Console.Write(num_even + " " + num_odd);
}
// Driver code
public static void Main()
{
int N = 9, K = 6;
Binary_Num(N, K);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
21 35
时间复杂度: O(N)
辅助空间: O(1)
注意:对于多个查询,预先计算数组中的阶乘值,这将导致每个查询的 O(1) 时间和 O(N) 的预先计算。