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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#include <cmath>
#include <queue>
#include <stack>

using namespace std;

#define LL long long

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

LL mod(LL x, LL md) { return ((x % md) + md) % md; }

class Mint {
public:
  static LL md;
  LL x;
  Mint(LL _x = 0) { x = mod(_x, md); }
  Mint(const Mint &y) { x = mod(y.x, md); }
};

void MintMyExgcd(LL a, LL b, LL &x, LL &y) {
  // solve equation ax + by = 1 (mod m)
  LL md = Mint::md;
  if (b == 0) {
    y = 0;
    x = 1;
    return;
  }
  MintMyExgcd(b, a % b, x, y);
  LL t = y;
  y = mod(x - (a / b) * y, md);
  x = t;
}

LL MintNY(LL x) {
  if (x == 0) {
    return 1;
  }
  LL md = Mint::md;
  LL a, b;
  MintMyExgcd(x, md, a, b);
  while (a < 0) {
    a += md;
  }
  return a;
}

/* LL gcd(LL a, LL b) {
        if (b == 0) {
                return a;
        }
        return gcd(b, a % b);
} */

Mint operator+(Mint a, Mint b) { return Mint(mod(a.x + b.x, Mint::md)); }
Mint operator-(Mint a, Mint b) { return Mint(mod(a.x - b.x, Mint::md)); }
Mint operator*(Mint a, Mint b) { return Mint(mod(a.x * b.x, Mint::md)); }
Mint operator/(Mint a, Mint b) {
  Mint c = Mint(MintNY(b.x));
  return Mint(mod(a.x * c.x, Mint::md));
}

Mint operator-(Mint a) { return Mint(-a.x); }
bool operator==(Mint a, Mint b) { return a.x == b.x; }
bool operator!=(Mint a, Mint b) { return a.x != b.x; }
bool operator>(Mint a, Mint b) { return a.x > b.x; }
bool operator<(Mint a, Mint b) { return a.x < b.x; }
bool operator>=(Mint a, Mint b) { return a.x >= b.x; }
bool operator<=(Mint a, Mint b) { return a.x <= b.x; }

void operator+=(Mint &a, Mint b) { a = a + b; }
void operator-=(Mint &a, Mint b) { a = a - b; }
void operator*=(Mint &a, Mint b) { a = a * b; }
void operator/=(Mint &a, Mint b) { a = a / b; }

ostream &operator<<(ostream &out, Mint a) {
  out << a.x;
  return out;
}

istream &operator>>(istream &in, Mint &a) {
  in >> a.x;
  return in;
}

Mint factorial(LL n) {
  if (n == 0) {
    return Mint(1);
  }
  Mint res = 1;
  for (LL i = 1; i <= n; ++i) {
    res *= i;
  }
  return res;
}
Mint multiRange(LL s, LL t) {
  Mint res = 1;
  for (LL i = s; i <= t; ++i) {
    res *= i;
  }
  return res;
}

Mint getC(LL n, LL k) {
  if (n - k < k) {
    return getC(n, n - k);
  }
  return multiRange(n - k + 1, n) / factorial(k);
}

Mint getA(LL n, LL k) { return multiRange(n - k + 1, n); }

Mint countNonNegativeDivision(LL n, LL k) { return getC(n + k - 1, k - 1); }
Mint countPositiveDivision(LL n, LL k) {
  if (n < k) {
    return 0;
  }
  if (k <= 0) {
    return 0;
  }
  return getC(n - 1, k - 1);
}
Mint quickMod(Mint a, LL b) {
  Mint res = 1;
  while (b > 0) {
    if (b % 2) {
      res *= a;
    }
    a *= a;
    b /= 2;
  }
  return res;
}
template <class TypeName> vector<TypeName> getVector1D(int n, TypeName val) {
  return vector<TypeName>(n, val);
}
template <class TypeName>
vector<vector<TypeName>> getVector2D(int n1, int n2, TypeName val) {
  vector<vector<TypeName>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<TypeName>(n2, val);
  }
  return res;
}
template <class TypeName>
vector<vector<vector<TypeName>>> getVector3D(int n1, int n2, int n3,
                                             TypeName val) {
  vector<vector<vector<TypeName>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<TypeName>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<TypeName>(n3, val);
    }
  }
  return res;
}
template <class TypeName>
vector<vector<vector<vector<TypeName>>>> getVector4D(int n1, int n2, int n3,
                                                     int n4, TypeName val) {
  vector<vector<vector<vector<TypeName>>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<vector<TypeName>>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<vector<TypeName>>(n3);
      for (int k = 0; k < n3; ++k) {
        res[i][j][k] = vector<TypeName>(n4, val);
      }
    }
  }
  return res;
}

LL Mint::md = 1000000007;
LL md = 1000000007;

int n, k;
vector<LL> a;
vector<LL> pp, mm;

LL getMaxValue(vector<LL> &aa, int k) {
  LL res = 1;
  int nn = aa.size();
  for (int i = 0; i < k; ++i) {
    res = res * aa[nn - i - 1];
    res = mod(res, md);
  }
  return res;
}
LL getMinValue(vector<LL> &aa, int k) {
  LL res = 1;
  int nn = aa.size();
  for (int i = 0; i < k; ++i) {
    res = res * aa[i];
    res = mod(res, md);
  }
  return res;
}
LL getTotal(vector<LL> &aa) {
  LL res = 1;
  for (int i = 0; i < aa.size(); ++i) {
    res = mod(res * aa[i], md);
  }
  return res;
}

int main() {
  a.clear();
  pp.clear();
  mm.clear();
  scanf("%d%d", &n, &k);
  LL aa;
  int pl = 0, mi = 0, ze = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &aa);
    a.push_back(aa);
    if (aa > 0) {
      ++pl;
      pp.push_back(aa);
    } else if (aa == 0) {
      ++ze;
    } else {
      ++mi;
      mm.push_back(-aa);
    }
  }
  sort(mm.begin(), mm.end());
  sort(pp.begin(), pp.end());
  // LL ans = 0;
  if (ze > n - k) {
    cout << "0" << endl;
    return 0;
  }
  if (ze == n - k) {
    LL ans = 1;
    if (mi % 2) {
      if (ze > 0) {
        ans = 0;
      } else {
        ans = mod(-getTotal(mm) * getTotal(pp), md);
      }
    } else {
      ans = mod(getTotal(mm) * getTotal(pp), md);
    }

    cout << ans << endl;
    return 0;
  }
  if (pl == 0) {
    if (k % 2) {
      if (ze == 0) {
        cout << (mod(-getMinValue(mm, k), md)) << endl;
        return 0;
      } else {
        cout << "0" << endl;
        return 0;
      }
    } else {
      cout << getMaxValue(mm, k) << endl;
      return 0;
    }
  }

  int minMinus = 0, maxPlus = k;
  if (pl < k) {
    minMinus = k - pl;
    if (minMinus % 2) {
      minMinus += 1;
    }
    maxPlus = k - minMinus; // <= pl
  }
  int plusN = maxPlus, minusN = minMinus;
  while (plusN >= 2 && minusN <= mi - 2) {
    LL p1 = pp[pl - plusN], p2 = pp[pl - plusN + 1];
    LL m1 = mm[mi - minusN - 1], m2 = mm[mi - minusN - 2];
    if (m1 * m2 > p1 * p2) {
      plusN -= 2;
      minusN += 2;
    } else {
      break;
    }
  }
  LL ans = mod(getMaxValue(pp, plusN) * getMaxValue(mm, minusN), md);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#include <cmath>
#include <queue>
#include <stack>

using namespace std;

#define LL long long

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

LL mod(LL x, LL md) { return ((x % md) + md) % md; }

class Mint {
public:
  static LL md;
  LL x;
  Mint(LL _x = 0) { x = mod(_x, md); }
  Mint(const Mint &y) { x = mod(y.x, md); }
};

void MintMyExgcd(LL a, LL b, LL &x, LL &y) {
  // solve equation ax + by = 1 (mod m)
  LL md = Mint::md;
  if (b == 0) {
    y = 0;
    x = 1;
    return;
  }
  MintMyExgcd(b, a % b, x, y);
  LL t = y;
  y = mod(x - (a / b) * y, md);
  x = t;
}

LL MintNY(LL x) {
  if (x == 0) {
    return 1;
  }
  LL md = Mint::md;
  LL a, b;
  MintMyExgcd(x, md, a, b);
  while (a < 0) {
    a += md;
  }
  return a;
}

/* LL gcd(LL a, LL b) {
        if (b == 0) {
                return a;
        }
        return gcd(b, a % b);
} */

Mint operator+(Mint a, Mint b) { return Mint(mod(a.x + b.x, Mint::md)); }
Mint operator-(Mint a, Mint b) { return Mint(mod(a.x - b.x, Mint::md)); }
Mint operator*(Mint a, Mint b) { return Mint(mod(a.x * b.x, Mint::md)); }
Mint operator/(Mint a, Mint b) {
  Mint c = Mint(MintNY(b.x));
  return Mint(mod(a.x * c.x, Mint::md));
}

Mint operator-(Mint a) { return Mint(-a.x); }
bool operator==(Mint a, Mint b) { return a.x == b.x; }
bool operator!=(Mint a, Mint b) { return a.x != b.x; }
bool operator>(Mint a, Mint b) { return a.x > b.x; }
bool operator<(Mint a, Mint b) { return a.x < b.x; }
bool operator>=(Mint a, Mint b) { return a.x >= b.x; }
bool operator<=(Mint a, Mint b) { return a.x <= b.x; }

void operator+=(Mint &a, Mint b) { a = a + b; }
void operator-=(Mint &a, Mint b) { a = a - b; }
void operator*=(Mint &a, Mint b) { a = a * b; }
void operator/=(Mint &a, Mint b) { a = a / b; }

ostream &operator<<(ostream &out, Mint a) {
  out << a.x;
  return out;
}

istream &operator>>(istream &in, Mint &a) {
  in >> a.x;
  return in;
}

Mint factorial(LL n) {
  if (n == 0) {
    return Mint(1);
  }
  Mint res = 1;
  for (LL i = 1; i <= n; ++i) {
    res *= i;
  }
  return res;
}
Mint multiRange(LL s, LL t) {
  Mint res = 1;
  for (LL i = s; i <= t; ++i) {
    res *= i;
  }
  return res;
}

Mint getC(LL n, LL k) {
  if (n - k < k) {
    return getC(n, n - k);
  }
  return multiRange(n - k + 1, n) / factorial(k);
}

Mint getA(LL n, LL k) { return multiRange(n - k + 1, n); }

Mint countNonNegativeDivision(LL n, LL k) { return getC(n + k - 1, k - 1); }
Mint countPositiveDivision(LL n, LL k) {
  if (n < k) {
    return 0;
  }
  if (k <= 0) {
    return 0;
  }
  return getC(n - 1, k - 1);
}
Mint quickMod(Mint a, LL b) {
  Mint res = 1;
  while (b > 0) {
    if (b % 2) {
      res *= a;
    }
    a *= a;
    b /= 2;
  }
  return res;
}
template <class TypeName> vector<TypeName> getVector1D(int n, TypeName val) {
  return vector<TypeName>(n, val);
}
template <class TypeName>
vector<vector<TypeName>> getVector2D(int n1, int n2, TypeName val) {
  vector<vector<TypeName>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<TypeName>(n2, val);
  }
  return res;
}
template <class TypeName>
vector<vector<vector<TypeName>>> getVector3D(int n1, int n2, int n3,
                                             TypeName val) {
  vector<vector<vector<TypeName>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<TypeName>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<TypeName>(n3, val);
    }
  }
  return res;
}
template <class TypeName>
vector<vector<vector<vector<TypeName>>>> getVector4D(int n1, int n2, int n3,
                                                     int n4, TypeName val) {
  vector<vector<vector<vector<TypeName>>>> res(n1);
  for (int i = 0; i < n1; ++i) {
    res[i] = vector<vector<vector<TypeName>>>(n2);
    for (int j = 0; j < n2; ++j) {
      res[i][j] = vector<vector<TypeName>>(n3);
      for (int k = 0; k < n3; ++k) {
        res[i][j][k] = vector<TypeName>(n4, val);
      }
    }
  }
  return res;
}

LL Mint::md = 1000000007;
LL md = 1000000007;

int n, k;
vector<LL> a;
vector<LL> pp, mm;

LL getMaxValue(vector<LL> &aa, int k) {
  LL res = 1;
  int nn = aa.size();
  for (int i = 0; i < k; ++i) {
    res = res * aa[nn - i - 1];
    res = mod(res, md);
  }
  return res;
}
LL getMinValue(vector<LL> &aa, int k) {
  LL res = 1;
  int nn = aa.size();
  for (int i = 0; i < k; ++i) {
    res = res * aa[i];
    res = mod(res, md);
  }
  return res;
}
LL getTotal(vector<LL> &aa) {
  LL res = 1;
  for (int i = 0; i < aa.size(); ++i) {
    res = mod(res * aa[i], md);
  }
  return res;
}

int main() {
  a.clear();
  pp.clear();
  mm.clear();
  scanf("%d%d", &n, &k);
  LL aa;
  int pl = 0, mi = 0, ze = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &aa);
    a.push_back(aa);
    if (aa > 0) {
      ++pl;
      pp.push_back(aa);
    } else if (aa == 0) {
      ++ze;
    } else {
      ++mi;
      mm.push_back(-aa);
    }
  }
  sort(mm.begin(), mm.end());
  sort(pp.begin(), pp.end());
  // LL ans = 0;
  if (ze > n - k) {
    cout << "0" << endl;
    return 0;
  }
  if (pl == 0) {
    if (k % 2) {
      if (ze == 0) {
        cout << (mod(-getMinValue(mm, k), md)) << endl;
        return 0;
      } else {
        cout << "0" << endl;
        return 0;
      }
    } else {
      cout << getMaxValue(mm, k) << endl;
      return 0;
    }
  }
  if (ze == n - k) {
    LL ans = 1;
    if (mi % 2) {
      if (ze > 0) {
        ans = 0;
      } else {
        ans = mod(-getTotal(mm) * getTotal(pp), md);
      }
    } else {
      ans = mod(getTotal(mm) * getTotal(pp), md);
    }

    cout << ans << endl;
    return 0;
  }

  int minMinus = 0, maxPlus = k;
  if (pl < k) {
    minMinus = k - pl;
    if (minMinus % 2) {
      minMinus += 1;
    }
    maxPlus = k - minMinus; // <= pl
  }
  int plusN = maxPlus, minusN = minMinus;
  while (plusN >= 2 && minusN <= mi - 2) {
    LL p1 = pp[pl - plusN], p2 = pp[pl - plusN + 1];
    LL m1 = mm[pl - minusN - 1], m2 = mm[mi - minusN - 2];
    if (m1 * m2 > p1 * p2) {
      plusN -= 2;
      minusN += 2;
    } else {
      break;
    }
  }
  LL ans = mod(getMaxValue(pp, plusN) * getMaxValue(mm, minusN), md);
  cout << ans << endl;
  return 0;
}