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

#include <bits/stdc++.h>
#define ll long long
#define inf 1000000007

using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

string gaojia(string a, string b, int p) {
  string s;
  char si[10005];
  int mi = min(a.length(), b.length() + p),
      ma = max(a.length(), b.length() + p);
  while (a.length() < ma) {
    a.append("0");
  }
  while (b.length() + p < ma) {
    b.append("0");
  }
  int ji = 0;
  for (int i = 0; i < p; i++) {
    si[i] = a[i];
  }
  for (int i = p; i < ma; i++) {
    int k = ji + a[i] + b[i - p] - 48 - 48;
    si[i] = k % 10 + 48;
    ji = k / 10;
  }
  int ki = ma;
  while (ji) {
    si[ki++] = ji % 10 + 48;
    ji = ji / 10;
  }
  si[ki++] = '\\0';
  s = si;
  return s;
}

string cheng(string a, ll z) {
  string s;
  char si[10005];
  ll ji = 0;
  for (int i = 0; i < a.length(); i++) {
    ll k = ji + (a[i] - 48) * z;
    si[i] = k % 10 + 48;
    ji = k / 10;
  }
  int l = a.length();
  while (ji) {
    si[l++] = ji % 10 + 48;
    ji = ji / 10;
  }
  si[l++] = '\\0';
  s = si;
  return s;
}

string gaocheng(string a, string b) {
  string s;
  s.append("0");
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  int ji = 0;
  if (a.length() < b.length()) {
    ll z = 0, d = 1;
    for (int i = 0; i < b.length(); i++) {
      if (i % 10 == 0) {
        z = b[i] - 48;
        d = 10;
      } else if (i % 10 == 9) {
        z = z + (b[i] - 48) * d;
        string k = cheng(a, z);
        s = gaojia(s, k, ji);
        ji = i + 1;
      } else {
        z = z + (b[i] - 48) * d;
        d = d * 10;
      }
      if (i % 10 != 9 && i == b.length() - 1) {
        string k = cheng(a, z);
        s = gaojia(s, k, ji);
      }
    }
  } else {
    ll z = 0, d = 1;
    for (int i = 0; i < a.length(); i++) {
      if (i % 10 == 0) {
        z = a[i] - 48;
        d = 10;
      } else if (i % 10 == 9) {
        z = z + (a[i] - 48) * d;
        string k = cheng(b, z);
        s = gaojia(s, k, ji);
        ji = i + 1;
      } else {
        z = z + (a[i] - 48) * d;
        d = d * 10;
      }
      if (i % 10 != 9 && i == a.length() - 1) {
        string k = cheng(b, z);
        s = gaojia(s, k, ji);
      }
    }
  }
  reverse(s.begin(), s.end());
  return s;
}

string t[100005];

int main() {
  int n;
  scanf("%d", &n);
  string s;
  s.append("1");
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    s = gaocheng(s, t[i]);
    if (s.length() >= 19 && s != "1000000000000000000") {
      s = "-1";
      for (i = 0; i < n; i++) {
        if (t[i] == "0") {
          s = "0";
        }
      }
      break;
    }
  }
  cout << s << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define inf 1000000007

using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

string gaojia(string a, string b, int p) {
  string s;
  char si[10005];
  int mi = min(a.length(), b.length() + p),
      ma = max(a.length(), b.length() + p);
  while (a.length() < ma) {
    a.append("0");
  }
  while (b.length() + p < ma) {
    b.append("0");
  }
  int ji = 0;
  for (int i = 0; i < p; i++) {
    si[i] = a[i];
  }
  for (int i = p; i < ma; i++) {
    int k = ji + a[i] + b[i - p] - 48 - 48;
    si[i] = k % 10 + 48;
    ji = k / 10;
  }
  int ki = ma;
  while (ji) {
    si[ki++] = ji % 10 + 48;
    ji = ji / 10;
  }
  si[ki++] = '\\0';
  s = si;
  return s;
}

string cheng(string a, ll z) {
  string s;
  char si[10005];
  ll ji = 0;
  for (int i = 0; i < a.length(); i++) {
    ll k = ji + (a[i] - 48) * z;
    si[i] = k % 10 + 48;
    ji = k / 10;
  }
  int l = a.length();
  while (ji) {
    si[l++] = ji % 10 + 48;
    ji = ji / 10;
  }
  si[l++] = '\\0';
  s = si;
  return s;
}

string gaocheng(string a, string b) {
  string s;
  s.append("0");
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  int ji = 0;
  if (a.length() < b.length()) {
    ll z = 0, d = 1;
    for (int i = 0; i < b.length(); i++) {
      if (i % 10 == 0) {
        z = b[i] - 48;
        d = 10;
      } else if (i % 10 == 9) {
        z = z + (b[i] - 48) * d;
        string k = cheng(a, z);
        s = gaojia(s, k, ji);
        ji = i + 1;
      } else {
        z = z + (b[i] - 48) * d;
        d = d * 10;
      }
      if (i % 10 != 9 && i == b.length() - 1) {
        string k = cheng(a, z);
        s = gaojia(s, k, ji);
      }
    }
  } else {
    ll z = 0, d = 1;
    for (int i = 0; i < a.length(); i++) {
      if (i % 10 == 0) {
        z = a[i] - 48;
        d = 10;
      } else if (i % 10 == 9) {
        z = z + (a[i] - 48) * d;
        string k = cheng(b, z);
        s = gaojia(s, k, ji);
        ji = i + 1;
      } else {
        z = z + (a[i] - 48) * d;
        d = d * 10;
      }
      if (i % 10 != 9 && i == a.length() - 1) {
        string k = cheng(b, z);
        s = gaojia(s, k, ji);
      }
    }
  }
  reverse(s.begin(), s.end());
  return s;
}

string t[100005];

int main() {
  int n;
  scanf("%d", &n);
  string s;
  s.append("1");
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < n; i++) {
    s = gaocheng(s, t[i]);
    if (s > "1000000000000000000") {
      s = "-1";
    }
  }
  cout << s << endl;
  return 0;
}