给定整数“ x”,找到满足以下条件的“ a”值的数量:
- 0 <=一个<= x
- 一个XOR x = a + x
例子 :
Input : 5
Output : 2
Explanation:
For x = 5, following 2 values
of 'a' satisfy the conditions:
5 XOR 0 = 5+0
5 XOR 2 = 5+2
Input : 10
Output : 4
Explanation:
For x = 10, following 4 values
of 'a' satisfy the conditions:
10 XOR 0 = 10+0
10 XOR 1 = 10+1
10 XOR 4 = 10+4
10 XOR 5 = 10+5
天真的方法:
一种简单的方法是检查“ a”在0到“ x”(包括两个端点)之间的所有值,并计算与x的XOR,并检查条件2是否满足。
下面是上述想法的实现:
C++
// C++ program to find count of values whose XOR
// with x is equal to the sum of value and x
// and values are smaller than equal to x
#include
using namespace std;
int FindValues(int x)
{
// Initialize result
int count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for (int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
// Driver code
int main()
{
int x = 10;
// Function call
cout << FindValues(x);
return 0;
}
Java
// Java program to find count of values whose XOR
// with x is equal to the sum of value and x
// and values are smaller than equal to x
class Fib
{
static int FindValues(int x)
{
// Initialize result
int count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for (int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
// Driver code
public static void main(String[] args)
{
int x = 10;
// Function call
System.out.println(FindValues(x));
}
}
Python3
# Python3 program to find count of
# values whose XOR with x is equal
# to the sum of value and x and
# values are smaller than equal to x
def FindValues(x):
# Initialize result
count = 0
# Traversing through all values
# between 0 and x both inclusive
# and counting numbers that
# satisfy given property
for i in range(0, x):
if ((x + i) == (x ^ i)):
count = count + 1
return count
# Driver code
x = 10
# Function call
print(FindValues(x))
# This code is contributed
# by Shivi_Aggarwal
C#
// C# program to find count of values whose XOR
// with x is equal to the sum of value and x
// and values are smaller than equal to x
using System;
class Fib
{
static int FindValues(int x)
{
// Initialize result
int count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for (int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
// Driver code
public static void Main()
{
int x = 10;
// Function call
Console.Write(FindValues(x));
}
}
// This code is contributed by Nitin Mittal.
PHP
C++
// C++ program to count numbers whose bitwise
// XOR and sum with x are equal
#include
using namespace std;
// Function to find total 0 bit in a number
long CountZeroBit(long x)
{
unsigned int count = 0;
while (x)
{
if (!(x & 1LL))
count++;
x >>= 1LL;
}
return count;
}
// Function to find Count of non-negative numbers
// less than or equal to x, whose bitwise XOR and
// SUM with x are equal.
long CountXORandSumEqual(long x)
{
// count number of zero bit in x
long count = CountZeroBit(x);
// power of 2 to count
return (1LL << count);
}
// Driver code
int main()
{
long x = 10;
// Function call
cout << CountXORandSumEqual(x);
return 0;
}
Java
// Java program to count
// numbers whose bitwise
// XOR and sum with x
// are equal
import java.io.*;
class GFG {
// Function to find total
// 0 bit in a number
static long CountZeroBit(long x)
{
long count = 0;
while (x > 0) {
if ((x & 1L) == 0)
count++;
x >>= 1L;
}
return count;
}
// Function to find Count
// of non-negative numbers
// less than or equal to x,
// whose bitwise XOR and
// SUM with x are equal.
static long CountXORandSumEqual(long x)
{
// count number of
// zero bit in x
long count = CountZeroBit(x);
// power of 2 to count
return (1L << count);
}
// Driver code
public static void main(String[] args)
{
long x = 10;
// Function call
System.out.println(CountXORandSumEqual(x));
}
}
// The code is contributed by ajit
Python3
# Python3 program to count numbers whose bitwise
# XOR and sum with x are equal
# Function to find total 0 bit in a number
def CountZeroBit(x):
count = 0
while (x):
if ((x & 1) == 0):
count += 1
x >>= 1
return count
# Function to find Count of non-negative numbers
# less than or equal to x, whose bitwise XOR and
# SUM with x are equal.
def CountXORandSumEqual(x):
# count number of zero bit in x
count = CountZeroBit(x)
# power of 2 to count
return (1 << count)
# Driver code
if __name__ == '__main__':
x = 10
# Function call
print(CountXORandSumEqual(x))
# This code is contributed by 29AjayKumar
C#
// C# program to count
// numbers whose bitwise
// XOR and sum with x
// are equal
using System;
class GFG {
// Function to find total
// 0 bit in a number
static int CountZeroBit(int x)
{
int count = 0;
while (x > 0) {
if ((x & 1) == 0)
count++;
x >>= 1;
}
return count;
}
// Function to find Count
// of non-negative numbers
// less than or equal to x,
// whose bitwise XOR and
// SUM with x are equal.
static int CountXORandSumEqual(int x)
{
// count number of
// zero bit in x
int count = CountZeroBit(x);
// power of 2 to count
return (1 << count);
}
// Driver code
static public void Main()
{
int x = 10;
// Function call
Console.WriteLine(CountXORandSumEqual(x));
}
}
// The code is contributed by ajit
PHP
>= 1;
}
return $count;
}
// Function to find Count of
// non-negative numbers less
// than or equal to x, whose
// bitwise XOR and SUM with
// x are equal.
function CountXORandSumEqual($x)
{
// count number of zero bit in x
$count = CountZeroBit($x);
// power of 2 to count
return (1 << $count);
}
// Driver code
$x = 10;
// Function call
echo CountXORandSumEqual($x);
// This code is contributed by m_kit
?>
输出
4
时间复杂度: O(x)。
高效方法:
XOR模拟二进制加法,而不会结转到下一位。对于’a’的零位,我们可以加1或0而不会得到进位,这意味着xor = +,而如果’a’中的位为1,则x中的匹配位被强制为0,以避免携带。对于x中匹配数字中的’a’中的每个0,其总组合计数为2 ^(零个数)可以是1或0。因此,我们只需要以数字的二进制表示形式来计算0的数量,答案将为2 ^(零的数量)。
下面是上述想法的实现:
C++
// C++ program to count numbers whose bitwise
// XOR and sum with x are equal
#include
using namespace std;
// Function to find total 0 bit in a number
long CountZeroBit(long x)
{
unsigned int count = 0;
while (x)
{
if (!(x & 1LL))
count++;
x >>= 1LL;
}
return count;
}
// Function to find Count of non-negative numbers
// less than or equal to x, whose bitwise XOR and
// SUM with x are equal.
long CountXORandSumEqual(long x)
{
// count number of zero bit in x
long count = CountZeroBit(x);
// power of 2 to count
return (1LL << count);
}
// Driver code
int main()
{
long x = 10;
// Function call
cout << CountXORandSumEqual(x);
return 0;
}
Java
// Java program to count
// numbers whose bitwise
// XOR and sum with x
// are equal
import java.io.*;
class GFG {
// Function to find total
// 0 bit in a number
static long CountZeroBit(long x)
{
long count = 0;
while (x > 0) {
if ((x & 1L) == 0)
count++;
x >>= 1L;
}
return count;
}
// Function to find Count
// of non-negative numbers
// less than or equal to x,
// whose bitwise XOR and
// SUM with x are equal.
static long CountXORandSumEqual(long x)
{
// count number of
// zero bit in x
long count = CountZeroBit(x);
// power of 2 to count
return (1L << count);
}
// Driver code
public static void main(String[] args)
{
long x = 10;
// Function call
System.out.println(CountXORandSumEqual(x));
}
}
// The code is contributed by ajit
Python3
# Python3 program to count numbers whose bitwise
# XOR and sum with x are equal
# Function to find total 0 bit in a number
def CountZeroBit(x):
count = 0
while (x):
if ((x & 1) == 0):
count += 1
x >>= 1
return count
# Function to find Count of non-negative numbers
# less than or equal to x, whose bitwise XOR and
# SUM with x are equal.
def CountXORandSumEqual(x):
# count number of zero bit in x
count = CountZeroBit(x)
# power of 2 to count
return (1 << count)
# Driver code
if __name__ == '__main__':
x = 10
# Function call
print(CountXORandSumEqual(x))
# This code is contributed by 29AjayKumar
C#
// C# program to count
// numbers whose bitwise
// XOR and sum with x
// are equal
using System;
class GFG {
// Function to find total
// 0 bit in a number
static int CountZeroBit(int x)
{
int count = 0;
while (x > 0) {
if ((x & 1) == 0)
count++;
x >>= 1;
}
return count;
}
// Function to find Count
// of non-negative numbers
// less than or equal to x,
// whose bitwise XOR and
// SUM with x are equal.
static int CountXORandSumEqual(int x)
{
// count number of
// zero bit in x
int count = CountZeroBit(x);
// power of 2 to count
return (1 << count);
}
// Driver code
static public void Main()
{
int x = 10;
// Function call
Console.WriteLine(CountXORandSumEqual(x));
}
}
// The code is contributed by ajit
的PHP
>= 1;
}
return $count;
}
// Function to find Count of
// non-negative numbers less
// than or equal to x, whose
// bitwise XOR and SUM with
// x are equal.
function CountXORandSumEqual($x)
{
// count number of zero bit in x
$count = CountZeroBit($x);
// power of 2 to count
return (1 << $count);
}
// Driver code
$x = 10;
// Function call
echo CountXORandSumEqual($x);
// This code is contributed by m_kit
?>
输出
4
时间复杂度: O(Log x)