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


#include <bits/stdc++.h>
using namespace std;
long long m = 1000000007;

#define boost                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define pb push_back
typedef long long ll;
typedef long double ld;
long long M = 1e9 + 7;
#define deb_(x) cerr << #x << " : " << x << endl;
#define deb(A, N)                                                              \\
  cerr << " :[";                                                               \\
  for (int i = 0; i < N; i++)                                                  \\
    cerr << A[i] << " ";                                                       \\
  cerr << "]\\n";
#define deb2(A, N, M)                                                          \\
  cerr << #A << " : \\n";                                                       \\
  for (int i = 0; i < N; i++) {                                                \\
    cerr << "[";                                                               \\
    for (int j = 0; j < M; ++j)                                                \\
      cerr << A[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define rep(n) for (int i = 0; i < n; i++)
#define range(a, b) for (int i = a; i <= b; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
int code();

int main() {
  boost;
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll b = code();
  }

  return 0;
}

int code() {
  ll n;
  cin >> n;
  ll temp = 1, A[n];
  rep(n) {
    ll x;
    cin >> x;
    A[i] = x;
    if (x == 0) {
      cout << "0" << endl;
      return 0;
    }
  }
  rep(n) {
    if (A[i] != 0 && temp > 1000000000000000000 / A[i]) {

      cout << "-1" << endl;
      return 0;
    }
    temp *= A[i];
  }
  cout << temp << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

#define max(a, b) ((a) > (b) ? (a) : (b))

using namespace std;

string add(string lhs, string rhs) {
  int length = max(lhs.size(), rhs.size());
  int carry = 0;
  int sum_col; // sum of two digits in the same column
  string result;

  // pad the shorter string with zeros
  while (lhs.size() < length)
    lhs.insert(0, "0");

  while (rhs.size() < length)
    rhs.insert(0, "0");

  // build result string from right to left
  for (int i = length - 1; i >= 0; i--) {
    sum_col = (lhs[i] - '0') + (rhs[i] - '0') + carry;
    carry = sum_col / 10;
    result.insert(0, to_string(sum_col % 10));
  }

  if (carry)
    result.insert(0, to_string(carry));

  // remove leading zeros
  return result.erase(0, min(result.find_first_not_of('0'), result.size() - 1));
}

string subtract(string lhs, string rhs) {
  int length = max(lhs.size(), rhs.size());
  int diff;
  string result;

  while (lhs.size() < length)
    lhs.insert(0, "0");

  while (rhs.size() < length)
    rhs.insert(0, "0");

  for (int i = length - 1; i >= 0; i--) {
    diff = (lhs[i] - '0') - (rhs[i] - '0');
    if (diff >= 0)
      result.insert(0, to_string(diff));
    else {

      // borrow from the previous column
      int j = i - 1;
      while (j >= 0) {
        lhs[j] = ((lhs[j] - '0') - 1) % 10 + '0';
        if (lhs[j] != '9')
          break;
        else
          j--;
      }
      result.insert(0, to_string(diff + 10));
    }
  }

  return result.erase(0, min(result.find_first_not_of('0'), result.size() - 1));
}

string multiply(string lhs, string rhs) {
  int length = max(lhs.size(), rhs.size());

  while (lhs.size() < length)
    lhs.insert(0, "0");

  while (rhs.size() < length)
    rhs.insert(0, "0");

  if (length == 1)
    return to_string((lhs[0] - '0') * (rhs[0] - '0'));

  string lhs0 = lhs.substr(0, length / 2);
  string lhs1 = lhs.substr(length / 2, length - length / 2);
  string rhs0 = rhs.substr(0, length / 2);
  string rhs1 = rhs.substr(length / 2, length - length / 2);

  string p0 = multiply(lhs0, rhs0);
  string p1 = multiply(lhs1, rhs1);
  string p2 = multiply(add(lhs0, lhs1), add(rhs0, rhs1));
  string p3 = subtract(p2, add(p0, p1));

  for (int i = 0; i < 2 * (length - length / 2); i++)
    p0.append("0");
  for (int i = 0; i < length - length / 2; i++)
    p3.append("0");

  string result = add(add(p0, p1), p3);

  return result.erase(0, min(result.find_first_not_of('0'), result.size() - 1));
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  cin >> s1;
  string s = s1;
  for (int i = 0; i < n - 1; i++) {
    cin >> s2;
    s = multiply(s, s2);
  }

  if (s.length() > 18) {
    int cas = 0;
    for (int i = 1; i < s.length(); i++) {
      if (s[i] != '0')
        cas = 1;
      // cout<<s[i];
    }
    if (cas == 1)
      cout << "-1" << endl;
    else
      cout << s << endl;
  } else
    cout << s << endl;
  return 0;
}