Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int maxN = 2e5 + 10, base = 1e9 + 7;
int N, X, Y;
int dd[maxN], sl[maxN];
long long ans;
vector<int> L, V, prime, color[maxN];

bool cmp(int a, int b) { return color[a][0] < color[b][0]; }

void eratos() {
  for (int i = 2; i < maxN / 2; i++)
    if (!dd[i])
      for (int j = 2; j * i < maxN; j++)
        dd[i * j] = 1;
  for (int i = 2; i < maxN; i++)
    if (!dd[i])
      prime.push_back(i);
}

void fact(int k, int w) {
  for (int i = 0; i < prime.size(); i++) {
    int x = prime[i], t = k, res = 0;
    if (x > k)
      return;
    while (t)
      t /= x, res += t;
    sl[i] += res * w;
  }
}

long long mulpower(int x, int n) {
  if (n == 1)
    return x;
  long long w = mulpower(x, n / 2);
  w = (w * w) % base;
  return n % 2 == 0 ? w : (w * x) % base;
}

int main() {
  eratos();
  cin >> N >> X >> Y;
  for (int i = 0, c, w; i < N; i++) {
    cin >> c >> w;
    if (color[c].empty())
      L.push_back(c);
    color[c].push_back(w);
  }
  for (int i = 1; i <= N; i++)
    sort(color[i].begin(), color[i].end());
  sort(L.begin(), L.end(), cmp);
  int vt = 0, sum = 0;
  for (int i = 1; i < L.size(); i++)
    if (color[L[0]][0] + color[L[i]][0] <= Y)
      vt = i;
  for (int i = 0; i <= vt; i++) {
    int x = L[i], num = 1;
    for (int j = 1; j < color[x].size(); j++) {
      if (color[x][0] + color[x][j] <= X)
        num = j + 1;
      if (i && color[x][j] + color[L[0]][0] <= Y)
        num = j + 1;
      if (i != 1 && color[x][j] + color[L[1]][0] <= Y)
        num = j + 1;
    }
    V.push_back(num);
    sum += num;
  }
  fact(sum, 1);
  for (int i = 0; i < V.size(); i++)
    fact(V[i], -1);
  ans = 1;
  for (int i = 0; i < prime.size(); i++)
    if (sl[i])
      ans = (ans * mulpower(prime[i], sl[i])) % base;
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxN = 2e5 + 10, base = 1e9 + 7;
int N, X, Y;
int dd[maxN], sl[maxN];
long long ans;
vector<int> L, V, prime, color[maxN];

bool cmp(int a, int b) { return color[a][0] < color[b][0]; }

void eratos() {
  for (int i = 2; i < maxN / 2; i++)
    if (!dd[i])
      for (int j = 2; j * i < maxN; j++)
        dd[i * j] = 1;
  for (int i = 2; i < maxN; i++)
    if (!dd[i])
      prime.push_back(i);
}

void fact(int k, int w) {
  for (int i = 0; i < prime.size(); i++) {
    int x = prime[i], t = k, res = 0;
    if (x > k)
      return;
    while (t)
      t /= x, res += t;
    sl[i] += res * w;
  }
}

long long mulpower(int x, int n) {
  if (n == 1)
    return x;
  long long w = mulpower(x, n / 2);
  w = (w * w) % base;
  return n % 2 == 0 ? w : (w * x) % base;
}

int main() {
  eratos();
  cin >> N >> X >> Y;
  for (int i = 0, c, w; i < N; i++) {
    cin >> c >> w;
    if (color[c].empty())
      L.push_back(c);
    color[c].push_back(w);
  }
  for (int i = 1; i <= N; i++)
    sort(color[i].begin(), color[i].end());
  sort(L.begin(), L.end(), cmp);
  int vt = 0, sum = 0;
  for (int i = 1; i < L.size(); i++)
    if (color[L[0]][0] + color[L[i]][0] <= Y)
      vt = i;
  for (int i = 0; i <= vt; i++) {
    int x = L[i], num = 1;
    for (int j = 1; j < color[x].size(); j++) {
      if (color[x][0] + color[x][j] <= X)
        num = j + 1;
      if (i && color[x][j] + color[L[0]][0] <= Y)
        num = j + 1;
      if (L.size() > 1 && i != 1 && color[x][j] + color[L[1]][0] <= Y)
        num = j + 1;
    }
    V.push_back(num);
    sum += num;
  }
  fact(sum, 1);
  for (int i = 0; i < V.size(); i++)
    fact(V[i], -1);
  ans = 1;
  for (int i = 0; i < prime.size(); i++)
    if (sl[i])
      ans = (ans * mulpower(prime[i], sl[i])) % base;
  cout << ans;
}