Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Count pairs with Bitwise-AND as even number in C++

นับคู่ที่ a & b เป็นเลขคู่

ข้อสังเกต: a & b เป็นคี่ ⟺ a และ b เป็นคี่ทั้งคู่

คู่ที่ AND เป็นคู่ = ทุกคู่ - คู่ที่ AND เป็นคี่ = C(n,2) - C(odd,2)

ตัวอย่าง: {2,5,1,8,3} → n=5, odd=3 (5,1,3) → C(5,2)-C(3,2) = 10-3 = 7

ตัวอย่างโค้ด C++

#include <bits/stdc++.h>
using namespace std;

int count_pair(int arr[], int n) {
    int odd = 0;
    for(int i=0;i

Input

{2,5,1,8,3}

Output

7