📌  相关文章
📜  求b的Kth最小值,使得a + b = a | b

📅  最后修改于: 2021-04-24 14:21:20             🧑  作者: Mango

给定数字ak ,任务是找到b的k个最小值,使得a + b = a | b ,其中“ |”表示按位OR运算符。 a和k的最大值可以是10^9

例子:

Input: a = 10, k = 3
Output: 5
Numbers satisfying the condition 5 + b = 5 | b
are 1, 4, 5, etc.
Since 3rd smallest value for b is required,
hence b = 5.

Input: a = 1, k = 1
Output: 2
Numbers satisfying the condition 1 + b = 1 | b
are 2, 4, 6, etc.
Since 1st smallest value for b is required,
hence b = 2.

方法:

  • 当且仅当b在所有位置a均为1的所有位置中b均为0时, b是给定方程的解(以二进制表示)
  • 因此,我们需要确定a的位置为0的b的位。令,如果a = 10100001,则b的最后八位必须为b = 0 * 0 **** 0 ,其中’*’表示0或1.用0或1替换所有“ *”将为我们提供解决方案。
  • 通过用数字k的二进制表示形式的数字替换y中的所有’*’,将接收第k个最小的数字
  • 因为a和k的最大值是10^9 ,因此检查二进制表示形式中的多达32个位置就足以为给定的方程式获得正确的答案。

下面是上述方法的实现:

CPP
// C++ program to find k'th smallest value for b
// such that a + b = a | b
  
#include 
using namespace std;
  
#define ll long long
  
// Function to find
// the kth smallest value for b
ll kthSmallest(ll a, ll k)
{
  
    // res will store final answer
    ll res = 0;
    ll j = 0;
    for (ll i = 0; i < 32; i++) {
  
        // skip when j'th position
        // has 1 in binary representation
        // as in res, j'th position will be 0.
  
        while (j < 32 && (a & (1 << j)))
            // j'th bit is set
            j++;
  
        // if i'th bit of k is 1
        // and i'th bit of j is 0
        // then set i'th bit in res.
  
        if (k & (1 << i)) // i'th bit is set
            res |= (1LL << j);
  
        // proceed to next bit
        j++;
    }
    return res;
}
  
// Driver Code
int main()
{
  
    ll a = 10, k = 3;
  
    cout << kthSmallest(a, k) << "\n";
  
    return 0;
}


Java
// Java program to find k'th smallest value for b
// such that a + b = a | b
class GFG
{
  
    // Function to find
    // the kth smallest value for b
    static int kthSmallest(int a, int k) 
    {
  
        // res wiint store final answer
        int res = 0;
        int j = 0;
        for (int i = 0; i < 32; i++)
        {
  
            // skip when j'th position
            // has 1 in binary representation
            // as in res, j'th position wiint be 0.
            while (j < 32 && (a & (1 << j)) > 0)
                  
                // j'th bit is set
                j++;
  
            // if i'th bit of k is 1
            // and i'th bit of j is 0
            // then set i'th bit in res.
            if ((k & (1 << i)) > 0) // i'th bit is set
                res |= (1 << j);
  
            // proceed to next bit
            j++;
        }
        return res;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int a = 10, k = 3;
  
        System.out.print(kthSmallest(a, k) + "\n");
    }
}
  
// This code is contributed by Rajput-Ji


Python
# Python3 program to find k'th smallest value for b
# such that a + b = a | b
  
# Function to find
# the kth smallest value for b
def kthSmallest(a, k):
  
    # res wistore final answer
    res = 0
    j = 0
    for i in range(32):
  
        # skip when j'th position
        # has 1 in binary representation
        # as in res, j'th position wibe 0.
        while (j < 32 and (a & (1 << j))):
              
            # j'th bit is set
            j += 1
  
        # if i'th bit of k is 1
        # and i'th bit of j is 0
        # then set i'th bit in res.
  
        if (k & (1 << i)): # i'th bit is set
            res |= (1 << j)
  
        # proceed to next bit
        j += 1
    return res
  
# Driver Code
  
a = 10
k = 3
  
print(kthSmallest(a, k))
  
# This code is contributed by mohit kumar 29


C#
// C# program to find k'th smallest value for b
// such that a + b = a | b
using System;
  
class GFG
{
  
    // Function to find
    // the kth smallest value for b
    static int kthSmallest(int a, int k) 
    {
  
        // res wiint store final answer
        int res = 0;
        int j = 0;
        for (int i = 0; i < 32; i++)
        {
  
            // skip when j'th position
            // has 1 in binary representation
            // as in res, j'th position wiint be 0.
            while (j < 32 && (a & (1 << j)) > 0)
                  
                // j'th bit is set
                j++;
  
            // if i'th bit of k is 1
            // and i'th bit of j is 0
            // then set i'th bit in res.
            if ((k & (1 << i)) > 0) // i'th bit is set
                res |= (1 << j);
  
            // proceed to next bit
            j++;
        }
        return res;
    }
  
    // Driver Code
    public static void Main() 
    {
        int a = 10, k = 3;
  
        Console.WriteLine(kthSmallest(a, k));
    }
}
  
// This code is contributed by AnkitRai01


输出:
5

时间复杂度: O(N)