Dang_Quan_10_Tin

GEN

Nov 6th, 2021 (edited)
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #define task "GEN"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 1e5 + 5;
  12. int n, a[N], cnt[N];
  13.  
  14. void Read()
  15. {
  16.     cin >> n;
  17.     for (int i = 1; i <= n; ++i)
  18.         cin >> a[i];
  19. }
  20.  
  21. // Kiểm tra liệu số x có bao nhiêu bit mở
  22. bool OnlyOneBit(int x)
  23. {
  24.     // Ở đây (x & -x) là lũy thừa lớn nhất của 2 là ước của x
  25.     // x có một bit mở nếu và chỉ nếu x là lũy thừa của 2
  26.     return x == (x & -x);
  27. }
  28.  
  29. void Solve()
  30. {
  31.     ll ans(0);
  32.  
  33.     for (int i = 1; i <= n; ++i)
  34.         ++cnt[a[i]]; // Đếm phân phối
  35.  
  36.     for (int x = 1; x <= 1000; ++x)
  37.     {
  38.         ans += 1ll * cnt[x] * (cnt[x] - 1) / 2; // Bài toán số 1
  39.  
  40.         for (int y = x + 1; y <= 1000; ++y)
  41.             if (OnlyOneBit(x ^ y))
  42.                 ans += cnt[x] * cnt[y]; // Bài toán số 2
  43.             // Hai bit giống nhau xor sẽ ra 0
  44.     }
  45.  
  46.     cout << ans;
  47. }
  48.  
  49. int32_t main()
  50. {
  51.     ios::sync_with_stdio(0);
  52.     cin.tie(0);
  53.     cout.tie(0);
  54.     if (fopen(task ".INP", "r"))
  55.     {
  56.         freopen(task ".INP", "r", stdin);
  57.         freopen(task ".OUT", "w", stdout);
  58.     }
  59.  
  60.     Read();
  61.     Solve();
  62. }
  63.  
Add Comment
Please, Sign In to add comment