📌  相关文章
📜  计算从 1 到 N 的对,使得它们的总和可以被它们的异或整除

📅  最后修改于: 2021-09-06 11:29:18             🧑  作者: Mango

给定一个数字N    ,任务是计算对 (x, y) 的数量,使得它们的总和 (x+y) 可以被它们的异或值 (x^y) 整除,并且条件1 ≤ x < y < N成立。
例子

Input: N = 3
Output: 3
Explanation: 
(1, 2), (1, 3), (2, 3) are the valid pairs

Input: N = 6
Output: 11

方法:

  • 将数组作为输入后,首先我们需要找出该数组中所有可能的对。
  • 所以,从数组中找出对
  • 然后对于每一对,检查该对的总和是否可以被该对的 xor 值整除。如果是,则将所需计数加一。
  • 检查完所有对后,返回或打印该对的计数。

下面是上述方法的实现:

C++
// C++ program to count pairs from 1 to N
// such that their Sum is divisible by their XOR
 
#include 
using namespace std;
 
// Function to count pairs
int countPairs(int n)
{
    // variable to store count
    int count = 0;
 
    // Generate all possible pairs such that
    // 1 <= x < y < n
    for (int x = 1; x < n; x++) {
        for (int y = x + 1; y <= n; y++) {
            if ((y + x) % (y ^ x) == 0)
                count++;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int n = 6;
 
    cout << countPairs(n);
 
    return 0;
}


Java
// Java program to count pairs from 1 to N
// such that their Sum is divisible by their XOR
class GFG
{
     
    // Function to count pairs
    static int countPairs(int n)
    {
        // variable to store count
        int count = 0;
     
        // Generate all possible pairs such that
        // 1 <= x < y < n
        for (int x = 1; x < n; x++)
        {
            for (int y = x + 1; y <= n; y++)
            {
                if ((y + x) % (y ^ x) == 0)
                    count++;
            }
        }
        return count;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 6;
        System.out.println(countPairs(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to count pairs from 1 to N
# such that their Sum is divisible by their XOR
 
# Function to count pairs
def countPairs(n) :
 
    # variable to store count
    count = 0;
 
    # Generate all possible pairs such that
    # 1 <= x < y < n
    for x in range(1, n) :
        for y in range(x + 1, n + 1) :
            if ((y + x) % (y ^ x) == 0) :
                count += 1;
 
    return count;
 
# Driver code
if __name__ == "__main__" :
 
    n = 6;
 
    print(countPairs(n));
 
# This code is contributed by AnkitRai01


C#
// C# program to count pairs from 1 to N
// such that their Sum is divisible by their XOR
using System;
 
public class GFG
{
     
    // Function to count pairs
    static int countPairs(int n)
    {
        // variable to store count
        int count = 0;
     
        // Generate all possible pairs such that
        // 1 <= x < y < n
        for (int x = 1; x < n; x++)
        {
            for (int y = x + 1; y <= n; y++)
            {
                if ((y + x) % (y ^ x) == 0)
                    count++;
            }
        }
        return count;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 6;
        Console.WriteLine(countPairs(n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
11

时间复杂度: O(N 2 )
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live