📌  相关文章
📜  对从1到N的对进行计数,以使它们的总和可被其XOR整除

📅  最后修改于: 2021-05-07 05:43:53             🧑  作者: Mango

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

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)