Problem p03253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define ll long long
#define int ll
#define INF (1LL << 60)
#define mod 1000000007
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

// 素数判定
bool isprime(ll n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  else
    for (int i = 3; i * i <= n; i++)
      if (n % i == 0)
        return false;

  return true;
}

// ax+by=1の解
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

// mod mでのaの逆数
ll mod_inverse(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// nの階乗 mod p
ll factorial(int n, int p) {
  ll r = 1;
  for (int i = 1; i <= n; i++)
    r = (r * i) % p;
  return r;
}

// nPr
ll permutation(int n, int r) {
  ll a = 1;
  for (int i = n - r + 1; i <= n; i++)
    a *= i;
  return a;
}

// nPr mod p
ll permutation(int n, int r, int p) {
  ll a = 1;
  for (int i = n - r + 1; i <= n; i++)
    a = (a * i) % p;
  return a;
}

// nCr mod p
ll combination(int n, int r, int p) {
  if (r > n / 2)
    r = n - r;
  ll numerator = permutation(n, r, p);
  ll denominator = factorial(r, p) % p;
  ll inv = mod_inverse(denominator, p);
  return (numerator * inv) % p;
}

signed main() {
  int N, M;
  cin >> N >> M;

  if (isprime(M))
    cout << N << endl;
  else {
    int result = 1;
    for (int i = 2;; i++) {
      int power = 0;
      while (M > 0 && M % i == 0) {
        M /= i;
        power++;
      }
      result *= combination(N + power - 1, power, mod);
      result = result % mod;
      if (M == 1)
        break;
    }
    cout << result << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define ll long long
#define int ll
#define INF (1LL << 60)
#define mod 1000000007
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

// 素数判定
bool isprime(ll n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  else
    for (int i = 3; i * i <= n; i++)
      if (n % i == 0)
        return false;

  return true;
}

// ax+by=1の解
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

// mod mでのaの逆数
ll mod_inverse(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

// nの階乗 mod p
ll factorial(int n, int p) {
  ll r = 1;
  for (int i = 1; i <= n; i++)
    r = (r * i) % p;
  return r;
}

// nPr
ll permutation(int n, int r) {
  ll a = 1;
  for (int i = n - r + 1; i <= n; i++)
    a *= i;
  return a;
}

// nPr mod p
ll permutation(int n, int r, int p) {
  ll a = 1;
  for (int i = n - r + 1; i <= n; i++)
    a = (a * i) % p;
  return a;
}

// nCr mod p
ll combination(int n, int r, int p) {
  if (r > n / 2)
    r = n - r;
  ll numerator = permutation(n, r, p);
  ll denominator = factorial(r, p) % p;
  ll inv = mod_inverse(denominator, p);
  return (numerator * inv) % p;
}

signed main() {
  int N, M;
  cin >> N >> M;

  if (isprime(M))
    cout << N << endl;
  else {
    int result = 1;
    for (int i = 2;; i++) {
      int power = 0;
      while (M > 0 && M % i == 0) {
        M /= i;
        power++;
      }
      if (power > 0) {
        result *= combination(N + power - 1, power, mod);
        result = result % mod;
      }
      if (M == 1)
        break;
    }
    cout << result << endl;
  }

  return 0;
}