📜  n = x + n⊕x的解数

📅  最后修改于: 2021-05-25 07:26:02             🧑  作者: Mango

给定数字n,我们必须找到X的可能值的数量,使得n = x + n x。 ⊕代表异或

例子:

Input : n = 3
Output : 4
The possible values of x are 0, 1, 2, and 3.

Input : n = 2
Output : 2
The possible values of x are 0 and 2.

蛮力法:我们可以看到x始终等于或小于n,因此我们可以在[0,n]范围内进行迭代并计算满足所需条件的值的数量。这种方法的时间复杂度为O(n)。

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the number of
// solutions of n = n xor x
int numberOfSolutions(int n)
{
    // Counter to store the number
    // of solutions found
    int c = 0;
 
    for (int x = 0; x <= n; ++x)
        if (n == x + n ^ x)
            ++c;
 
    return c;
}
 
// Driver code
int main()
{
    int n = 3;
    cout << numberOfSolutions(n);
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
import java.lang.*;
 
class GFG
{
// Function to find the number of
// solutions of n = n xor x
static int numberOfSolutions(int n)
{
    // Counter to store the number
    // of solutions found
    int c = 0;
 
    for (int x = 0; x <= n; ++x)
        if (n == x + (n ^ x))
            ++c;
 
    return c;
}
 
// Driver code
public static void main(String args[])
{
    int n = 3;
    System.out.print(numberOfSolutions(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python3
# Python 3 implementation
# of above approach
 
# Function to find the number of
# solutions of n = n xor x
def numberOfSolutions(n):
 
    # Counter to store the number
    # of solutions found
    c = 0
 
    for x in range(n + 1):
        if (n ==( x +( n ^ x))):
            c += 1
 
    return c
 
# Driver code
if __name__ == "__main__":
    n = 3
    print(numberOfSolutions(n))
 
# This code is contributed
# by ChitraNayal


C#
// C# implementation of above approach
using System;
 
class GFG
{
// Function to find the number of
// solutions of n = n xor x
static int numberOfSolutions(int n)
{
    // Counter to store the number
    // of solutions found
    int c = 0;
 
    for (int x = 0; x <= n; ++x)
        if (n == x + (n ^ x))
            ++c;
 
    return c;
}
 
// Driver code
public static void Main()
{
    int n = 3;
    Console.Write(numberOfSolutions(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the number of
// solutions of n = n xor x
int numberOfSolutions(int n)
{
    // Number of set bits in n
    int c = 0;
 
    while (n) {
        c += n % 2;
        n /= 2;
    }
 
    // We can also write (1 << c)
    return pow(2, c);
}
 
// Driver code
int main()
{
    int n = 3;
    cout << numberOfSolutions(n);
    return 0;
}


Java
// Java  implementation of above approach
import java.io.*;
 
class GFG {
// Function to find the number of
// solutions of n = n xor x
static int numberOfSolutions(int n)
{
    // Number of set bits in n
    int c = 0;
 
    while (n>0) {
        c += n % 2;
        n /= 2;
    }
 
    // We can also write (1 << c)
    return (int)Math.pow(2, c);
}
 
// Driver code
 
    public static void main (String[] args) {
        int n = 3;
    System.out.println( numberOfSolutions(n));
    }
}
//This code is contributed by anuj_67


Python3
# Python3 implementation of above approach
 
# from math lib. import everything
from math import *
 
# Function to find the number of
# solutions of n = n xor x
def numberOfSolutions(n) :
 
    # Number of set bits in n
    c = 0
 
    while(n) :
        c += n % 2
        n //= 2
 
    # We can also write (1 << c)
    return int(pow(2, c))
 
         
# Driver code    
if __name__ == "__main__" :
 
    n = 3
    print(numberOfSolutions(n))
 
# This code is contributed by ANKITRAI1


C#
// C# implementation of above approach
using System;
 
class GFG
{
// Function to find the number of
// solutions of n = n xor x
static int numberOfSolutions(int n)
{
    // Number of set bits in n
    int c = 0;
 
    while (n > 0)
    {
        c += n % 2;
        n /= 2;
    }
 
    // We can also write (1 << c)
    return (int)Math.Pow(2, c);
}
 
// Driver code
public static void Main ()
{
    int n = 3;
    Console.WriteLine(numberOfSolutions(n));
}
}
 
// This code is contributed by anuj_67


PHP


Javascript


输出:
4

时间复杂度:O(n)

高效的方法:如果我们以二进制形式考虑n,则可以以更有效的方式解决此问题。如果设置了n位(即1),那么我们可以推断出x或n x中必须有一个对应的设置位(但不能同时存在)。如果在x中设置了相应的位,则不会在n x中将其设置为1 1 =0。否则,在n x中将其设置为0 1 =1。因此,对于n中的每个设置位,我们x中可以有一个设置位或一个未设置位。但是,我们不能在x中设置一个与n中未设置的位相对应的位。通过这种逻辑,解的数量被提高到n中置位数量的幂的2。此方法的时间复杂度为O(log n)。

C++

// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the number of
// solutions of n = n xor x
int numberOfSolutions(int n)
{
    // Number of set bits in n
    int c = 0;
 
    while (n) {
        c += n % 2;
        n /= 2;
    }
 
    // We can also write (1 << c)
    return pow(2, c);
}
 
// Driver code
int main()
{
    int n = 3;
    cout << numberOfSolutions(n);
    return 0;
}

Java

// Java  implementation of above approach
import java.io.*;
 
class GFG {
// Function to find the number of
// solutions of n = n xor x
static int numberOfSolutions(int n)
{
    // Number of set bits in n
    int c = 0;
 
    while (n>0) {
        c += n % 2;
        n /= 2;
    }
 
    // We can also write (1 << c)
    return (int)Math.pow(2, c);
}
 
// Driver code
 
    public static void main (String[] args) {
        int n = 3;
    System.out.println( numberOfSolutions(n));
    }
}
//This code is contributed by anuj_67

Python3

# Python3 implementation of above approach
 
# from math lib. import everything
from math import *
 
# Function to find the number of
# solutions of n = n xor x
def numberOfSolutions(n) :
 
    # Number of set bits in n
    c = 0
 
    while(n) :
        c += n % 2
        n //= 2
 
    # We can also write (1 << c)
    return int(pow(2, c))
 
         
# Driver code    
if __name__ == "__main__" :
 
    n = 3
    print(numberOfSolutions(n))
 
# This code is contributed by ANKITRAI1

C#

// C# implementation of above approach
using System;
 
class GFG
{
// Function to find the number of
// solutions of n = n xor x
static int numberOfSolutions(int n)
{
    // Number of set bits in n
    int c = 0;
 
    while (n > 0)
    {
        c += n % 2;
        n /= 2;
    }
 
    // We can also write (1 << c)
    return (int)Math.Pow(2, c);
}
 
// Driver code
public static void Main ()
{
    int n = 3;
    Console.WriteLine(numberOfSolutions(n));
}
}
 
// This code is contributed by anuj_67

的PHP


Java脚本


输出:
4

时间复杂度:O(log n)