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

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

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

/* Macros reg. ends here */

const ll INF = 1LL << 50;
static const long long mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x(x % mod) {}
  mint &operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    (x += mod - a.x) %= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint ret(*this);
    return ret += a;
  }
  mint operator-(const mint a) const {
    mint ret(*this);
    return ret -= a;
  }
  mint operator*(const mint a) const {
    mint ret(*this);
    return ret *= a;
  }
  mint pow(ll t) const {
    if (t == 0)
      return mint(1);
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint ret(*this);
    return ret /= a;
  }
};

int main() {
  cout << fixed << setprecision(15);

  vll ps, ns;
  ll n, k;
  cin >> n >> k;

  rep(i, n) {
    ll a;
    cin >> a;
    if (a < 0)
      ns.push_back(-a);
    else
      ps.push_back(a);
  }
  rSort(ns), rSort(ps);
  ll np = ps.size(), nn = ns.size();
  mint ans = 1;

  if (n == k) {
    rep(i, np) ans *= ps[i];
    rep(i, nn) ans *= mod - ns[i];
    cout << ans.x << endl;
    return 0;
  }

  bool minus = np == 0 && (k % 2) == 1;
  if (minus) {
    rep(i, k) { ans *= mod - ns[nn - 1 - i]; }
    cout << ans.x << endl;
    return 0;
  }

  ll kn = min((k / 2) * 2, (nn / 2) * 2);
  rep(i, kn) { ans *= ns[i]; }
  ll kp = k - kn;
  rep(i, kp) { ans *= ps[i]; }

  while (true) {
    kn -= 2;
    kp += 2;
    bool br = kn < 0 || kp > np;
    if (br)
      break;
    bool change = ps[kp - 1] * ps[kp - 2] > ns[kn + 1] * ns[kn];
    if (change) {
      ans /= ns[kn + 1] * ns[kn];
      ans *= ps[kp - 1] * ps[kp - 2];
    } else {
      break;
    }
  }

  cout << ans.x << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

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

/* Macros reg. ends here */

const ll INF = 1LL << 50;
static const long long mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x(x % mod) {}
  mint &operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    (x += mod - a.x) %= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint ret(*this);
    return ret += a;
  }
  mint operator-(const mint a) const {
    mint ret(*this);
    return ret -= a;
  }
  mint operator*(const mint a) const {
    mint ret(*this);
    return ret *= a;
  }
  mint pow(ll t) const {
    if (t == 0)
      return mint(1);
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint ret(*this);
    return ret /= a;
  }
};

int main() {
  cout << fixed << setprecision(15);

  vll ps, ns;
  ll n, k;
  cin >> n >> k;

  rep(i, n) {
    ll a;
    cin >> a;
    if (a < 0)
      ns.push_back(-a);
    else
      ps.push_back(a);
  }
  rSort(ns), rSort(ps);
  ll np = ps.size(), nn = ns.size();
  mint ans = 1;

  if (n == k) {
    rep(i, np) ans *= ps[i];
    rep(i, nn) ans *= mod - ns[i];
    cout << ans.x << endl;
    return 0;
  }

  bool minus = np == 0 && (k % 2) == 1;
  if (minus) {
    rep(i, k) { ans *= mod - ns[nn - 1 - i]; }
    cout << ans.x << endl;
    return 0;
  }

  ll kn = (k / 2) * 2;
  rep(i, kn) { ans *= ns[i]; }
  ll kp = k - kn;
  rep(i, kp) { ans *= ps[i]; }

  while (true) {
    kn -= 2;
    kp += 2;
    bool br = kn < 0 || kp > np;
    if (br)
      break;
    bool change = ps[kp - 1] * ps[kp - 2] > ns[kn + 1] * ns[kn];
    if (change) {
      ans /= ns[kn + 1] * ns[kn];
      ans *= ps[kp - 1] * ps[kp - 2];
    } else {
      break;
    }
  }

  cout << ans.x << endl;

  return 0;
}