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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int N;
typedef long long ll;
map<ll, ll> f, f_;
const int T = 1;
ll mod = 1145141919;
ll ex(ll x, ll p) {
  ll a = 1;
  if (p < 0)
    return 0;
  while (p) {
    if (p % 2)
      a = a * x % mod;
    x = x * x % mod;
    p /= 2;
  }
  return a;
}
void add(ll &x, ll y) {
  x += y;
  x %= mod;
}

const ll ccc = 1000000;
bool prime[ccc + 1];
vector<ll> pr;
void makeprime() {
  ll i, j;
  for (i = 2; i <= ccc; i++)
    prime[i] = true;
  for (i = 2; i * i <= ccc; i++)
    if (prime[i])
      for (j = 2; j * i <= ccc; j++)
        prime[j * i] = false;
  for (i = 2; i <= ccc; i++)
    if (prime[i])
      pr.push_back(i);
}

map<ll, int> fac;
vector<ll> ds;
void dfs(map<ll, int>::iterator it, ll d) {
  if (it == fac.end()) {
    ds.pb(d);
    return;
  }
  ll p = it->fs, q = it->sc, t = 1;
  auto it2 = it;
  it2++;
  rep(i, q / 2 + 1) {
    dfs(it2, d * t);
    t *= p;
  }
}
int main() {
  makeprime();
  cin >> N;
  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    f_[a] += b;
  }
  for (auto p : f_)
    if (p.sc != 0)
      f[p.fs] = p.sc;
  if (f.empty()) {
    puts("Yes 1");
    return 0;
  }
  ll c = abs(f.begin()->sc);
  //	show(c);
  ll tmp = c;
  for (ll p : pr) {
    while (tmp % p == 0)
      tmp /= p, fac[p]++;
  }
  if (tmp > 1) {
    ll a = round(sqrt(tmp));
    if (a * a == tmp)
      fac[a] = 2;
  }
  dfs(fac.begin(), 1);
  sort(all(ds));
  for (ll a : ds) {
    bool ok = 1;
    ll x = 0;
    for (auto p : f)
      add(x, p.sc % mod * ex(a, p.fs));
    if (x != 0)
      ok = 0;
    x = 0;
    for (auto p : f)
      add(x, -p.sc % mod * p.fs % mod * ex(a, p.fs - 1));
    if (x != 0)
      ok = 0;
    if (ok) {
      cout << "Yes " << a << endl;
      return 0;
    }
  }
  puts("No");
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int N;
typedef long long ll;
map<ll, ll> f, f_;
const int T = 10;
ll ms[10] = {}, mod;
ll ex(ll x, ll p) {
  ll a = 1;
  if (p < 0)
    return 0;
  while (p) {
    if (p % 2)
      a = a * x % mod;
    x = x * x % mod;
    p /= 2;
  }
  return a;
}
void add(ll &x, ll y) {
  x += y;
  x %= mod;
}
int main() {
  srand((unsigned)time(NULL));
  rep(i, T) ms[i] = rand() % 1000000000 + 2;
  cin >> N;
  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    f_[a] += b;
  }
  for (auto p : f_)
    if (p.sc != 0)
      f[p.fs] = p.sc;
  if (f.empty()) {
    puts("Yes 1");
    return 0;
  }
  ll c = abs(f.begin()->sc);
  vector<ll> ds;
  for (ll i = 1; i * i <= c; i++) {
    if (c % i == 0)
      ds.pb(i);
  }
  for (ll a : ds) {
    if (c % (a * a))
      continue;
    bool ok = 1;
    rep(i, T) {
      mod = ms[i];
      ll x = 0;
      for (auto p : f)
        add(x, p.sc % mod * ex(a, p.fs));
      if (x != 0)
        ok = 0;
      x = 0;
      for (auto p : f)
        add(x, -p.sc % mod * p.fs % mod * ex(a, p.fs - 1));
      if (x != 0)
        ok = 0;
    }
    if (ok) {
      cout << "Yes " << a << endl;
      return 0;
    }
  }
  puts("No");
}