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

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

ll modpower(ll a, ll r, ll mod) { // a^r
  ll x = 1;
  a %= mod;
  while (r > 0) {
    if (r & 1)
      x = x * a % mod;
    r >>= 1;
    a = a * a % mod;
  }
  return x;
}

int main() {
  ll mod = 1e9 + 7;
  int N;
  cin >> N;
  vector<ll> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  multiset<pair<ll, ll>> mst;
  ll ans = 1;
  ll zero = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] == 0 && B[i] == 0) {
      zero++;
      continue;
    }
    ll g = gcd(abs(A[i]), abs(B[i]));
    A[i] /= g;
    B[i] /= g;
    if (A[i] < 0) {
      A[i] *= -1;
      B[i] *= -1;
    }
    mst.insert({A[i], B[i]});
  }

  while (!mst.empty()) {
    auto it = mst.begin();
    ll a = (*it).first, b = (*it).second;
    ll cnt_a = 1, cnt_b = 0;
    mst.erase(it);

    it = mst.lower_bound({a, b});
    while (it != mst.end() && (*it).first == a && (*it).second == b) {
      cnt_a++;
      it = mst.erase(it);
    }

    it = mst.lower_bound({-a, -b});
    while (it != mst.end() && (*it).first == -a && (*it).second == -b) {
      cnt_a++;
      it = mst.erase(it);
    }

    it = mst.lower_bound({b, -a});
    while (it != mst.end() && (*it).first == b && (*it).second == -a) {
      cnt_b++;
      it = mst.erase(it);
    }

    it = mst.lower_bound({-b, a});
    while (it != mst.end() && (*it).first == -b && (*it).second == a) {
      cnt_b++;
      it = mst.erase(it);
    }

    ans *= modpower(2, cnt_a, mod) + modpower(2, cnt_b, mod) - 1;
    ans %= mod;
  }
  cout << (ans + zero + mod - 1) % mod << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

ll modpower(ll a, ll r, ll mod) { // a^r
  ll x = 1;
  a %= mod;
  while (r > 0) {
    if (r & 1)
      x = x * a % mod;
    r >>= 1;
    a = a * a % mod;
  }
  return x;
}

int main() {
  ld pi = 3.141592653589793238462643383;
  ld eps = 2e-16;
  ll mod = 1e9 + 7;
  int N;
  cin >> N;
  vector<ll> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  multiset<pair<ld, int>> mst;
  vector<bool> checked(N, false);
  ll ans = 1;
  ll zero = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] == 0 && B[i] == 0) {
      zero++;
      continue;
    }
    mst.insert({atan2((ld)A[i], (ld)B[i]), i});
  }

  while (!mst.empty()) {
    auto it = mst.begin();
    ld a = (*it).first;
    int i = (*it).second;
    ll cnt_a = 1, cnt_b = 0;
    mst.erase(it);

    it = mst.lower_bound({a, 0});
    while (it != mst.end() && (*it).first < a + eps) {
      int j = (*it).second;
      if (((A[i] % mod) * (B[j] % mod) - (B[i] % mod) * (A[j] % mod)) % mod !=
          0) {
        it++;
        continue;
      }
      cnt_a++;
      it = mst.erase(it);
    }

    it = mst.lower_bound({a + pi / 2, 0});
    while (it != mst.end() && (*it).first < a + pi / 2 + eps) {
      int j = (*it).second;
      if (((A[i] % mod) * (A[j] % mod) + (B[i] % mod) * (B[j] % mod)) % mod !=
          0) {
        it++;
        continue;
      }
      cnt_b++;
      it = mst.erase(it);
    }

    it = mst.lower_bound({a + pi, 0});
    while (it != mst.end() && (*it).first < a + pi + eps) {
      int j = (*it).second;
      if (((A[i] % mod) * (B[j] % mod) - (B[i] % mod) * (A[j] % mod)) % mod !=
          0) {
        it++;
        continue;
      }
      cnt_a++;
      it = mst.erase(it);
    }

    it = mst.lower_bound({a + pi * 3 / 2, 0});
    while (it != mst.end() && (*it).first < a + pi * 3 / 2 + eps) {
      int j = (*it).second;
      if (((A[i] % mod) * (A[j] % mod) + (B[i] % mod) * (B[j] % mod)) % mod !=
          0) {
        it++;
        continue;
      }
      cnt_b++;
      it = mst.erase(it);
    }

    it = mst.lower_bound({a + pi * 2, 0});
    while (it != mst.end() && (*it).first < a + pi * 2 + eps) {
      int j = (*it).second;
      if (((A[i] % mod) * (B[j] % mod) - (B[i] % mod) * (A[j] % mod)) % mod !=
          0) {
        it++;
        continue;
      }
      cnt_a++;
      it = mst.erase(it);
    }

    ans *= modpower(2, cnt_a, mod) + modpower(2, cnt_b, mod) - 1;
    ans %= mod;
  }
  cout << (ans + zero + mod - 1) % mod << endl;
  return 0;
}