给定一个总和还有一个数字 。任务是对所有可能的正数对(a,b)进行计数,以使两个正整数a和b的总和为S ,而X的按位异或。
例子:
Input : S = 9, K = 5
Output : 4
The ordered pairs are (2, 7), (3, 6), (6, 3), (7, 2)
Input : S = 2, K = 2
Output : 0
There are no such ordered pair.
方法:
对于任何两个整数和 ,
Sum S = a + b can be written as S = (a b) + (a & b)*2
Where a b is the bitwise XOR and a & b is bitwise AND of the two number a and b respectively.
这是因为是非携带的二进制加法。因此,我们可以写出a&b =(SK)/ 2 ,其中S =(a + b)和K =(a
*** QuickLaTeX cannot compile formula:
*** Error message:
Error: Nothing to show, formula is empty
b) 。
如果(SK)为奇数或(SK)小于0,
- 那么就没有这样的有序对。
现在,对于每一位, {0,1}和(a b) {0,1}。
- 如果一个 b)= 0,那么a i = b i ,因此我们有一种可能性:a i = b i =(a i &b i )。
- 如果一个 b)= 1,那么我们必须具有(a i &b i )= 0(否则输出为0),并且我们有两个选择:(a i = 1和b i = 0)或(a i = 0且b i = 1)。
其中a i是第i个比特在a和b i是b中的第i比特。
因此,答案是2 , 在哪里是K中的置位位数。
如果S和K相等,我们将减去2,因为a和b必须为正(> 0)。
下面是上述方法的实现:
C++
// C++ program to count ordered pairs of
// positive numbers such that their
// sum is S and XOR is K
#include
using namespace std;
// Function to count ordered pairs of
// positive numbers such that their
// sum is S and XOR is K
int countPairs(int s, int K)
{
// Check if no such pair exists
if (K > s || (s - K) % 2) {
return 0;
}
if ((s - K) / 2 & K) {
return 0;
}
// Calculate set bits in K
int setBits = __builtin_popcount(K);
// Calculate pairs
int pairsCount = pow(2, setBits);
// If s = k, subtract 2 from result
if (s == K)
pairsCount -= 2;
return pairsCount;
}
// Driver code
int main()
{
int s = 9, K = 5;
cout << countPairs(s, K);
return 0;
}
Java
// Java program to count ordered pairs of
// positive numbers such that their
// sum is S and XOR is K
class GFG {
// Function to count ordered pairs of
// positive numbers such that their
// sum is S and XOR is K
static int countPairs(int s, int K) {
// Check if no such pair exists
if (K > s || (s - K) % 2 ==1) {
return 0;
}
if ((s - K) / 2 == 1 & K == 1) {
return 0;
}
// Calculate set bits in K
int setBits = __builtin_popcount(K);
// Calculate pairs
int pairsCount = (int) Math.pow(2, setBits);
// If s = k, subtract 2 from result
if (s == K) {
pairsCount -= 2;
}
return pairsCount;
}
static int __builtin_popcount(int n) {
/* Function to get no of set
bits in binary representation
of positive integer n */
int count = 0;
while (n > 0) {
count += n & 1;
n >>= 1;
}
return count;
}
// Driver program to test above function
public static void main(String[] args) {
int s = 9, K = 5;
System.out.println(countPairs(s, K));
}
}
Python3
# Python3 program to count ordered pairs of
# positive numbers such that their
# sum is S and XOR is K
# Function to count ordered pairs of
# positive numbers such that their
# sum is S and XOR is K
def countPairs(s,K):
if(K>s or (s-K)%2==1):
return 0
# Calculate set bits in k
setBits=(str(bin(K))[2:]).count("1")
# Calculate pairs
pairsCount = pow(2,setBits)
# If s = k, subtract 2 from result
if(s==K):
pairsCount-=2
return pairsCount
# Driver code
if __name__=='__main__':
s,K=9,5
print(countPairs(s,K))
# This code is contributed by
# Indrajit Sinha.
C#
// C# program to count ordered pairs
// of positive numbers such that their
// sum is S and XOR is K
using System;
class GFG
{
// Function to count ordered pairs of
// positive numbers such that their
// sum is S and XOR is K
static int countPairs(int s, int K)
{
// Check if no such pair exists
if (K > s || (s - K) % 2 ==1)
{
return 0;
}
if ((s - K) / 2 == 1 & K == 1)
{
return 0;
}
// Calculate set bits in K
int setBits = __builtin_popcount(K);
// Calculate pairs
int pairsCount = (int) Math.Pow(2, setBits);
// If s = k, subtract 2 from result
if (s == K)
{
pairsCount -= 2;
}
return pairsCount;
}
static int __builtin_popcount(int n)
{
/* Function to get no of set
bits in binary representation
of positive integer n */
int count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
// Driver Code
public static void Main()
{
int s = 9, K = 5;
Console.Write(countPairs(s, K));
}
}
// This code is contributed
// by Rajput-Ji
PHP
$s || ($s - $K) % 2 == 1)
{
return 0;
}
if (($s - $K) / 2 == 1 & $K == 1)
{
return 0;
}
// Calculate set bits in K
$setBits = __builtin_popcount($K);
// Calculate pairs
$pairsCount = (int)pow(2, $setBits);
// If s = k, subtract 2 from result
if ($s == $K)
{
$pairsCount -= 2;
}
return $pairsCount;
}
function __builtin_popcount($n)
{
/* Function to get no of set
bits in binary representation
of positive integer n */
$count = 0;
while ($n > 0)
{
$count += $n & 1;
$n >>= 1;
}
return $count;
}
// Driver Code
$s = 9; $K = 5;
echo countPairs($s, $K) . "\n";
// This code is contributed
// by Akanksha Rai
Javascript
输出:
4
时间复杂度: O(log(K))