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

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define EPS 1e-12
// #define B 1000000

bool isPrime(ll n) {
  if (n == 1)
    return false;
  if (n == 2)
    return true;

  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0)
      return false;
  }
  return true;
}

ll MOD = 4294967311LL;
ll mod(ll x) { return (x % MOD + MOD) % MOD; }

ll mul(ll x, ll n) {
  ll ret = 0;
  ret += mod(x * (n % (1 << 16)));
  n >>= 16;
  ret += mod(mod(x * (n % (1 << 16))) << 16);
  n >>= 16;
  ret += mod(mod(mod(x * (n % (1 << 16))) << 16) << 16);
  n >>= 16;
  return ret;
  /*
          ll ret = 0;

          if (x <= (1LL << 31) && n <= (1LL << 32)) {
                  return x * n % MOD;
          }

          for (ll i = 0, k = 1; k <= n; ++i, k <<= 1) {
                  if (n & k) {
                          ret = mod(ret + x);
                  }
                  x = mod(x + x);
          }
          return ret;
  */
}
ll power(ll x, ll n) {
  ll ret = 1;
  for (ll i = 0, k = 1; k <= n; ++i, k <<= 1) {
    if (n & k) {
      ret = mul(ret, x);
    }
    x = mul(x, x);
  }
  return ret;
}

ll Div(ll x, ll n) { return mul(x, power(n, MOD - 2)); }
ll cal(vector<ll> op, vector<ll> Y) {
  ll Z = 1;
  ll X = 0;
  int N = op.size();
  bool zf = false;
  for (int i = N - 1; i >= 0; --i) {
    bool f = zf ^ (Y[i] < 0);
    ll y = abs(Y[i]);
    switch (op[i]) {
    case 2:
      f = !f;
    case 1:
      X = mod(X + (f ? -1 : 1) * mul(Z, y));
      break;

    case 3:
      Z = mul(Z, y);
      zf = f;
      break;
    case 4:
      Z = Div(Z, y);
      zf = f;
      break;
    }
  }
  return X;
}

int main() {
  //	while ( !isPrime(MOD) ) ++MOD;
  //	cout << MOD << endl;
  //	return 0;
  ll X = 0;

  int N;
  cin >> N;
  vector<ll> op(N), Y(N);
  for (int i = 0; i < N; ++i) {
    cin >> op[i] >> Y[i];
  }

  ll ans = cal(op, Y);

  if (ans >= (1LL << 31))
    cout << ans - MOD << endl;
  else
    cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;

#define EPS 1e-12
// #define B 1000000

bool isPrime(ll n) {
  if (n == 1)
    return false;
  if (n == 2)
    return true;

  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0)
      return false;
  }
  return true;
}

ll MOD = 4294967311LL, D;
ll mod(ll x) { return (x % MOD + MOD) % MOD; }

ll mul(ll x, ll n) {
  ll ret = 0;

  if (x <= (1LL << 31) && n <= (1LL << 32)) {
    return x * n % MOD;
  }

  for (ll i = 0, k = 1; k <= n; ++i, k <<= 1) {
    if (n & k) {
      ret = mod(ret + x);
    }
    x = mod(x + x);
  }
  return ret;
}
ll power(ll x, ll n) {
  ll ret = 1;
  for (ll i = 0, k = 1; k <= n; ++i, k <<= 1) {
    if (n & k) {
      ret = mod(mul(ret, x));
    }
    x = mod(mul(x, x));
  }
  return ret;
}

ll Div(ll x, ll n) { return mul(x, power(n, MOD - 2)); }
ll cal(vector<ll> op, vector<ll> Y) {
  ll Z = 1;
  ll X = 0;
  int N = op.size();
  bool zf = false;
  for (int i = N - 1; i >= 0; --i) {
    bool f = zf ^ (Y[i] < 0);
    ll y = abs(Y[i]);
    switch (op[i]) {
    case 2:
      f = !f;
    case 1:
      X = mod(X + (f ? -1 : 1) * mul(Z, y));
      break;

    case 3:
      Z = mod(mul(Z, y));
      zf = f;
      break;
    case 4:
      Z = mod(Div(Z, y));
      zf = f;
      break;
    }
  }
  return X;
}

int main() {
  //	while ( !isPrime(MOD) ) ++MOD;
  //	cout << MOD << endl;
  //	return 0;
  ll X = 0;

  int N;
  cin >> N;
  vector<ll> op(N), Y(N);
  for (int i = 0; i < N; ++i) {
    cin >> op[i] >> Y[i];
  }

  ll ans = cal(op, Y);

  if (ans >= (1LL << 31))
    cout << ans - MOD << endl;
  else
    cout << ans << endl;
}