Problem p02659 (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());
  while (s[0] == '0' && s.length() >= 2) {
    s.erase(0, 1);
  }
  return s;
}

int main() {
  string a, b, z;
  cin >> a >> b;
  for (int i = 0; i < b.length(); i++) {
    if (b[i] == '.') {
      b.erase(i, 1);
      break;
    }
  }
  z.append("10000");
  z = gaocheng(z, a);
  z = gaocheng(z, b);
  if (z.length() > 6)
    for (int i = 0; i < z.length() - 6; i++) {
      printf("%c", z[i]);
    }
  else {
    printf("0");
  }
  printf("\\n");
  return 0;
}

// 99899999999999001


Error: -11

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() {
  string a, b, z;
  cin >> a >> b;
  for (int i = 0; i < b.length(); i++) {
    if (b[i] == '.') {
      b.erase(i, 1);
    }
  }
  z.append("10000");
  z = gaocheng(z, a);
  z = gaocheng(z, b);
  if (z.length() > 6)
    for (int i = 0; i < a.length() - 6; i++) {
      printf("%c", z[i]);
    }
  else {
    printf("0\\n");
  }
  printf("\\n");
  return 0;
}

// 99899999999999001