Problem p02862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

const int det = -3;

long long C[2000005];

bool ipr[1000005];

vector<int> primelessthanN(int n) {
  // vector<bool> isprime(n + 1, true);
  vector<int> prime;
  prime.push_back(2);
  int i;
  for (i = 3; i * i <= n; i += 2) {
    if (!ipr[i]) {
      prime.push_back(i);
      for (int j = i * i; j <= n; j += i)
        ipr[j] = true;
    }
  }
  while (i <= n) {
    if (!ipr[i])
      prime.push_back(i);
    i += 2;
  }
  return prime;
}
int Cal(int n, int p) {
  int res = 0;
  long long rec = p;
  while ((long long)n >= rec) {
    res += (int)((long long)n / rec);
    rec *= (long long)p;
  }
  return res;
}

const int Mod = 1000000007;
long long PowerMod(int n, int k) {
  int t(k), n0(n);
  long long res = 1;
  while (t) {
    if (t & 1)
      res = (res * (long long)n0) % Mod;
    n0 = (n0 * n0) % Mod;
    t >>= 1;
  }
  return res;
}

long long Cnm(int n, int m) {
  vector<int> prime = primelessthanN(n);
  long long res = 1;
  for (int i = 0; i < prime.size(); i++) {
    res = (res * (PowerMod(prime[i], Cal(n, prime[i]) - Cal(n - m, prime[i]) -
                                         Cal(m, prime[i])))) %
          Mod;
  }
  return res;
}

int main() {
  int x, y;
  cin >> x >> y;
  int det1 = x - 2 * y;
  int det2 = y - 2 * x;
  if (det1 % det != 0 || det2 % det != 0)
    return cout << 0 << endl, 0;

  int m = det1 / det;
  int n = det2 / det;
  int s = m + n;
  long long ans = 1LL;
  // C[0] = 1;
  // for (int i = 1; i <= n; i++)
  //	C[i] = (C[i - 1] * (s - i + 1) / i) % (1000000007);
  cout << Cnm(s, (m > n) ? n : m) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

const int det = -3;

long long C[2000005];

bool ipr[1000005];

vector<int> primelessthanN(int n) {
  // vector<bool> isprime(n + 1, true);
  vector<int> prime;
  prime.push_back(2);
  int i;
  for (i = 3; i * i <= n; i += 2) {
    if (!ipr[i]) {
      prime.push_back(i);
      for (int j = i * i; j <= n; j += i)
        ipr[j] = true;
    }
  }
  while (i <= n) {
    if (!ipr[i])
      prime.push_back(i);
    i += 2;
  }
  return prime;
}
int Cal(int n, int p) {
  int res = 0;
  long long rec = p;
  while ((long long)n >= rec) {
    res += (int)((long long)n / rec);
    rec *= (long long)p;
  }
  return res;
}

const int Mod = 1000000007;
long long PowerMod(int n, int k) {
  int t(k), n0(n);
  long long res = 1;
  while (t) {
    if (t & 1)
      res = (res * (long long)n0) % Mod;
    n0 = (n0 * n0) % Mod;
    t >>= 1;
  }
  return res;
}

long long Cnm(int n, int m) {
  vector<int> prime = primelessthanN(n);
  long long res = 1;
  for (int i = 0; i < prime.size(); i++) {
    res = (res * (PowerMod(prime[i], Cal(n, prime[i]) - Cal(n - m, prime[i]) -
                                         Cal(m, prime[i])))) %
          Mod;
  }
  return res;
}

int main() {
  int x, y;
  cin >> x >> y;
  int det1 = x - 2 * y;
  int det2 = y - 2 * x;
  if (det1 % det != 0 || det2 % det != 0)
    return cout << 0 << endl, 0;

  int m = det1 / det;
  int n = det2 / det;
  if (m < 0 || n < 0)
    return cout << 0 << endl, 0;
  int s = m + n;
  long long ans = 1LL;
  // C[0] = 1;
  // for (int i = 1; i <= n; i++)
  //	C[i] = (C[i - 1] * (s - i + 1) / i) % (1000000007);
  cout << Cnm(s, (m > n) ? n : m) << endl;
  return 0;
}