Problem p02130 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \\
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

const int MOD = 10007;
long long mod(long long a, int m) { return (a % m + m) % m; }
inline long long inv(long long a, int m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  return mod(u, m);
}

template <class T> struct Matrix {
  vector<vector<T>> val;
  Matrix(int n = 1, int m = 1) {
    val.clear();
    val.resize(n, vector<T>(m));
  }
  Matrix(int n, int m, T x) {
    val.clear();
    val.resize(n, vector<T>(m, x));
  }
  void init(int n, int m, T x = 0) {
    val.clear();
    val.resize(n, vector<T>(m, x));
  }
  void resize(int n, int m, T x = 0) {
    val.resize(n);
    for (int i = 0; i < n; ++i)
      val[i].resize(m, x);
  }
  int size() { return val.size(); }
  inline vector<T> &operator[](int i) { return val[i]; }
  friend ostream &operator<<(ostream &s, Matrix<T> M) {
    s << endl;
    for (int i = 0; i < M.val.size(); ++i)
      s << M[i] << endl;
    return s;
  }
};

template <class T> int Rank(Matrix<T> A, int p) {
  int rank = 0;
  /*
for (int i = 0; i < A.size(); ++i)
  for (int j = 0; j < A[0].size(); ++j)
      A[i][j] = mod(A[i][j], p);
  */

  T res = 1;
  for (int i = 0; i < A[0].size(); ++i) {
    int pivot = rank;
    bool exist = false;
    for (int j = rank; j < A.size(); ++j) {
      if (A[j][i] != 0) {
        pivot = j;
        exist = true;
        break;
      }
    }
    if (exist) {
      swap(A[pivot], A[rank]);
      res = mod(res * A[rank][i], p);
      T ainv = inv(A[rank][i], p);
      vector<T> tmp(A[0].size());
      for (int j = 0; j < A[0].size(); ++j) {
        tmp[j] = mod(A[rank][j] * ainv, p);
      }
      for (int j = 0; j < A.size(); ++j) {
        if (j != rank && A[j][i] != 0) {
          T fac = A[j][i];
          for (int k = 0; k < A[0].size(); ++k) {
            A[j][k] = mod(A[j][k] - tmp[k] * fac, p);
          }
        }
      }
      ++rank;
    }
    // else return 0;
  }

  return rank;
};

unsigned int randInt() {
  static unsigned int tx = 123456789, ty = 362436069, tz = 521288629,
                      tw = 88675123;
  unsigned int tt = (tx ^ (tx << 11));
  tx = ty;
  ty = tz;
  tz = tw;
  return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
}

int randInt(int minv, int maxv) { return randInt() % (maxv - minv + 1) + minv; }

int N, A, B;
int a[2100], b[2100];

int main() {
  while (cin >> N >> A >> B) {
    for (int i = 0; i < N; ++i)
      cin >> a[i] >> b[i];

    vector<int> dame;
    for (int i = 0; i < N; ++i) {
      if (abs(a[i] - b[i]) <= A ||
          (abs(a[i] - b[i]) >= B && abs(a[i] - b[i]) <= A * 2))
        continue;
      dame.push_back(i);
    }

    if (dame.empty()) {
      cout << N << endl;
      continue;
    }

    int V = dame.size();

    Matrix<int> M(V, V, 0);
    int iter = 0;
    for (int i = 0; i < dame.size(); ++i) {
      for (int j = i + 1; j < dame.size(); ++j) {
        int u = dame[i], v = dame[j];
        int dist = abs(a[u] + a[v] - b[u] - b[v]);
        if (dist <= A || (dist >= B && dist <= A * 2)) {
          int ran = randInt(1, MOD - 1);
          M[i][j] = ran;
          M[j][i] = MOD - ran;
        }
      }
    }

    /*
    COUT(dame);
    COUT(M);
    */

    int rank = Rank(M, MOD);

    // COUT(rank);

    int res = rank / 2 + (N - (int)dame.size());
    cout << res << endl;
    // COUT(single);

    /*
    Matrix<int> M(V, V, 0);
    for (int i = 0; i < single.size(); ++i) {
      int u = single[i];
      int v = N + i;
      int ran = randInt(1, MOD-1);
      M[u][v] = ran;
      M[v][u] = MOD - ran;
    }


    //Matrix<int> M(N, N, 0);
    for (int i = 0; i < N; ++i) {
      for (int j = i+1; j < N; ++j) {
            int dist = abs(a[i] + a[j] - b[i] - b[j]);
            if (dist <= A || (dist >= B && dist <= A*2)) {
              int ran = randInt(1, MOD-1);
              M[i][j] = ran;
              M[j][i] = MOD - ran;
            }
      }
    }


    //COUT(M);

    int res = Rank(M, MOD);
    //COUT(res);

    */
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \\
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

const int MOD = 10007;
long long mod(long long a, int m) { return (a % m + m) % m; }
inline long long inv(long long a, int m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  return mod(u, m);
}

template <class T> struct Matrix {
  vector<vector<T>> val;
  Matrix(int n = 1, int m = 1) {
    val.clear();
    val.resize(n, vector<T>(m));
  }
  Matrix(int n, int m, T x) {
    val.clear();
    val.resize(n, vector<T>(m, x));
  }
  void init(int n, int m, T x = 0) {
    val.clear();
    val.resize(n, vector<T>(m, x));
  }
  void resize(int n, int m, T x = 0) {
    val.resize(n);
    for (int i = 0; i < n; ++i)
      val[i].resize(m, x);
  }
  int size() { return val.size(); }
  inline vector<T> &operator[](int i) { return val[i]; }
  friend ostream &operator<<(ostream &s, Matrix<T> M) {
    s << endl;
    for (int i = 0; i < M.val.size(); ++i)
      s << M[i] << endl;
    return s;
  }
};

template <class T> int Rank(Matrix<T> A, int p) {
  int rank = 0;
  for (int i = 0; i < A.size(); ++i)
    for (int j = 0; j < A[0].size(); ++j)
      A[i][j] = mod(A[i][j], p);

  T res = 1;
  for (int i = 0; i < A[0].size(); ++i) {
    int pivot = rank;
    bool exist = false;
    for (int j = rank; j < A.size(); ++j) {
      if (A[j][i] != 0) {
        pivot = j;
        exist = true;
        break;
      }
    }
    if (exist) {
      swap(A[pivot], A[rank]);
      res = mod(res * A[rank][i], p);
      T ainv = inv(A[rank][i], p);
      vector<T> tmp(A[0].size());
      for (int j = 0; j < A[0].size(); ++j) {
        tmp[j] = mod(A[rank][j] * ainv, p);
      }
      for (int j = 0; j < A.size(); ++j) {
        if (j != rank && A[j][i] != 0) {
          T fac = A[j][i];
          for (int k = 0; k < A[0].size(); ++k) {
            A[j][k] = mod(A[j][k] - tmp[k] * fac, p);
          }
        }
      }
      ++rank;
    }
    // else return 0;
  }

  return rank;
};

unsigned int randInt() {
  static unsigned int tx = 123456789, ty = 362436069, tz = 521288629,
                      tw = 88675123;
  unsigned int tt = (tx ^ (tx << 11));
  tx = ty;
  ty = tz;
  tz = tw;
  return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
}

int randInt(int minv, int maxv) { return randInt() % (maxv - minv + 1) + minv; }

int N, A, B;
int a[2100], b[2100];

int main() {
  while (cin >> N >> A >> B) {
    for (int i = 0; i < N; ++i)
      cin >> a[i] >> b[i];

    vector<int> single;
    for (int i = 0; i < N; ++i) {
      if (abs(a[i] - b[i]) <= A ||
          (abs(a[i] - b[i]) >= B && abs(a[i] - b[i]) <= A * 2))
        single.push_back(i);
    }

    int add = single.size();
    int V = N + add;

    // COUT(single);

    Matrix<int> M(V, V, 0);
    for (int i = 0; i < single.size(); ++i) {
      int u = single[i];
      int v = N + i;
      int ran = randInt(1, MOD - 1);
      M[u][v] = ran;
      M[v][u] = MOD - ran;
    }

    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        int dist = abs(a[i] + a[j] - b[i] - b[j]);
        if (dist <= A || (dist >= B && dist <= A * 2)) {
          int ran = randInt(1, MOD - 1);
          M[i][j] = ran;
          M[j][i] = MOD - ran;
        }
      }
    }

    // COUT(M);

    int res = Rank(M, MOD);
    // COUT(res);

    cout << res / 2 << endl;
  }
}