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


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \\
  int i;                                                                       \\
  cin >> i
#define readll(i)                                                              \\
  ll i;                                                                        \\
  cin >> i
#define readdouble(i)                                                          \\
  double i;                                                                    \\
  cin >> i
#define readstring(s)                                                          \\
  string s;                                                                    \\
  cin >> s

typedef long long ll;

using namespace std;

ll modd = 1000 * 1000 * 1000 + 7;

int main() {

  ios_base::sync_with_stdio(false);

  cout.precision(17);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readll(n);
    readll(k);
    vector<ll> a_pos, a_neg;
    forr(i, 0, n) {
      readll(kk);
      if (kk > 0) {
        a_pos.push_back(kk);
      }
      if (kk < 0) {
        a_neg.push_back(kk);
      }
    }
    sort(a_pos.begin(), a_pos.end());
    sort(a_neg.begin(), a_neg.end());

    if (a_pos.size() + a_neg.size() < k) {
      cout << 0 << endl;
      continue;
    }
    ll ret_tot = 1;
    for (ll x : a_pos) {
      ret_tot *= (modd + x);
      ret_tot = ret_tot % modd;
    }
    for (ll x : a_neg) {
      ret_tot *= (modd + x);
      ret_tot = ret_tot % modd;
    }
    if (k == n) {
      cout << ret_tot << endl;
      continue;
    }
    if (a_pos.size() + a_neg.size() == k) {
      if (a_neg.size() % 2 == 1) {
        cout << 0 << endl;
      } else {
        cout << ret_tot << endl;
      }
      continue;
    }

    if (a_pos.empty()) {
      if (k % 2 == 1) {
        if (a_neg.size() == n) {
          ll ret = 1;
          forr(i, 0, k) {
            ret *= (modd + a_neg[(int)a_neg.size() - 1 - i]);
            ret = ret % modd;
          }
          cout << ret << endl;
          continue;
        } else {
          cout << 0 << endl;
          continue;
        }
      } else {
        ll ret = 1;
        forr(i, 0, k) {
          ret *= (modd + a_neg[i]);
          ret = ret % modd;
        }
        cout << ret << endl;
        continue;
      }
    } else {
      int x_from = -1, x_to = x_from;
      for (int x = 0; x <= n / 2; x++) {
        if ((2 * x <= a_neg.size()) && (k - (int)a_pos.size() <= 2 * x) &&
            (k - 2 * x >= 0)) {
          if (x_from == -1) {
            x_from = x;
          }
          x_to = x;
        }
      }
      int x_opt;
      int x_pays_to_enlarge = -1;
      for (int x = x_from; x < x_to; ++x) {
        if (a_neg[2 * x] * a_neg[2 * x + 1] >=
            a_pos[(int)a_pos.size() - (k - 2 * x)] *
                a_pos[(int)a_pos.size() + 1 - (k - 2 * x)]) {
          x_pays_to_enlarge = x;
        }
      }
      if (x_pays_to_enlarge == -1) {
        x_opt = x_from;
      } else {
        x_opt = x_pays_to_enlarge + 1;
      }
      ll ret = 1;
      forr(i, 0, 2 * x_opt) {
        ret *= (modd + a_neg[i]);
        ret = ret % modd;
      }
      forr(i, 0, k - 2 * x_opt) {
        ret *= a_pos[(int)a_pos.size() - 1 - i];
        ret = ret % modd;
      }
      cout << ret << endl;
    }
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}


Error: 0

Fixed submission:


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \\
  int i;                                                                       \\
  cin >> i
#define readll(i)                                                              \\
  ll i;                                                                        \\
  cin >> i
#define readdouble(i)                                                          \\
  double i;                                                                    \\
  cin >> i
#define readstring(s)                                                          \\
  string s;                                                                    \\
  cin >> s

typedef long long ll;

using namespace std;

ll modd = 1000 * 1000 * 1000 + 7;

int main() {

  ios_base::sync_with_stdio(false);

  cout.precision(17);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readll(n);
    readll(k);
    vector<ll> a_pos, a_neg;
    forr(i, 0, n) {
      readll(kk);
      if (kk > 0) {
        a_pos.push_back(kk);
      }
      if (kk < 0) {
        a_neg.push_back(kk);
      }
    }
    sort(a_pos.begin(), a_pos.end());
    sort(a_neg.begin(), a_neg.end());

    if (a_pos.size() + a_neg.size() < k) {
      cout << 0 << endl;
      continue;
    }
    ll ret_tot = 1;
    for (ll x : a_pos) {
      ret_tot *= (modd + x);
      ret_tot = ret_tot % modd;
    }
    for (ll x : a_neg) {
      ret_tot *= (modd + x);
      ret_tot = ret_tot % modd;
    }
    if (k == n) {
      cout << ret_tot << endl;
      continue;
    }
    if (a_pos.size() + a_neg.size() == k) {
      if (a_neg.size() % 2 == 1) {
        cout << 0 << endl;
      } else {
        cout << ret_tot << endl;
      }
      continue;
    }

    if (a_pos.empty()) {
      if (k % 2 == 1) {
        if (a_neg.size() == n) {
          ll ret = 1;
          forr(i, 0, k) {
            ret *= (modd + a_neg[(int)a_neg.size() - 1 - i]);
            ret = ret % modd;
          }
          cout << ret << endl;
          continue;
        } else {
          cout << 0 << endl;
          continue;
        }
      } else {
        ll ret = 1;
        forr(i, 0, k) {
          ret *= (modd + a_neg[i]);
          ret = ret % modd;
        }
        cout << ret << endl;
        continue;
      }
    } else {
      int x_from = -1, x_to = x_from;
      for (int x = 0; x <= n / 2; x++) {
        if ((2 * x <= a_neg.size()) && (k - (int)a_pos.size() <= 2 * x)) {
          if (x_from == -1) {
            x_from = x;
          }
          x_to = x;
        }
      }
      int x_opt;
      if (x_from == x_to) {
        x_opt = x_from;
      } else {
        int x_pays_to_enlarge = -1;
        for (int x = x_from; x < x_to; ++x) {
          if (a_neg[2 * x] * a_neg[2 * x + 1] >=
              a_pos[(int)a_pos.size() - (k - 2 * x)] *
                  a_pos[(int)a_pos.size() + 1 - (k - 2 * x)]) {
            x_pays_to_enlarge = x_from;
          }
        }
        if (x_pays_to_enlarge == -1) {
          x_opt = x_from;
        } else {
          x_opt = x_pays_to_enlarge + 1;
        }
      }
      ll ret = 1;
      forr(i, 0, 2 * x_opt) {
        ret *= (modd + a_neg[i]);
        ret = ret % modd;
      }
      forr(i, 0, k - 2 * x_opt) {
        ret *= a_pos[(int)a_pos.size() - 1 - i];
        ret = ret % modd;
      }
      cout << ret << endl;
    }
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}