📌  相关文章
📜  查找按位或等于K的N个不同的数字

📅  最后修改于: 2021-05-25 09:40:39             🧑  作者: Mango

给定两个整数NK ,任务是找到按位或等于K的N个不同整数。如果不存在任何可能的答案,则打印-1

例子:

方法:

  1. 我们知道,如果逐位或数字序列为K然后是0 K中的所有位的位置也必须在所有的数字为零。
  2. 因此,我们只需要更改那些位置(其中K1 )。假设计数为Bit_K
  3. 现在,我们可以用Bit_K位形成pow(2,Bit_K)不同的数字。所以,如果我们设置一个号码被K本身,然后休息N – 1倍的数字可以通过设置0所有在每一个数字,其在K和其他位位置Bit_K的任意排列位比数K其他都是0位之上。
  4. 如果pow(2,Bit_K)则我们找不到任何可能的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
#define MAX 32
  
ll pow2[MAX];
bool visited[MAX];
vector ans;
  
// Function to pre-calculate
// all the powers of 2 upto MAX
void power_2()
{
    ll ans = 1;
    for (int i = 0; i < MAX; i++) {
        pow2[i] = ans;
        ans *= 2;
    }
}
  
// Function to return the
// count of set bits in x
int countSetBits(ll x)
{
  
    // To store the count
    // of set bits
    int setBits = 0;
    while (x != 0) {
        x = x & (x - 1);
        setBits++;
    }
  
    return setBits;
}
  
// Function to add num to the answer
// by setting all bit positions as 0
// which are also 0 in K
void add(ll num)
{
    int point = 0;
    ll value = 0;
  
    for (ll i = 0; i < MAX; i++) {
  
        // Bit i is 0 in K
        if (visited[i])
            continue;
        else {
            if (num & 1) {
                value += (1 << i);
            }
            num /= 2;
        }
    }
  
    ans.push_back(value);
}
  
// Function to find and print N distinct
// numbers whose bitwise OR is K
void solve(ll n, ll k)
{
  
    // Choosing K itself as one number
    ans.push_back(k);
  
    // Find the count of set bits in K
    int countk = countSetBits(k);
  
    // Impossible to get N
    // distinct integers
    if (pow2[countk] < n) {
        cout << -1;
        return;
    }
  
    int count = 0;
    for (ll i = 0; i < pow2[countk] - 1; i++) {
  
        // Add i to the answer after
        // setting all the bits as 0
        // which are 0 in K
        add(i);
        count++;
  
        // If N distinct numbers are generated
        if (count == n)
            break;
    }
  
    // Print the generated numbers
    for (int i = 0; i < n; i++) {
        cout << ans[i] << " ";
    }
}
  
// Driver code
int main()
{
    ll n = 3, k = 5;
  
    // Pre-calculate all
    // the powers of 2
    power_2();
  
    solve(n, k);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
      
static final int MAX = 32;
  
static long []pow2 = new long[MAX];
static boolean []visited = new boolean[MAX];
static Vector ans = new Vector<>();
  
// Function to pre-calculate
// all the powers of 2 upto MAX
static void power_2()
{
    long ans = 1;
    for (int i = 0; i < MAX; i++) 
    {
        pow2[i] = ans;
        ans *= 2;
    }
}
  
// Function to return the
// count of set bits in x
static int countSetBits(long x)
{
  
    // To store the count
    // of set bits
    int setBits = 0;
    while (x != 0) 
    {
        x = x & (x - 1);
        setBits++;
    }
  
    return setBits;
}
  
// Function to add num to the answer
// by setting all bit positions as 0
// which are also 0 in K
static void add(long num)
{
    int point = 0;
    long value = 0;
  
    for (int i = 0; i < MAX; i++) 
    {
  
        // Bit i is 0 in K
        if (visited[i])
            continue;
        else 
        {
            if (num %2== 1)
            {
                value += (1 << i);
            }
            num /= 2;
        }
    }
  
    ans.add(value);
}
  
// Function to find and print N distinct
// numbers whose bitwise OR is K
static void solve(long n, long k)
{
  
    // Choosing K itself as one number
    ans.add(k);
  
    // Find the count of set bits in K
    int countk = countSetBits(k);
  
    // Impossible to get N
    // distinct integers
    if (pow2[countk] < n)
    {
        System.out.print(-1);
        return;
    }
  
    int count = 0;
    for (long i = 0; i < pow2[countk] - 1; i++)
    {
  
        // Add i to the answer after
        // setting all the bits as 0
        // which are 0 in K
        add(i);
        count++;
  
        // If N distinct numbers are generated
        if (count == n)
            break;
    }
  
    // Print the generated numbers
    for (int i = 0; i < n; i++) 
    {
        System.out.print(ans.get(i)+" ");
    }
}
  
// Driver code
public static void main(String[] args)
{
    long n = 3, k = 5;
  
    // Pre-calculate all
    // the powers of 2
    power_2();
  
    solve(n, k);
}
}
  
// This code has been contributed by 29AjayKumar


Python3
# Pyhton 3 implementation of the approach
MAX = 32
  
pow2 = [0 for i in range(MAX)]
visited = [False for i in range(MAX)]
ans = []
  
# Function to pre-calculate
# all the powers of 2 upto MAX
def power_2():
    an = 1
    for i in range(MAX):
        pow2[i] = an
        an *= 2
  
# Function to return the
# count of set bits in x
def countSetBits(x):
    # To store the count
    # of set bits
    setBits = 0
    while (x != 0):
        x = x & (x - 1)
        setBits += 1
      
    return setBits
  
# Function to add num to the answer
# by setting all bit positions as 0
# which are also 0 in K
def add(num):
    point = 0
    value = 0
  
    for i in range(MAX):
        # Bit i is 0 in K
        if (visited[i]):
            continue
        else:
            if (num & 1):
                value += (1 << i)
            num = num//2
  
    ans.append(value)
  
# Function to find and print N distinct
# numbers whose bitwise OR is K
def solve(n, k):
    # Choosing K itself as one number
    ans.append(k)
  
    # Find the count of set bits in K
    countk = countSetBits(k)
  
    # Impossible to get N
    # distinct integers
    if (pow2[countk] < n):
        print(-1)
        return
  
    count = 0
    for i in range(pow2[countk] - 1):
        # Add i to the answer after
        # setting all the bits as 0
        # which are 0 in K
        add(i)
        count += 1
  
        # If N distinct numbers are generated
        if (count == n):
            break
  
    # Print the generated numbers
    for i in range(n):
        print(ans[i],end = " ")
  
# Driver code
if __name__ == '__main__':
    n = 3
    k = 5
  
    # Pre-calculate all
    # the powers of 2
    power_2()
  
    solve(n, k)
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
          
    static int MAX = 32;
      
    static long [] pow2 = new long[MAX];
    static bool [] visited = new bool[MAX];
    static List ans = new List();
      
    // Function to pre-calculate
    // all the powers of 2 upto MAX
    static void power_2()
    {
        long ans = 1;
        for (int i = 0; i < MAX; i++) 
        {
            pow2[i] = ans;
            ans *= 2;
        }
    }
      
    // Function to return the
    // count of set bits in x
    static int countSetBits(long x)
    {
      
        // To store the count
        // of set bits
        int setBits = 0;
        while (x != 0) 
        {
            x = x & (x - 1);
            setBits++;
        }
      
        return setBits;
    }
      
    // Function to add num to the answer
    // by setting all bit positions as 0
    // which are also 0 in K
    static void add(long num)
    {
          
        long value = 0;
      
        for (int i = 0; i < MAX; i++) 
        {
      
            // Bit i is 0 in K
            if (visited[i])
                continue;
            else
            {
                if (num %2== 1)
                {
                    value += (1 << i);
                }
                num /= 2;
            }
        }
      
        ans.Add(value);
    }
      
    // Function to find and print N distinct
    // numbers whose bitwise OR is K
    static void solve(long n, long k)
    {
      
        // Choosing K itself as one number
        ans.Add(k);
      
        // Find the count of set bits in K
        int countk = countSetBits(k);
      
        // Impossible to get N
        // distinct integers
        if (pow2[countk] < n)
        {
            Console.WriteLine(-1);
            return;
        }
      
        int count = 0;
        for (long i = 0; i < pow2[countk] - 1; i++)
        {
      
            // Add i to the answer after
            // setting all the bits as 0
            // which are 0 in K
            add(i);
            count++;
      
            // If N distinct numbers are generated
            if (count == n)
                break;
        }
      
        // Print the generated numbers
        for (int i = 0; i < n; i++) 
        {
            Console.Write(ans[i]+ " ");
        }
    }
      
    // Driver code
    public static void Main()
    {
        long n = 3, k = 5;
      
        // Pre-calculate all
        // the powers of 2
        power_2();
      
        solve(n, k);
    }
}
  
// This code is contributed by ihritik


输出:
5 0 1