Problem p00993 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

string sub1(string a) {
  assert(a != "0");

  int as = a.size();
  int len = as + 5;
  vector<int> A(len, 0);
  for (int i = 0; i < as; i++)
    A[i] = a[as - 1 - i] - '0';

  A[0]--;
  for (int i = 0; i + 1 < len; i++) {
    if (A[i] < 0) {
      A[i + 1]--;
      A[i] += 10;
    }
    A[i + 1] += A[i] / 10;
    A[i] %= 10;
  }

  string res = "";
  for (int i = len - 1; i >= 0; i--) {
    if (res.size() == 0 && A[i] == 0)
      continue;
    res.push_back('0' + A[i]);
  }
  return res;
}

string add1(string a) {
  int as = a.size();
  int len = as + 5;
  vector<int> A(len, 0);
  for (int i = 0; i < as; i++)
    A[i] = a[as - 1 - i] - '0';

  A[0]++;
  for (int i = 0; i + 1 < len; i++) {
    A[i + 1] += A[i] / 10;
    A[i] %= 10;
  }
  string res = "";
  for (int i = len - 1; i >= 0; i--) {
    if (res.size() == 0 && A[i] == 0)
      continue;
    res.push_back('0' + A[i]);
  }
  return res;
}

string mul(string a, string b) {
  if (a == "0" || b == "0")
    return "0";
  int as = a.size(), bs = b.size();
  int len = as + bs;
  vector<int> A(as), B(bs), C(len);
  for (int i = 0; i < as; i++)
    A[i] = a[as - 1 - i] - '0';
  for (int i = 0; i < bs; i++)
    B[i] = b[bs - 1 - i] - '0';

  for (int i = 0; i < as; i++)
    for (int j = 0; j < bs; j++)
      C[i + j] += A[i] * B[j];

  for (int i = 0; i + 1 < len; i++) {
    C[i + 1] += C[i] / 10;
    C[i] %= 10;
  }

  string res = "";
  for (int i = len - 1; i >= 0; i--) {
    if (res.size() == 0 && C[i] == 0)
      continue;
    res.push_back('0' + C[i]);
  }
  return res;
}

string i2s(int x) {
  stringstream ss;
  ss << x;
  string res;
  ss >> res;
  return res;
}

const int MAX = 10000000;
bool isP[MAX];
int isQ[MAX];

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

  if (n == 1) {
    cout << 100 << endl;
    cout << 2 << endl;
    return 0;
  }

  for (int i = 2; i < MAX; i++) {
    isP[i] = true;
    isQ[i] = i;
  }

  for (int i = 2; i * i < MAX; i++) {
    if (!isP[i])
      continue;
    for (int j = i * i; j < MAX; j += i) {
      isP[j] = false;
      isQ[j] = i;
    }
  }

  vector<int> primes;

  for (int i = 2; i <= n; i++) {
    if (isP[i]) {
      primes.push_back(i);
    }
  }

  string base = "100";
  for (int i = 0; i < (int)primes.size(); i++) {
    string str = i2s(primes[i]);
    base = mul(base, str);
  }

  string X = add1(base);
  string Y = sub1(base);

  base = mul(X, Y);
  base = add1(base);

  vector<string> ans;
  ans.push_back("2");
  base = sub1(base);
  ans.push_back(X);
  for (int i = 0; i < n - 2; i++) {
    base = sub1(base);
    ans.push_back(i2s(isQ[i + 2]));
  }
  cout << base << endl;
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

string sub1(string a) {
  assert(a != "0");

  int as = a.size();
  int len = as + 5;
  vector<int> A(len, 0);
  for (int i = 0; i < as; i++)
    A[i] = a[as - 1 - i] - '0';

  A[0]--;
  for (int i = 0; i + 1 < len; i++) {
    if (A[i] < 0) {
      A[i + 1]--;
      A[i] += 10;
    }
    A[i + 1] += A[i] / 10;
    A[i] %= 10;
  }

  string res = "";
  for (int i = len - 1; i >= 0; i--) {
    if (res.size() == 0 && A[i] == 0)
      continue;
    res.push_back('0' + A[i]);
  }
  return res;
}

string add1(string a) {
  int as = a.size();
  int len = as + 5;
  vector<int> A(len, 0);
  for (int i = 0; i < as; i++)
    A[i] = a[as - 1 - i] - '0';

  A[0]++;
  for (int i = 0; i + 1 < len; i++) {
    A[i + 1] += A[i] / 10;
    A[i] %= 10;
  }
  string res = "";
  for (int i = len - 1; i >= 0; i--) {
    if (res.size() == 0 && A[i] == 0)
      continue;
    res.push_back('0' + A[i]);
  }
  return res;
}

string mul(string a, string b) {
  if (a == "0" || b == "0")
    return "0";
  int as = a.size(), bs = b.size();
  int len = as + bs;
  vector<int> A(as), B(bs), C(len);
  for (int i = 0; i < as; i++)
    A[i] = a[as - 1 - i] - '0';
  for (int i = 0; i < bs; i++)
    B[i] = b[bs - 1 - i] - '0';

  for (int i = 0; i < as; i++)
    for (int j = 0; j < bs; j++)
      C[i + j] += A[i] * B[j];

  for (int i = 0; i + 1 < len; i++) {
    C[i + 1] += C[i] / 10;
    C[i] %= 10;
  }

  string res = "";
  for (int i = len - 1; i >= 0; i--) {
    if (res.size() == 0 && C[i] == 0)
      continue;
    res.push_back('0' + C[i]);
  }
  return res;
}

string i2s(int x) {
  stringstream ss;
  ss << x;
  string res;
  ss >> res;
  return res;
}

const int MAX = 10000000;
bool isP[MAX];
int isQ[MAX];

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

  for (int i = 2; i < MAX; i++) {
    isP[i] = true;
    isQ[i] = i;
  }

  for (int i = 2; i * i < MAX; i++) {
    if (!isP[i])
      continue;
    for (int j = i * i; j < MAX; j += i) {
      isP[j] = false;
      isQ[j] = i;
    }
  }

  vector<int> primes;

  for (int i = 2; i <= n; i++) {
    if (isP[i]) {
      primes.push_back(i);
    }
  }

  string base = "100";
  for (int i = 0; i < (int)primes.size(); i++) {
    string str = i2s(primes[i]);
    base = mul(base, str);
  }

  string X = add1(base);
  string Y = sub1(base);

  base = mul(X, Y);
  base = add1(base);

  vector<string> ans;
  ans.push_back("2");
  base = sub1(base);
  ans.push_back(X);
  for (int i = 0; i < n - 2; i++) {
    base = sub1(base);
    ans.push_back(i2s(isQ[i + 2]));
  }
  cout << base << endl;
  reverse(ans.begin(), ans.end());
  for (string s : ans)
    cout << s << endl;
  return 0;
}