Advertisement
El_GEMMY

array bitwise &

Sep 12th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3.  
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. typedef long long ll;
  8. typedef unsigned long long ull;
  9. typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
  10.  
  11. #define all(v) v.begin(),v.end()
  12. #define rall(v) v.rbegin(),v.rend()
  13. #define MOD 1000000007
  14. #define PI 3.14159265
  15. #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  16. #define imin INT_MIN
  17. #define imax INT_MAX
  18. #define nl '\n'
  19. #define modulo(a, b, m) ((a % m) * (b % m)) % m
  20.  
  21. void Start_Crushing() {
  22.     ios::sync_with_stdio(false);
  23.     cin.tie(nullptr);
  24.     cout.tie(nullptr);
  25. #ifndef ONLINE_JUDGE
  26.     freopen("input.txt", "r", stdin);
  27.     freopen("output.txt", "w", stdout);
  28. #endif
  29. }
  30. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  31. //int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
  32.  
  33.  
  34. void solve(){
  35.     unordered_map<int, long> powerFreq, powerMatching;
  36.  
  37.     int n; cin >> n;
  38.     vector<int> v(n);
  39.  
  40.     long ans = 0;
  41.     for(auto& i : v){
  42.         cin >> i;
  43.         if(log2(i) == int(log2(i))){
  44.             powerFreq[i]++;
  45.         }else{
  46.             for(long bit = 0; bit < 31; bit++){
  47.                 if(i & (1 << bit)){
  48.                     powerFreq[(1 << bit)]++;
  49.                 }
  50.             }
  51.         }
  52.     }
  53.  
  54.     for(auto& [x, y] : powerFreq){
  55.         ans += y * (y - 1) / 2;
  56.     }
  57.     for(auto& [x, y] : powerMatching){
  58.         ans += y * powerFreq[x];
  59.     }
  60.     cout << ans;
  61. }
  62.  
  63. int main(){
  64.     //        freopen("equal.in", "r", stdin);
  65.     Start_Crushing();
  66.     int t = 1;
  67. //    /*is Single Test case?*/ cin >> t;
  68.     while (t--) {
  69.         solve();
  70.         if(!t) break;
  71.         cout << "\n";
  72.     }
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement