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

#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int, int> P;
typedef long long ll;
ostream &operator<<(ostream &o, P p) {
  return o << "(" << p.first << "," << p.second << ")";
}
istream &operator>>(istream &i, P &p) { return i >> p.first >> p.second; }
template <class T> istream &operator>>(istream &i, vector<T> &a) {
  for (auto &t : a)
    cin >> t;
  return i;
}
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T> void prArr(T a, string s = " ") {
  int i = 0;
  for (auto t : a)
    cout << (i++ ? s : "") << t;
  cout << endl;
}

class Int {
public:
  string val;
  Int() : val("0") {}
  Int(string num, bool reversed = 0) {
    if (!reversed)
      R(num);
    val = num;
  }
  Int(int num) {
    val = to_string(num);
    R(val);
  }

  void R(string &a) { reverse(a.begin(), a.end()); }
  bool neg(const string &a) const { return a.back() == '-'; }
  char &operator[](int i) {
    assert(i < (int)val.size());
    return val[i];
  }

  bool operator==(Int b) {
    R(b.val);
    return *this == b.val;
  }
  bool operator==(string b) {
    R(b);
    return val == b;
  }
  template <class T> bool operator==(T b) { return *this == Int(b); }

  bool operator!=(Int b) {
    R(b.val);
    return *this != b.val;
  }
  bool operator!=(string b) {
    R(b);
    return !(val == b);
  }
  template <class T> bool operator!=(T b) { return *this != Int(b); }

  bool operator>=(Int b) {
    R(b.val);
    return *this >= b.val;
  }
  bool operator>=(string b) { return !(*this < b); }
  template <class T> bool operator>=(T b) { return *this >= Int(b); }

  bool operator>(Int b) {
    R(b.val);
    return *this > b.val;
  }
  bool operator>(string b) { return *this >= b && *this != b; }
  template <class T> bool operator>(T b) { return *this > Int(b); }

  bool operator<=(Int b) {
    R(b.val);
    return *this <= b.val;
  }
  bool operator<=(string b) { return *this < b || *this == b; }
  template <class T> bool operator<=(T b) { return *this <= Int(b); }

  bool operator<(Int b) {
    R(b.val);
    return *this < b.val;
  }
  template <class T> bool operator<(T b) { return *this < Int(b); }

  bool operator<(string b) {
    R(b);
    const string &a = val;
    if (neg(a) != neg(b))
      return neg(a) ? 1 : 0;
    if (a.size() != b.size())
      return !neg(a) ? (a.size() < b.size()) : (a.size() > b.size());
    for (int i = a.size() - 1; i >= 0; i--)
      if (a[i] != b[i])
        return !neg(a) ? a[i] < b[i] : a[i] > b[i];
    return 0;
  }

  Int operator+(string b) {
    R(b);
    string a = val;
    string fa = neg(a) ? "-" : "";
    string fb = neg(b) ? "-" : "";
    if (neg(a))
      a.pop_back();
    if (neg(b))
      b.pop_back();
    if (Int(a, 1) < Int(b, 1))
      swap(a, b), swap(fa, fb);

    if (fa == fb) {
      a += '0';
      for (int i = 0; i < (int)a.size() - 1; i++) {
        if (i < (int)b.size())
          a[i] += b[i] - '0';
        a[i + 1] += (a[i] - '0') / 10;
        a[i] = (a[i] - '0') % 10 + '0';
      }
      if (a.size() > 1 && a.back() == '0')
        a.pop_back();
      if ((int)a.size() > 1 && a.back() == '0')
        a.pop_back();
      if (a != "0")
        a += fa;
    } else {
      for (int i = 0; i < (int)a.size(); i++) {
        if (i < (int)b.size())
          a[i] -= b[i] - '0';
        if (a[i] - '0' < 0)
          a[i] += 10, a[i + 1]--;
      }
      while ((int)a.size() > 1 && a.back() == '0')
        a.pop_back();
      if (a != "0")
        a += fa;
    }
    return Int(a, 1);
  }

  Int operator*(string b) {
    R(b);
    vector<int> res;
    string a = val;
    string fa = neg(a) ? "-" : "";
    string fb = neg(b) ? "-" : "";
    if (neg(a))
      a.pop_back();
    if (neg(b))
      b.pop_back();
    if (a.size() < b.size())
      swap(a, b), swap(fa, fb);
    if (a == "0" || b == "0")
      return Int("0");

    for (int j = 0; j < (int)b.size(); j++)
      for (int i = 0; i < (int)a.size(); i++) {
        if (i + j >= (int)res.size())
          res.push_back(0);
        res[i + j] += (a[i] - '0') * (b[j] - '0');
      }
    for (int i = 0; i < (int)res.size(); i++) {
      if (res[i] / 10 == 0)
        continue;
      if (i + 1 >= (int)res.size())
        res.push_back(0);
      res[i + 1] += res[i] / 10, res[i] = res[i] % 10;
    }
    string sres;
    for (int i : res)
      sres += char(i + '0');
    if (fa != fb)
      sres += '-';
    return Int(sres, 1);
  }

  Int div(string b, int flg) {
    assert(b != "0");
    string a = val;
    string fa = neg(a) ? "-" : "";
    string fb = b[0] == '-' ? "-" : "";
    if (neg(a))
      a.pop_back();
    if (b[0] == '-')
      b.erase(b.begin());
    Int mod("0");
    string d = "0";
    for (int i = (int)a.size() - 1; i >= 0; i--) {
      mod = mod * 10 + (a[i] - '0');
      while (mod >= b)
        mod -= b, d.back() += 1;
      if (i && d != "0")
        d += '0';
    }
    if (flg == 0 && d != "0" && (fa != fb))
      d = "-" + d;
    if (flg == 1 && mod != "0" && (fa == "-"))
      mod *= -1;
    return flg == 0 ? d : mod;
  }

  Int operator+(Int b) {
    R(b.val);
    return *this + b.val;
  }
  template <class T> Int operator+(T b) { return *this + Int(b); }

  Int operator*(Int b) {
    R(b.val);
    return *this * b.val;
  }
  template <class T> Int operator*(T b) { return *this * Int(b); }

  Int operator-(string b) {
    b = (b[0] == '-') ? b.substr(1) : "-" + b;
    return *this + b;
  }
  Int operator-(Int b) {
    R(b.val);
    return *this - b.val;
  }
  template <class T> Int operator-(T b) { return *this - Int(b); }

  Int operator/(string b) { return div(b, 0); }
  Int operator/(Int b) {
    R(b.val);
    return div(b.val, 0);
  }
  template <class T> Int operator/(T b) { return *this / Int(b); }

  Int operator%(string b) { return div(b, 1); }
  Int operator%(Int b) {
    R(b.val);
    return div(b.val, 1);
  }
  template <class T> Int operator%(T b) { return *this % Int(b); }

  Int operator+=(Int b) { return *this = *this + b; }
  Int operator+=(string b) { return *this = *this + b; }
  template <class T> Int operator+=(T b) { return *this += Int(b); }

  Int operator-=(Int b) { return *this = *this - b; }
  Int operator-=(string b) { return *this = *this - b; }
  template <class T> Int operator-=(T b) { return *this -= Int(b); }

  Int operator*=(Int b) { return *this = *this * b; }
  Int operator*=(string b) { return *this = *this * b; }
  template <class T> Int operator*=(T b) { return *this *= Int(b); }

  Int operator/=(string b) { return *this = *this / b; }
  Int operator/=(Int b) { return *this = *this / b; }
  template <class T> Int operator/=(T b) { return *this /= Int(b); }

  Int operator%=(string b) { return *this = *this % b; }
  Int operator%=(Int b) { return *this = *this % b; }
  template <class T> Int operator%=(T b) { return *this %= Int(b); }

  Int operator++() { return *this += 1; }
  Int operator--() { return *this -= 1; }

#ifdef int
#undef int
#define eraseIntDefine
#endif
  Int operator++(int) {
    Int tmp = *this;
    *this += 1;
    return tmp;
  }

  Int operator--(int) {
    Int tmp = *this;
    *this -= 1;
    return tmp;
  }
#ifdef eraseIntDefine
#define int long long
#endif

  friend ostream &operator<<(ostream &os, const Int a) {
    for (int i = (int)a.val.size() - 1; i >= 0; i--)
      os << a.val[i];
    return os;
  }

  friend istream &operator>>(istream &is, Int &a) {
    string num;
    is >> num;
    a = Int(num);
    return is;
  }
  friend string to_string(Int a) {
    reverse(a.val.begin(), a.val.end());
    return a.val;
  }
};

vector<int> getPrime(int n) {
  vector<bool> used(n + 1, 0);

  for (ll i = 2; i * i <= n; i++)
    if (!used[i])
      for (int j = 2; j <= n / i; j++)
        used[i * j] = 1;

  vector<int> res;
  for (int i = 2; i <= n; i++)
    if (!used[i])
      res.push_back(i);

  return res;
}

signed main() {
  int n;
  cin >> n;

  Int num("1");
  for (int i = 2; i <= 1502; i++)
    num *= i;

  cout << num + 2 << endl;
  for (int i = 0; i < n; i++) {
    cout << 2 + i << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int, int> P;
typedef long long ll;
ostream &operator<<(ostream &o, P p) {
  return o << "(" << p.first << "," << p.second << ")";
}
istream &operator>>(istream &i, P &p) { return i >> p.first >> p.second; }
template <class T> istream &operator>>(istream &i, vector<T> &a) {
  for (auto &t : a)
    cin >> t;
  return i;
}
template <class T> T Max(T &a, T b) { return a = max(a, b); }
template <class T> T Min(T &a, T b) { return a = min(a, b); }
template <class T> void prArr(T a, string s = " ") {
  int i = 0;
  for (auto t : a)
    cout << (i++ ? s : "") << t;
  cout << endl;
}

class Int {
public:
  string val;
  Int() : val("0") {}
  Int(string num, bool reversed = 0) {
    if (!reversed)
      R(num);
    val = num;
  }
  Int(int num) {
    val = to_string(num);
    R(val);
  }

  void R(string &a) { reverse(a.begin(), a.end()); }
  bool neg(const string &a) const { return a.back() == '-'; }
  char &operator[](int i) {
    assert(i < (int)val.size());
    return val[i];
  }

  bool operator==(Int b) {
    R(b.val);
    return *this == b.val;
  }
  bool operator==(string b) {
    R(b);
    return val == b;
  }
  template <class T> bool operator==(T b) { return *this == Int(b); }

  bool operator!=(Int b) {
    R(b.val);
    return *this != b.val;
  }
  bool operator!=(string b) {
    R(b);
    return !(val == b);
  }
  template <class T> bool operator!=(T b) { return *this != Int(b); }

  bool operator>=(Int b) {
    R(b.val);
    return *this >= b.val;
  }
  bool operator>=(string b) { return !(*this < b); }
  template <class T> bool operator>=(T b) { return *this >= Int(b); }

  bool operator>(Int b) {
    R(b.val);
    return *this > b.val;
  }
  bool operator>(string b) { return *this >= b && *this != b; }
  template <class T> bool operator>(T b) { return *this > Int(b); }

  bool operator<=(Int b) {
    R(b.val);
    return *this <= b.val;
  }
  bool operator<=(string b) { return *this < b || *this == b; }
  template <class T> bool operator<=(T b) { return *this <= Int(b); }

  bool operator<(Int b) {
    R(b.val);
    return *this < b.val;
  }
  template <class T> bool operator<(T b) { return *this < Int(b); }

  bool operator<(string b) {
    R(b);
    const string &a = val;
    if (neg(a) != neg(b))
      return neg(a) ? 1 : 0;
    if (a.size() != b.size())
      return !neg(a) ? (a.size() < b.size()) : (a.size() > b.size());
    for (int i = a.size() - 1; i >= 0; i--)
      if (a[i] != b[i])
        return !neg(a) ? a[i] < b[i] : a[i] > b[i];
    return 0;
  }

  Int operator+(string b) {
    R(b);
    string a = val;
    string fa = neg(a) ? "-" : "";
    string fb = neg(b) ? "-" : "";
    if (neg(a))
      a.pop_back();
    if (neg(b))
      b.pop_back();
    if (Int(a, 1) < Int(b, 1))
      swap(a, b), swap(fa, fb);

    if (fa == fb) {
      a += '0';
      for (int i = 0; i < (int)a.size() - 1; i++) {
        if (i < (int)b.size())
          a[i] += b[i] - '0';
        a[i + 1] += (a[i] - '0') / 10;
        a[i] = (a[i] - '0') % 10 + '0';
      }
      if (a.size() > 1 && a.back() == '0')
        a.pop_back();
      if ((int)a.size() > 1 && a.back() == '0')
        a.pop_back();
      if (a != "0")
        a += fa;
    } else {
      for (int i = 0; i < (int)a.size(); i++) {
        if (i < (int)b.size())
          a[i] -= b[i] - '0';
        if (a[i] - '0' < 0)
          a[i] += 10, a[i + 1]--;
      }
      while ((int)a.size() > 1 && a.back() == '0')
        a.pop_back();
      if (a != "0")
        a += fa;
    }
    return Int(a, 1);
  }

  Int operator*(string b) {
    R(b);
    vector<int> res;
    string a = val;
    string fa = neg(a) ? "-" : "";
    string fb = neg(b) ? "-" : "";
    if (neg(a))
      a.pop_back();
    if (neg(b))
      b.pop_back();
    if (a.size() < b.size())
      swap(a, b), swap(fa, fb);
    if (a == "0" || b == "0")
      return Int("0");

    for (int j = 0; j < (int)b.size(); j++)
      for (int i = 0; i < (int)a.size(); i++) {
        if (i + j >= (int)res.size())
          res.push_back(0);
        res[i + j] += (a[i] - '0') * (b[j] - '0');
      }
    for (int i = 0; i < (int)res.size(); i++) {
      if (res[i] / 10 == 0)
        continue;
      if (i + 1 >= (int)res.size())
        res.push_back(0);
      res[i + 1] += res[i] / 10, res[i] = res[i] % 10;
    }
    string sres;
    for (int i : res)
      sres += char(i + '0');
    if (fa != fb)
      sres += '-';
    return Int(sres, 1);
  }

  Int div(string b, int flg) {
    assert(b != "0");
    string a = val;
    string fa = neg(a) ? "-" : "";
    string fb = b[0] == '-' ? "-" : "";
    if (neg(a))
      a.pop_back();
    if (b[0] == '-')
      b.erase(b.begin());
    Int mod("0");
    string d = "0";
    for (int i = (int)a.size() - 1; i >= 0; i--) {
      mod = mod * 10 + (a[i] - '0');
      while (mod >= b)
        mod -= b, d.back() += 1;
      if (i && d != "0")
        d += '0';
    }
    if (flg == 0 && d != "0" && (fa != fb))
      d = "-" + d;
    if (flg == 1 && mod != "0" && (fa == "-"))
      mod *= -1;
    return flg == 0 ? d : mod;
  }

  Int operator+(Int b) {
    R(b.val);
    return *this + b.val;
  }
  template <class T> Int operator+(T b) { return *this + Int(b); }

  Int operator*(Int b) {
    R(b.val);
    return *this * b.val;
  }
  template <class T> Int operator*(T b) { return *this * Int(b); }

  Int operator-(string b) {
    b = (b[0] == '-') ? b.substr(1) : "-" + b;
    return *this + b;
  }
  Int operator-(Int b) {
    R(b.val);
    return *this - b.val;
  }
  template <class T> Int operator-(T b) { return *this - Int(b); }

  Int operator/(string b) { return div(b, 0); }
  Int operator/(Int b) {
    R(b.val);
    return div(b.val, 0);
  }
  template <class T> Int operator/(T b) { return *this / Int(b); }

  Int operator%(string b) { return div(b, 1); }
  Int operator%(Int b) {
    R(b.val);
    return div(b.val, 1);
  }
  template <class T> Int operator%(T b) { return *this % Int(b); }

  Int operator+=(Int b) { return *this = *this + b; }
  Int operator+=(string b) { return *this = *this + b; }
  template <class T> Int operator+=(T b) { return *this += Int(b); }

  Int operator-=(Int b) { return *this = *this - b; }
  Int operator-=(string b) { return *this = *this - b; }
  template <class T> Int operator-=(T b) { return *this -= Int(b); }

  Int operator*=(Int b) { return *this = *this * b; }
  Int operator*=(string b) { return *this = *this * b; }
  template <class T> Int operator*=(T b) { return *this *= Int(b); }

  Int operator/=(string b) { return *this = *this / b; }
  Int operator/=(Int b) { return *this = *this / b; }
  template <class T> Int operator/=(T b) { return *this /= Int(b); }

  Int operator%=(string b) { return *this = *this % b; }
  Int operator%=(Int b) { return *this = *this % b; }
  template <class T> Int operator%=(T b) { return *this %= Int(b); }

  Int operator++() { return *this += 1; }
  Int operator--() { return *this -= 1; }

#ifdef int
#undef int
#define eraseIntDefine
#endif
  Int operator++(int) {
    Int tmp = *this;
    *this += 1;
    return tmp;
  }

  Int operator--(int) {
    Int tmp = *this;
    *this -= 1;
    return tmp;
  }
#ifdef eraseIntDefine
#define int long long
#endif

  friend ostream &operator<<(ostream &os, const Int a) {
    for (int i = (int)a.val.size() - 1; i >= 0; i--)
      os << a.val[i];
    return os;
  }

  friend istream &operator>>(istream &is, Int &a) {
    string num;
    is >> num;
    a = Int(num);
    return is;
  }
  friend string to_string(Int a) {
    reverse(a.val.begin(), a.val.end());
    return a.val;
  }
};

vector<int> getPrime(int n) {
  vector<bool> used(n + 1, 0);

  for (ll i = 2; i * i <= n; i++)
    if (!used[i])
      for (int j = 2; j <= n / i; j++)
        used[i * j] = 1;

  vector<int> res;
  for (int i = 2; i <= n; i++)
    if (!used[i])
      res.push_back(i);

  return res;
}

signed main() {
  int n;
  cin >> n;
  auto prime = getPrime(1e7);
  int m = prime.size();

  Int ans = 1;
  for (int i = 0; i < 1000; i++)
    ans *= 2;

  auto getDiv = [&](Int n) {
    for (int i = 0; i < m; i++)
      if (n % prime[i] == 0)
        return prime[i];
    assert(0);
  };

  cout << ans << endl;
  for (int i = 0; i < n; i++) {
    if (i % 2 == 0)
      cout << 2 << endl;
    else
      cout << getDiv(ans + i) << endl;
  }

  return 0;
}