Ankit_132

Special Operation

Jun 14th, 2023
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8. #define ff     first
  9. #define ss     second
  10. #define pb     push_back
  11. #define ppb    pop_back
  12.  
  13. int main()
  14. {
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(NULL);
  17.     cout.tie(NULL);
  18.  
  19.     _test
  20.     {
  21.         ll int n, m;
  22.         cin>>n>>m;
  23.  
  24.         vector<vector<int>> a(n, vector<int> (m));
  25.         for(auto &b: a) for(auto &e: b)  cin>>e;
  26.  
  27.         ll int tot = 0;
  28.  
  29.         vector<vector<int>> row(n, vector<int> (30));
  30.         vector<vector<int>> col(m, vector<int> (30));
  31.         vector<int> cnt(30);
  32.  
  33.         for(int i=0; i<n; i++)
  34.         {
  35.             for(int j=0; j<m; j++)
  36.             {
  37.                 for(int b=0; b<30; b++)
  38.                 {
  39.                     if(((1<<b)&a[i][j]) > 0)
  40.                     {
  41.                         cnt[b]++;
  42.                         row[i][b]++;
  43.                         col[j][b]++;
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.  
  49.         ll int ans = 0;
  50.         vector<int> tmp(30);
  51.         ll int val = 0;
  52.  
  53.         for(int i=0; i<n; i++)
  54.         {
  55.             for(int j=0; j<m; j++)
  56.             {
  57.                 val = 0;
  58.                 for(int b=0; b<30; b++)
  59.                     tmp[b] = cnt[b] - row[i][b] - col[j][b] + (((1<<b)&a[i][j])>0);
  60.  
  61.                 for(int b=0; b<30; b++)
  62.                 {
  63.                     if(((1<<b)&a[i][j]) == 0)
  64.                         val += tmp[b]*1ll*(1<<b);
  65.                     else
  66.                         val += ((n*1ll*m -n-m +1ll) - tmp[b])*1ll*(1<<b);
  67.                 }
  68.  
  69.  
  70.                 ans = max(ans, val);
  71.             }
  72.         }
  73.  
  74.         cout<<ans<<"\n";
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment