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

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    cout
        << "7222808692966442065110652130465651052534046869049593326004414767685"
           "7493358690644482529638108395307511654564478438634721376272188778051"
           "8675716774943039713904788735721782600660681500173560787826612412389"
           "8753375728085290517221168083329425314502494398365621869529675941373"
           "3574883908589221245240568177781436209401676317319383038846775665336"
           "8269011296318958227342685204756430480451276546242699331709070510103"
           "0071764331392186585655992423206315114436531017362320594114458155060"
           "0924166393303087536601524168192597964476245223257921963650541646329"
           "2785171437589234831733477231066011435378386421782781738290234449560"
           "8950331117307361844575353287581386759866329685673931568490360969714"
           "4796703000651178486736265301118699204591404539960193266425232481989"
           "1897560614315252229685969642499330887189097431929763954664792251387"
           "9487596126405816099954834020879911430322628858037923084140730975224"
           "8092079394823964414350647207773176541876297915527225415606053332774"
           "8142574963637980328733352092449761524605124613032816643509403622008"
           "5536527924788839498861868913749022818930820388602819365904816028740"
           "0841160648403948450468224084289104098406210815195475652974251962169"
           "0230427081126385058098616774317419876443061748528811039085901632785"
           "6340587807712490977905456118231100633349430715640825101879940889224"
           "3760644696289136985692091001832810603200225192529800598541540207945"
           "7222952905032330513710762466178012464776704918983862468132022524082"
           "3869605580183547838920734331470457538409601059105514602356128088840"
           "7508720627001204634924305070472606572970589678020718487701632305423"
           "4342353493445220255072123537289602182652562111606663813829431177558"
           "9251760483247589383816059586521579216100314651648840327044951939301"
           "5135593822826378540820967201713148164147547345002056230263528047074"
           "6222256387481409919867026083988653990497446014271578866798688724011"
           "0295040728994828373358374063168824883360490658764218548833900441482"
           "6636286040142084791683914628657165348291172073808784695309880256246"
           "1342884917449251296550252654193672967068581625490728082819081041962"
           "8050795440518407592257975937436425990056196672414053798624352654419"
           "1866882749547587269821552286730063949533728318429778480319961104918"
           "8447310059042819566018755399222186590103777996086005222481049299159"
           "8030731920853529418443911744522602363875355474532900544934121936320"
           "2337799785750760984531925797938724780584079004514261157265167710941"
           "3777894825208478360037029789990530674193300300509266570096676125035"
           "0998349970944074389755253054566192275621736047571771455971872505712"
           "5088075068494238743382339920393013025095894842804425739450336201397"
           "7538757850394327141114700790336005892803275552335277018468040081459"
           "3092567704919384433267791620246785329524415050570586248955112767107"
           "7913008968771802260142300348484357305227827114586911391477996693157"
           "0741542380736495529028094778718147889827679001277078657757853534218"
           "1639657461633921049936971543282484652331354541225826247245761311446"
           "2278858700390438045370815099558667681904548529563967959887782439051"
           "3815150930510838399710931849246995333248838645336280682554747685305"
           "6331806253568665110990288165843553019861578111424993822962693031586"
           "9443959252881511510806892499353192984181283162193449665304948147353"
           "5944123894855538696984547876785424000680475272147128962159083169487"
           "3831941325481803196419026421384662457481596239751174736082669137340"
           "4559244514825698683380176427007758604913736911634120333325940804474"
           "2157428047473456006598767249756488423131226447290828086691367822822"
           "9938305741597084902951688569484124860020258139098177510720778053820"
           "2966696283718312699336922215896101871392802295957267843867101490929"
           "5639279941518947884541926115642250716469250013902066471388808973404"
           "7285629101611163013120112718521800052722424522937410659806748323033"
           "1626123791916818249484120815023729026712298569622439434649600000000"
           "0000000000000000000000000000000000000000000000000000000000000000000"
           "0000000000000000000000000000000000000000000000000000000000000000000"
           "0000000000000000000000000000000000000000000000000000000000000000000"
           "0000000000000000000000000000000000000000000000000000000000000000000"
           "0000000000000000000000000000000000000000000000000000000000000000000"
           "0000000000000000000000000000002"
        << endl;
    for (int i = 0; i < n; i++) {
      cout << i + 2 << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define all(c) c.begin(), c.end()
#define uni(c) c.erase(unique(c.begin(), c.end()), c.end())
#define pb push_back
using namespace std;
string sum(string a, string b) {
  string s;
  int n = max(a.size(), b.size());
  while (a.size() != n) {
    a = "0" + a;
  }
  while (b.size() != n) {
    b = "0" + b;
  }
  reverse(all(a));
  reverse(all(b));
  int p = 0;
  for (int i = 0; i < n; i++) {
    int x = (a[i] - '0') + (b[i] - '0') + p;
    p = 0;
    if (x >= 10)
      p = 1;
    s = (char)('0' + x % 10) + s;
  }
  if (p) {
    s = '1' + s;
  }
  return s;
}
string mult(string a, string b) {
  string s = "0";
  reverse(all(a));
  reverse(all(b));
  string pz = "";
  for (int i = 0; i < b.size(); i++) {
    int p = 0;
    string t = "";
    for (int j = 0; j < a.size(); j++) {
      int x = (a[j] - '0') * (b[i] - '0') + p;
      p = x / 10;
      t = (char)(x % 10 + '0') + t;
    }
    if (p) {
      t = (char)(p % 10 + '0') + t;
    }
    s = sum(s, t + pz);
    pz += "0";
  }

  return s;
}
string tost(int x) {
  string s = "";
  while (x) {
    s = (char)(x % 10 + '0') + s;
    x /= 10;
  }
  return s;
}
int main() {
  int n;
  while (cin >> n) {
    string mul = "1";
    for (int i = 0; i < n + 1; i++) {
      mul = mult(mul, tost(i + 1));
    }
    cout << sum(mul, tost(2)) << endl;
    for (int i = 0; i < n; i++) {
      cout << i + 2 << endl;
    }
  }
}