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


#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 200005;
const int MAXV = 3;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
int arr[MAXN];
void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    extgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}

inline ll getInv(ll x, ll mod) {
  ll g, a, b;
  extgcd(x, mod, g, a, b);
  return (a % mod + mod) % mod;
}

int fastExp(ll x, ll p, int mod) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return ans;
}

LD prefN[MAXN], prefP[MAXN];
ll prefN1[MAXN], prefP1[MAXN];
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%d%d", &n, &k);
  vector<int> pos, neg, zr;
  for (int i = 0; i < n; i++) {
    arr[i] = read<int>();
    if (arr[i] < 0)
      neg.push_back(arr[i]);
    if (arr[i] > 0)
      pos.push_back(arr[i]);
    if (arr[i] == 0)
      zr.push_back(arr[i]);
  }
  if (k == n) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ans = (ans * arr[i] % MOD + MOD) % MOD;
    }
    printf("%lld\\n", ans);
    return 0;
  }
  auto cmp = [](int a, int b) { return abs(a) > abs(b); };
  sort(pos.begin(), pos.end(), cmp);
  sort(neg.begin(), neg.end(), cmp);
  if (pos.size() == 0 && (k & 1)) {
    if (zr.size() > 0)
      printf("0\\n");
    else {
      ll c = 1;
      for (int i = neg.size() - 1; i >= max(0, (int)neg.size() - k); i--) {
        c = (c * neg[i] % MOD + MOD) % MOD;
      }
      printf("%lld\\n", c);
    }
    return 0;
  }
  prefP1[0] = 1, prefN1[0] = 1;
  for (int i = 1; i <= pos.size(); i++) {
    prefP[i] = prefP[i - 1] + std::log((LD)pos[i - 1]);
    prefP1[i] = prefP1[i - 1] * pos[i - 1] % MOD;
  }
  for (int i = 1; i <= neg.size(); i++) {
    prefN[i] = prefN[i - 1] + std::log((LD)abs(neg[i - 1]));
    prefN1[i] = ((prefN1[i - 1] * neg[i - 1] % MOD) + MOD) % MOD;
  }
  LD cur = -1.0 / 0.0;
  ll ans = 0;
  int sz = pos.size();
  for (int i = 0; i <= neg.size(); i += 2) {
    int req = k - i;
    if (req < 0)
      break;
    if (sz < req)
      continue;
    if (prefN[i] + prefP[req] >= cur) {
      cur = prefN[i] + prefP[req];
      ans = prefN1[i] * prefP1[req] % MOD;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 25;
const int MAXV = 3;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
int arr[MAXN];
void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    extgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}

inline ll getInv(ll x, ll mod) {
  ll g, a, b;
  extgcd(x, mod, g, a, b);
  return (a % mod + mod) % mod;
}

int fastExp(ll x, ll p, int mod) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return ans;
}

LD prefN[MAXN], prefP[MAXN];
ll prefN1[MAXN], prefP1[MAXN];
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%d%d", &n, &k);
  vector<int> pos, neg, zr;
  for (int i = 0; i < n; i++) {
    arr[i] = read<int>();
    if (arr[i] < 0)
      neg.push_back(arr[i]);
    if (arr[i] > 0)
      pos.push_back(arr[i]);
    if (arr[i] == 0)
      zr.push_back(arr[i]);
  }
  auto cmp = [](int a, int b) { return abs(a) > abs(b); };
  sort(pos.begin(), pos.end(), cmp);
  sort(neg.begin(), neg.end(), cmp);
  if (pos.size() == 0 && (k & 1)) {
    if (zr.size() > 0)
      printf("0\\n");
    else {
      ll c = 1;
      for (int i = neg.size() - 1; i >= max(0, (int)neg.size() - k); i--) {
        c = (c * neg[i] % MOD + MOD) % MOD;
      }
      printf("%lld\\n", c);
    }
    return 0;
  }
  prefP1[0] = 1, prefN1[0] = 1;
  for (int i = 1; i <= pos.size(); i++) {
    prefP[i] = prefP[i - 1] + std::log((LD)pos[i - 1]);
    prefP1[i] = prefP1[i - 1] * pos[i - 1] % MOD;
  }
  for (int i = 1; i <= neg.size(); i++) {
    prefN[i] = prefN[i - 1] + std::log((LD)abs(neg[i - 1]));
    prefN1[i] = ((prefN1[i - 1] * neg[i - 1] % MOD) + MOD) % MOD;
  }
  LD cur = -1.0 / 0.0;
  ll ans = 0;
  int sz = pos.size();
  for (int i = 0; i <= neg.size(); i += 2) {
    int req = k - i;
    if (req < 0)
      break;
    if (sz < req)
      continue;
    if (prefN[i] + prefP[req] > cur ||
        (i > 0 && req + 1 < sz &&
         neg[i] * neg[i - 1] > pos[req] * pos[req + 1])) {
      cur = prefN[i] + prefP[req];
      ans = prefN1[i] * prefP1[req] % MOD;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}