Advertisement
Guest User

I - Matrix Sum

a guest
Aug 29th, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. #define mk make_pair
  4. #define fi first
  5. #define se second
  6. #define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
  7. using namespace std;
  8. typedef vector<int> vi;
  9. typedef pair<int,int> ii;
  10. typedef long long ll;
  11. typedef vector<bool> vb;
  12. const int N=1010;
  13.  
  14. ll sm, s, n, r[N], c[N], m[N][N], a[N][N];
  15.  
  16. int main(void) {
  17.     cin >> n;
  18.     For(i,0,n)
  19.         For(j,0,n) {
  20.             cin >> m[i][j];
  21.             c[j]+=m[i][j];
  22.             r[i]+=m[i][j];
  23.             sm += m[i][j];
  24.         }
  25.  
  26.     if (n == 1) {
  27.         if (m[0][0] == 0 || m[0][0] == 1)
  28.             goto ans_1;
  29.         goto ans_0;
  30.     }
  31.  
  32.     if (sm % (2*n - 1) != 0)
  33.         goto ans_0;
  34.    
  35.     s = sm / (ll)(2*n-1);
  36.  
  37.     for (int i = 0; i<n; i++)
  38.         for (int j = 0; j<n; j++) {
  39.             if ((c[j]-s)%(n-1) != 0)
  40.                 goto ans_0;
  41.            
  42.             ll sa = (c[j]-s)/(n-1);
  43.  
  44.             if ((r[i]-s)%(n-1) != 0)
  45.                 goto ans_0;
  46.  
  47.             ll sb = (r[i]-s)/(n-1);
  48.  
  49.             a[i][j] = sa + sb - m[i][j];
  50.             if (a[i][j] > 1 || a[i][j] < 0)
  51.                 goto ans_0;
  52.         }
  53.  
  54. ans_1:
  55.     cout << "1\n";
  56.     return 0;
  57.  
  58. ans_0:
  59.     cout << "0\n";
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement