CF742B Arpa's obvious problem and Mehrdad's terrible solution 題解
阿新 • • 發佈:2021-12-16
CF742B Arpa's obvious problem and Mehrdad's terrible solution 題解
。
Content
有一個長度為 \(n\) 的陣列,請求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的數對 \((i,j)\) 的個數。其中 \(\oplus\) 表示異或符號。
資料範圍:\(1\leqslant n,a_i\leqslant 10^5,0\leqslant x\leqslant 10^5\)。
Solution
利用一個異或的性質:\(a\oplus b=c\Rightarrow a\oplus c=b,b\oplus c=a\),我們發現問題其實就迎刃而解了。直接統計每個數異或 \(x\) 得到的數在原數組裡面的個數再加起來就好了。
注意,數對可能很多,要開 \(\texttt{long long}\)
Code
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <iostream> #include <map> using namespace std; int n, x, a[100007]; map<int, int> vis; long long ans; int main() { scanf("%d%d", &n, &x); for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); ans += vis[a[i] ^ x]; vis[a[i]]++; } printf("%d", ans / 2); return 0; }