b770簡單

共同通道與奇數差異對

逐無序紀錄對判斷共同開啟數及差異奇偶。

解題思路

枚舉 i<j,a&b 的1位數就是共同開啟通道數,a xor b 的1位數就是恰一者開啟通道數。前者等於 K 且後者為奇數時合格,加上聯集 a|b。位元操作直接實作題意,不需要轉40字元字串。

i<j 同時排除自配與雙重計數,數值相同的不同位置仍照常枚舉,只是其 XOR=0 為偶數而不合格。Python bit_count 與 C++ __builtin_popcountll 都要對完整40位值處理,不能截成32位。

時間 O(N²),空間 O(N) 含 Python 輸入 token;40位視為固定寬度。最多7140對,每個 OR<2^40,總和<2^53,64位足夠。

Step-by-step animation

逐步推演(例子條件見步驟說明)

回報動畫錯誤
步驟 1 / 3

選出一對

自訂紀錄 [1,3]、K=1,二進位是01、11。

01
11
共同通道
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    int n, K;
    cin >> n >> K;
    vector<unsigned long long> a(n);
    for (auto &x : a)
        cin >> x;
    ll cnt = 0, sum = 0;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (__builtin_popcountll(a[i] & a[j]) == K &&
                (__builtin_popcountll(a[i] ^ a[j]) & 1))
                cnt++, sum += a[i] | a[j];
    cout << cnt << ' ' << sum << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多