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

#include <bits/stdc++.h>
using namespace std;
// (setq-default c-basic-offset 2)
// Multiplies str1 and str2, and prints result.

// int main() {
//   int n, k;
//   cin >> n >> k;
//   vector<int> a(n + 1);

//   for (int i = 1; i <= n; ++i) {
//     int ai;
//     cin >> ai;
//     a[i] = ai;
//   }

//   // vector<long long> grades;
//   vector<string> grades;
//   // long long s = 1;
//   string s;
//   s = "1";
//   for (int i = 1; i < k; ++i) {
//     // s *= (long long)a[i];
//     s = multiply(s, to_string(a[i]));
//   }
//   for (int i = k; i <= n; ++i) {
//     // s *= a[i];
//     s = multiply(s, to_string(a[i]));
//     grades.push_back(s);
//     // s /= a[i - k + 1];
//     s = dvd(s, a[i - k + 1]);
//   }

//   // for (int ss : grades) {
//   //   cout << ss << " ";
//   // }
//   // cout << endl;

//   for (int i = 1; i < (int)grades.size(); ++i) {
//     // if (grades[i] > grades[i - 1]) {
//     if (is_greater(grades[i], grades[i - 1])) {
//       cout << "Yes" << endl;
//     } else
//       cout << "No" << endl;
//   }
//   return 0;
// }

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  vector<double> loga(n + 1);
  for (int i = 1; i <= n; ++i) {
    int ai;
    cin >> ai;
    a[i] = ai;
    loga[i] = log(double(ai));
  }

  vector<long long> grades;
  vector<double> loggrades;
  long long s = 1;
  double logs = 0;
  for (int i = 1; i < k; ++i) {
    s *= (long long)a[i];
    logs += loga[i];
  }
  for (int i = k; i <= n; ++i) {
    s *= a[i];
    logs += loga[i];
    grades.push_back(s);
    loggrades.push_back(logs);
    s /= a[i - k + 1];
    logs -= loga[i - k + 1];
  }

  // for (int i = k + 1; i <= n; ++i) {
  //   int aa = a[i];
  //   int bb = a[i - k + 1];
  //   if (aa > bb) {
  //     cout << "Yes" << endl;
  //   } else {
  //     cout << "No" << endl;
  //   }
  // }

  // for (int ss : grades) {
  //   cout << ss << " ";
  // }
  // cout << endl;

  for (int i = 1; i < (int)grades.size(); ++i) {
    // if (grades[i] > grades[i - 1]) {
    if (loggrades[i] > loggrades[i - 1]) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// (setq-default c-basic-offset 2)
// Multiplies str1 and str2, and prints result.
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;
}

bool is_greater(string a, string b) {
  if (a.size() > b.size())
    return true;
  if (b.size() > a.size())
    return false;
  for (int i = 0; i < (int)a.size(); ++i) {
    if ((a[i] - '0') > (b[i] - '0'))
      return true;
    if ((b[i] - '0') > (a[i] - '0'))
      return false;
  }
  return false;
}
// A function to perform division of large numbers
string dvd(string number, int divisor) {
  // As result can be very large store it in string
  string ans;

  // Find prefix of number that is larger
  // than divisor.
  int idx = 0;
  int temp = number[idx] - '0';
  while (temp < divisor)
    temp = temp * 10 + (number[++idx] - '0');

  // Repeatedly divide divisor with temp. After
  // every division, update temp to include one
  // more digit.
  while (number.size() > idx) {
    // Store result in answer i.e. temp / divisor
    ans += (temp / divisor) + '0';

    // Take next digit of number
    temp = (temp % divisor) * 10 + number[++idx] - '0';
  }

  // If divisor is greater than number
  if (ans.length() == 0)
    return "0";

  // else return ans
  return ans;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);

  for (int i = 1; i <= n; ++i) {
    int ai;
    cin >> ai;
    a[i] = ai;
  }

  // vector<long long> grades;
  vector<string> grades;
  // long long s = 1;
  string s;
  s = "1";
  for (int i = 1; i < k; ++i) {
    // s *= (long long)a[i];
    s = multiply(s, to_string(a[i]));
  }
  for (int i = k; i <= n; ++i) {
    // s *= a[i];
    s = multiply(s, to_string(a[i]));
    grades.push_back(s);
    // s /= a[i - k + 1];
    s = dvd(s, a[i - k + 1]);
  }

  // for (int ss : grades) {
  //   cout << ss << " ";
  // }
  // cout << endl;

  for (int i = 1; i < (int)grades.size(); ++i) {
    // if (grades[i] > grades[i - 1]) {
    if (is_greater(grades[i], grades[i - 1])) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  }
  return 0;
}