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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll modpow(ll a, ll t) {
  if (t < 0)
    assert(false);
  ll ret = 1LL;
  while (t) {
    if (t & 1LL) {
      ret *= a;
      ret %= MOD;
    }
    a *= a;
    a %= MOD;
    t >>= 1;
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  ll a, b, x = 0, y = 0, z = 0;

  map<Pll, ll> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    if (a < 0) {
      a *= -1;
      b *= -1;
    }
    if (a == 0) {
      if (b == 0)
        ++z;
      else
        ++y;
    } else {
      if (b == 0)
        ++x;
      else {
        ll g = gcd(a, b);
        a /= g;
        b /= g;
        ++mp[{a, b}];
      }
    }
  }

  ll ans = 1LL;
  vector<Pll> p;
  if (x != 0) {
    p.emplace_back(x, y);
  } else if (y != 0) {
    p.emplace_back(y, x);
  }
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    tie(a, b) = (itr->first);
    // cerr << "mp: " << a << ", " << b << endl;
    if (mp.find({-b, a}) != mp.end()) {
      if (Pii(a, b) < Pii(-b, a)) {
        p.emplace_back(itr->second, mp[{-b, a}]);
      }
    } else if (mp.find({b, -a}) != mp.end()) {
      if (Pii(a, b) < Pii(b, -a)) {
        p.emplace_back(itr->second, mp[{b, -a}]);
      }
    } else {
      p.emplace_back(itr->second, 0);
    }
  }

  for (Pll q : p) {
    tie(a, b) = q;
    // cerr << a << " " << b << endl;
    if (b == 0) {
      ans *= modpow(2LL, a);
    } else {
      ans *= (((modpow(2LL, a) + modpow(2LL, b)) % MOD) + MOD - 1LL) % MOD;
    }
    ans %= MOD;
  }
  ans += z;
  ans %= MOD;
  ans += MOD - 1;
  ans %= MOD;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll modpow(ll a, ll t) {
  if (t < 0)
    assert(false);
  ll ret = 1LL;
  while (t) {
    if (t & 1LL) {
      ret *= a;
      ret %= MOD;
    }
    a *= a;
    a %= MOD;
    t >>= 1;
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  ll a, b, x = 0, y = 0, z = 0;

  map<Pll, ll> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    if (a < 0) {
      a *= -1;
      b *= -1;
    }
    if (a == 0) {
      if (b == 0)
        ++z;
      else
        ++y;
    } else {
      if (b == 0)
        ++x;
      else {
        ll g = gcd(a, b);
        a /= g;
        b /= g;
        ++mp[{a, b}];
      }
    }
  }

  ll ans = 1LL;
  vector<Pll> p;
  if (x != 0) {
    p.emplace_back(x, y);
  } else if (y != 0) {
    p.emplace_back(y, x);
  }
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    tie(a, b) = (itr->first);
    cerr << "mp: " << a << ", " << b << endl;
    if (mp.find({-b, a}) != mp.end()) {
      if (Pii(a, b) < Pii(-b, a)) {
        p.emplace_back(itr->second, mp[{-b, a}]);
      }
    } else if (mp.find({b, -a}) != mp.end()) {
      if (Pii(a, b) < Pii(b, -a)) {
        p.emplace_back(itr->second, mp[{b, -a}]);
      }
    } else {
      p.emplace_back(itr->second, 0);
    }
  }

  for (Pll q : p) {
    tie(a, b) = q;
    cerr << a << " " << b << endl;
    if (b == 0) {
      ans *= modpow(2LL, a);
    } else {
      ans *= (((modpow(2LL, a) + modpow(2LL, b)) % MOD) + MOD - 1LL) % MOD;
    }
    ans %= MOD;
  }
  ans += z;
  ans %= MOD;
  ans += MOD - 1;
  ans %= MOD;
  cout << ans << endl;
}