📌  相关文章
📜  按位与为零的长度为 N 的不同排列的计数

📅  最后修改于: 2022-05-13 01:56:05.969000             🧑  作者: Mango

按位与为零的长度为 N 的不同排列的计数

给定一个整数N .,任务是找到长度为N不同排列的数量,使得每个排列的按位与值为零

例子:

方法:可以使用观察来解决任务。可以观察到,如果一个数字是2 的幂,假设是“ x ”, x & (x-1)的按位与始终为零。所有长度大于1 的排列,按位 AND为零,对于N = 1 ,不同排列的计数为0 。因此,所需的计数等于可能排列的数量,即N!

以下是上述方法的实施 -

C++
// C++ program for the
// above approach
#include 
using namespace std;
 
// Function to calculate factorial
// of a number
long long int fact(long long N)
{
    long long int ans = 1;
    for (int i = 2; i <= N; i++)
        ans *= i;
 
    return ans;
}
 
// Function to find distinct no of
// permutations having bitwise and (&)
// equals to 0
long long permutation_count(long long n)
{
    // corner case
    if (n == 1)
        return 0;
 
    return fact(n);
}
 
// Driver code
int main()
{
 
    long long N = 3;
    cout << permutation_count(N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to calculate factorial
// of a number
static long fact( long N)
{
    long  ans = 1;
    for (int i = 2; i <= N; i++)
        ans *= i;
 
    return ans;
}
 
// Function to find distinct no of
// permutations having bitwise and (&)
// equals to 0
static long  permutation_count(long n)
{
   
    // corner case
    if (n == 1)
        return 0;
 
    return fact(n);
}
 
    public static void main (String[] args) {
       long N = 3;
     System.out.println(permutation_count(N));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the
# above approach
 
# Function to calculate factorial
# of a number
def fact(N):
 
    ans = 1
    for i in range(2,  N + 1):
        ans *= i
 
    return ans
 
# Function to find distinct no of
# permutations having bitwise and (&)
# equals to 0
def permutation_count(n):
 
    # corner case
    if (n == 1):
        return 0
 
    return fact(n)
 
# Driver code
if __name__ == "__main__":
 
    N = 3
    print(permutation_count(N))
 
    # This code is contributed by ukasp.


C#
// C# program for the
// above approach
using System;
 
class GFG
{
// Function to calculate factorial
// of a number
static long fact(long N)
{
    long ans = 1;
    for (int i = 2; i <= N; i++)
        ans *= i;
 
    return ans;
}
 
// Function to find distinct no of
// permutations having bitwise and (&)
// equals to 0
static long permutation_count(long n)
{
    // corner case
    if (n == 1)
        return 0;
 
    return fact(n);
}
 
// Driver code
public static void Main()
{
 
    long N = 3;
    Console.Write(permutation_count(N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
6

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