📌  相关文章
📜  使得a = b +(a ^ b)的b的数量

📅  最后修改于: 2021-04-22 02:57:09             🧑  作者: Mango

给定一个整数a 。查找解决方案的数量b满足等式:

例子:

Input: a = 4 
Output: 2
The only values of b are 0 and 4 itself.

Input: a = 3 
Output: 4 

一个幼稚的解决方案是从0迭代到a并计算满足给定方程的值的数量。我们只需要走到a因为任何大于a将给出XOR值> a ,因此不能满足方程式。

下面是上述方法的实现:

C++
// C++ program to find the number of values
// of b such that a = b + (a^b)
#include 
using namespace std;
  
// function to return the number of solutions
int countSolutions(int a)
{
    int count = 0;
  
    // check for every possible value
    for (int i = 0; i <= a; i++) {
        if (a == (i + (a ^ i)))
            count++;
    }
  
    return count;
}
  
// Driver Code
int main()
{
    int a = 3;
    cout << countSolutions(a);
}


Java
// Java program to find the number of values
// of b such that a = b + (a^b)
  
import java.io.*;
  
class GFG {
  
  
// function to return the number of solutions
 static int countSolutions(int a)
{
    int count = 0;
  
    // check for every possible value
    for (int i = 0; i <= a; i++) {
        if (a == (i + (a ^ i)))
            count++;
    }
  
    return count;
}
  
// Driver Code
  
  
    public static void main (String[] args) {
        int a = 3;
    System.out.println( countSolutions(a));
    }
}
// This code is contributed by inder_verma


Python 3
# Python 3 program to find 
# the number of values of b
# such that a = b + (a^b)
  
# function to return the 
# number of solutions
def countSolutions(a):
  
    count = 0
  
    # check for every possible value
    for i in range(a + 1):
        if (a == (i + (a ^ i))):
            count += 1
  
    return count
  
# Driver Code
if __name__ == "__main__":
    a = 3
    print(countSolutions(a))
  
# This code is contributed
# by ChitraNayal


C#
// C# program to find the number of 
// values of b such that a = b + (a^b)
using System;
  
class GFG 
{
  
// function to return the
// number of solutions
static int countSolutions(int a)
{
    int count = 0;
  
    // check for every possible value
    for (int i = 0; i <= a; i++) 
    {
        if (a == (i + (a ^ i)))
            count++;
    }
  
    return count;
}
  
// Driver Code
public static void Main () 
{
    int a = 3;
    Console.WriteLine(countSolutions(a));
}
}
  
// This code is contributed by inder_verma


PHP


C++
// C++ program to find the number of values
// of b such that a = b + (a^b)
#include 
using namespace std;
  
// function to return the number of solutions
int countSolutions(int a)
{
    int count = __builtin_popcount(a);
  
    count = pow(2, count);
    return count;
}
  
// Driver Code
int main()
{
    int a = 3;
    cout << countSolutions(a);
}


Java
// Java program to find the number of values
// of b such that a = b + (a^b)
import java.io.*;
class GFG
{
   
    // function to return the number of solutions
    static int countSolutions(int a)
    {
        int count = Integer.bitCount(a);
       
        count =(int) Math.pow(2, count);
        return count;
    }
       
    // Driver Code
        public static void main (String[] args) 
    {
        int a = 3;
        System.out.println(countSolutions(a));
    }
}
// This code is contributed by Raj


Python3
# Python3 program to find the number 
# of values of b such that a = b + (a^b) 
  
# function to return the number 
# of solutions 
def countSolutions(a): 
  
    count = bin(a).count('1') 
    return 2 ** count 
  
# Driver Code 
if __name__ == "__main__":
  
    a = 3
    print(countSolutions(a)) 
  
# This code is contributed by
# Rituraj Jain


C#
// C# program to find the number of 
// values of b such that a = b + (a^b)
class GFG
{
  
// function to return the number 
// of solutions
static int countSolutions(int a)
{
    int count = bitCount(a);
  
    count =(int) System.Math.Pow(2, count);
    return count;
}
  
static int bitCount(int n)
{
    int count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
  
// Driver Code
public static void Main() 
{
    int a = 3;
    System.Console.WriteLine(countSolutions(a));
}
}
  
// This code is contributed by mits


PHP


输出:
4

时间复杂度:O(a)

一种有效的方法是,当我们针对不同的值编写可能的解决方案时,观察答案的模式。 a 。仅将设置的位用于确定可能的答案的数量。问题的答案将始终是2 ^(设置位数),可以通过观察确定。

下面是上述方法的实现:

C++

// C++ program to find the number of values
// of b such that a = b + (a^b)
#include 
using namespace std;
  
// function to return the number of solutions
int countSolutions(int a)
{
    int count = __builtin_popcount(a);
  
    count = pow(2, count);
    return count;
}
  
// Driver Code
int main()
{
    int a = 3;
    cout << countSolutions(a);
}

Java

// Java program to find the number of values
// of b such that a = b + (a^b)
import java.io.*;
class GFG
{
   
    // function to return the number of solutions
    static int countSolutions(int a)
    {
        int count = Integer.bitCount(a);
       
        count =(int) Math.pow(2, count);
        return count;
    }
       
    // Driver Code
        public static void main (String[] args) 
    {
        int a = 3;
        System.out.println(countSolutions(a));
    }
}
// This code is contributed by Raj

Python3

# Python3 program to find the number 
# of values of b such that a = b + (a^b) 
  
# function to return the number 
# of solutions 
def countSolutions(a): 
  
    count = bin(a).count('1') 
    return 2 ** count 
  
# Driver Code 
if __name__ == "__main__":
  
    a = 3
    print(countSolutions(a)) 
  
# This code is contributed by
# Rituraj Jain

C#

// C# program to find the number of 
// values of b such that a = b + (a^b)
class GFG
{
  
// function to return the number 
// of solutions
static int countSolutions(int a)
{
    int count = bitCount(a);
  
    count =(int) System.Math.Pow(2, count);
    return count;
}
  
static int bitCount(int n)
{
    int count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
  
// Driver Code
public static void Main() 
{
    int a = 3;
    System.Console.WriteLine(countSolutions(a));
}
}
  
// This code is contributed by mits

的PHP


输出:
4

时间复杂度: O(log N)