OMYGAME - Editorial

Problem Link : CodeChef: Practical coding for everyone

First note that when And operation is done between two numbers then the 1’s come in the result are present in both operands. Hence the OR if all the given AND will give that particular number.
For more clarification, see the code.

#include<bits/stdc++.h>
using namespace std;
#define ll long long


int main(){
    int n;
    cin >> n;

    int arr[n][n];
    for(int i = 0 ; i < n ; i++){
        for(int j = 0 ; j < n ; j++){
            cin >> arr[i][j];
        }
    }

    ll sum = 0;
    for(int i = 0 ; i < n ; i++){
        int x = 0;
        for(int j = 0 ; j < n ; j++){
            if(i != j){
                x |= arr[i][j];
            }
        }
        sum += x;
    }

    cout << sum << "\n";

    return 0;
}
2 Likes
#include<bits/stdc++.h>
using namespace std;
#define int long long int

void solve() {
      int n;
      cin >> n;
      vector<vector<int>> a(n, vector<int>(n, 0));
      for(auto &vec: a) {
            for(auto &x: vec) {
                  cin >> x;
            }
      }

      vector<int> b(n, 0);
      for(int i=0; i<n; i++) {
            for(int j=i+1; j<n; j++) {
                  b[i] |= a[i][j];
                  b[j] |= a[i][j];
            }
      }
      cout << accumulate(b.begin(), b.end(), 0) << endl;
}

signed main() {
      ios_base::sync_with_stdio(0);
      cin.tie(0);
      solve();
}

What is wrong with my code?
What i am doing is - i am setting all the bits both in b[i] and b[j] if that are set in a[i][j].
What did i miss?

Pass 0LL instead of 0 in the accumulate() function or else the sum gets stored in an int,causing an overflow!

1 Like

Can anyone explain with details…

Shit. I got a WA for this. thanks @sherlock69