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

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<LL> Vl;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vl> VVl;
typedef pair<LL, LL> Pll;
typedef pair<DD, DD> Pdd;
typedef vector<Pll> Vpl;
typedef vector<Pdd> Vpd;
typedef queue<LL> Ql;
typedef stack<LL> Sl;
typedef deque<LL> Dl;

LL _set(LL N, LL pos) { return N = N | (1 << pos); }
LL _reset(LL N, LL pos) { return N = N & ~(1 << pos); }
bool _check(LL N, LL pos) { return (bool)(N & (1 << pos)); }
bool _upper(char a) { return a >= 'A' && a <= 'Z'; }
bool _lower(char a) { return a >= 'a' && a <= 'z'; }
bool _digit(char a) { return a >= '0' && a <= '9'; }

LL dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
LL dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

string multiply(string num1, string num2) {
  int len1 = num1.size();
  int len2 = num2.size();
  if (len1 == 0 || len2 == 0)
    return "0";

  // will keep the result number in vector
  // in reverse order
  vector<int> result(len1 + len2, 0);

  // Below two indexes are used to find positions
  // in result.
  int i_n1 = 0;
  int i_n2 = 0;

  // Go from right to left in num1
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';

    // To shift position to left after every
    // multiplication of a digit in num2
    i_n2 = 0;

    // Go from right to left in num2
    for (int j = len2 - 1; j >= 0; j--) {
      // Take current digit of second number
      int n2 = num2[j] - '0';

      // Multiply with current digit of first number
      // and add result to previously stored result
      // at current position.
      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;

      // Carry for next iteration
      carry = sum / 10;

      // Store result
      result[i_n1 + i_n2] = sum % 10;

      i_n2++;
    }

    // store carry in next cell
    if (carry > 0)
      result[i_n1 + i_n2] += carry;

    // To shift position to left after every
    // multiplication of a digit in num1.
    i_n1++;
  }

  // ignore '0's from the right
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0)
    i--;

  // If all were '0's - means either both or
  // one of num1 or num2 were '0'
  if (i == -1)
    return "0";

  // generate the result string
  string s = "";

  while (i >= 0)
    s += std::to_string(result[i--]);

  return s;
}

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

  string s = "1", t = "1000000000000000000", x[mx5];
  for (int i = 0; i < n; i++) {
    cin >> x[i];

    if (x[i] == "0")
      return cout << 0 << '\\n', 0;
  }

  for (int i = 0; i < n; i++) {
    s = multiply(s, x[i]);
    if (s.size() > 19)
      return cout << -1 << '\\n', 0;
  }

  if (s == t)
    cout << s << '\\n';
  else if (s.size() > 18)
    cout << -1 << '\\n';
  else
    cout << s << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<LL> Vl;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vl> VVl;
typedef pair<LL, LL> Pll;
typedef pair<DD, DD> Pdd;
typedef vector<Pll> Vpl;
typedef vector<Pdd> Vpd;
typedef queue<LL> Ql;
typedef stack<LL> Sl;
typedef deque<LL> Dl;

LL _set(LL N, LL pos) { return N = N | (1 << pos); }
LL _reset(LL N, LL pos) { return N = N & ~(1 << pos); }
bool _check(LL N, LL pos) { return (bool)(N & (1 << pos)); }
bool _upper(char a) { return a >= 'A' && a <= 'Z'; }
bool _lower(char a) { return a >= 'a' && a <= 'z'; }
bool _digit(char a) { return a >= '0' && a <= '9'; }

LL dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
LL dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

string multiply(string num1, string num2) {
  int len1 = num1.size();
  int len2 = num2.size();
  if (len1 == 0 || len2 == 0)
    return "0";

  // will keep the result number in vector
  // in reverse order
  vector<int> result(len1 + len2, 0);

  // Below two indexes are used to find positions
  // in result.
  int i_n1 = 0;
  int i_n2 = 0;

  // Go from right to left in num1
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';

    // To shift position to left after every
    // multiplication of a digit in num2
    i_n2 = 0;

    // Go from right to left in num2
    for (int j = len2 - 1; j >= 0; j--) {
      // Take current digit of second number
      int n2 = num2[j] - '0';

      // Multiply with current digit of first number
      // and add result to previously stored result
      // at current position.
      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;

      // Carry for next iteration
      carry = sum / 10;

      // Store result
      result[i_n1 + i_n2] = sum % 10;

      i_n2++;
    }

    // store carry in next cell
    if (carry > 0)
      result[i_n1 + i_n2] += carry;

    // To shift position to left after every
    // multiplication of a digit in num1.
    i_n1++;
  }

  // ignore '0's from the right
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0)
    i--;

  // If all were '0's - means either both or
  // one of num1 or num2 were '0'
  if (i == -1)
    return "0";

  // generate the result string
  string s = "";

  while (i >= 0)
    s += std::to_string(result[i--]);

  return s;
}

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

  string s = "1", t = "1000000000000000000";
  for (int i = 0; i < n; i++) {
    string x;
    cin >> x;

    s = multiply(s, x);
  }

  if (s == t)
    cout << s << '\\n';
  else if (s.size() > 18)
    cout << -1 << '\\n';
  else
    cout << s << '\\n';
  return 0;
}