Problem p02990 (C++)

Buggy submission (status: Runtime Error):
void solve();

int main() {
  solve();
  return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////
#include <algorithm>
#include <assert.h>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef long double ld;
typedef long long ll;
typedef complex<ld> Point;
int N, K;
vector<vector<ll>> dp;

int MOD = 1e9 + 7;

// N!を求める(O(N))
long long stair(int N, int mod = 1000000007) {
  long long temp = 1;
  if (mod != -1) {
    for (int i = 2; i <= N; i++) {
      temp = (temp * i) % mod;
    }
  } else {
    for (int i = 2; i <= N; i++) {
      temp = temp * i;
    }
  }
  return temp;
}

// 0!,1!,...,N!を求める(O(N))
void stairs(int N, vector<long long> &vec, int mod = 1000000007) {
  long long temp = 1;
  vec.resize(N + 1);
  vec[0] = 1;
  vec[1] = 1;
  for (int i = 2; i <= N; i++) {
    if (mod != -1)
      temp = (temp * i) % mod;
    else
      temp *= i;
    vec[i] = temp;
  }
  return;
}

// 繰り返し二乗法
// 掛け算が定義されていることが必要
// O(log n)
template <class T> T pow(T base, int p, T mod = -1) {
  if (p == 0)
    return 1;
  if (p == 1)
    return mod == -1 ? base % mod : base;
  int b = 1, c = 0;
  while (b <= p) {
    b *= 2;
    c++;
  }
  T *arr2 = new T[c]; // T[n] = T^(2^n)
  arr2[0] = mod == -1 ? base % mod : base;
  for (int n = 1; n < c; n++) {
    if (mod != -1)
      arr2[n] = (arr2[n - 1] * arr2[n - 1]) % mod;
    else
      arr2[n] = arr2[n - 1] * arr2[n - 1];
  }
  c--;
  T ans = arr2[c];
  b /= 2;
  p -= b;
  while (p > 0) {
    c--;
    b /= 2;
    if (p >= b) {
      if (mod != -1)
        ans = (ans * arr2[c]) % mod;
      else
        ans = ans * arr2[c];
      p -= b;
    }
  }
  return ans;
}

// 0!,1!,...,N!の逆元(mod剰余)を求める
// O(log(mod)+N)
void stairInv(int N, vector<long long> &vect, int mod = 1000000007) {
  vect.resize(N + 1);
  long long temp = stair(N, mod);
  temp = pow(temp, mod - 2, (long long)mod);
  vect[N] = temp;
  for (long long n = N - 1; n >= 1; n--) {
    temp = (vect[n + 1] * (n + 1)) % mod;
    vect[n] = temp;
  }
  return;
}

// nCkを求める(mod剰余)
// O(N+log(mod)+max(n-k,k))
long long C(int n, int k, int mod = 1000000007) {
  if (n == k)
    return 1;
  if (k == 0)
    return 1;
  long long temp = stair(n, mod);
  vector<long long> *vect = new vector<long long>;
  stairInv(max(n - k, k), *vect, mod);
  temp = (temp * vect->at(n - k)) % mod;
  temp = (temp * vect->at(k)) % mod;
  delete vect;
  return temp;
}

void solve() {
  C(3, 3);
  cin >> N >> K;
  dp.resize(K + 1);
  for (int k = 0; k <= K; k++) {
    dp[k].resize(K + 1, 0);
  }
  for (int k = 1; k <= K; k++)
    dp[k][1] = 1;
  dp[1][1] = 1;
  for (int k = 2; k <= K; k++) {
    for (int i = 2; i <= k; i++) {
      dp[k][i] = dp[k - 1][i] + dp[k - 1][i - 1];
      dp[k][i] %= MOD;
    }
  }
  for (int i = 1; i <= K; i++) {
    cout << (C(N - K + 1, i) * dp[K][i]) % MOD << endl;
  }
  return;
}

Fixed submission:
void solve();

int main() {
  solve();
  return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////
#include <algorithm>
#include <assert.h>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef long double ld;
typedef long long ll;
typedef complex<ld> Point;
int N, K;
vector<vector<ll>> dp;

int MOD = 1e9 + 7;

// N!を求める(O(N))
long long stair(int N, int mod = 1000000007) {
  long long temp = 1;
  if (mod != -1) {
    for (int i = 2; i <= N; i++) {
      temp = (temp * i) % mod;
    }
  } else {
    for (int i = 2; i <= N; i++) {
      temp = temp * i;
    }
  }
  return temp;
}

// 0!,1!,...,N!を求める(O(N))
void stairs(int N, vector<long long> &vec, int mod = 1000000007) {
  long long temp = 1;
  vec.resize(N + 1);
  vec[0] = 1;
  vec[1] = 1;
  for (int i = 2; i <= N; i++) {
    if (mod != -1)
      temp = (temp * i) % mod;
    else
      temp *= i;
    vec[i] = temp;
  }
  return;
}

// 繰り返し二乗法
// 掛け算が定義されていることが必要
// O(log n)
template <class T> T pow(T base, int p, T mod = -1) {
  if (p == 0)
    return 1;
  if (p == 1)
    return mod == -1 ? base % mod : base;
  int b = 1, c = 0;
  while (b <= p) {
    b *= 2;
    c++;
  }
  T *arr2 = new T[c]; // T[n] = T^(2^n)
  arr2[0] = mod == -1 ? base % mod : base;
  for (int n = 1; n < c; n++) {
    if (mod != -1)
      arr2[n] = (arr2[n - 1] * arr2[n - 1]) % mod;
    else
      arr2[n] = arr2[n - 1] * arr2[n - 1];
  }
  c--;
  T ans = arr2[c];
  b /= 2;
  p -= b;
  while (p > 0) {
    c--;
    b /= 2;
    if (p >= b) {
      if (mod != -1)
        ans = (ans * arr2[c]) % mod;
      else
        ans = ans * arr2[c];
      p -= b;
    }
  }
  return ans;
}

// 0!,1!,...,N!の逆元(mod剰余)を求める
// O(log(mod)+N)
void stairInv(int N, vector<long long> &vect, int mod = 1000000007) {
  vect.resize(N + 1);
  long long temp = stair(N, mod);
  temp = pow(temp, mod - 2, (long long)mod);
  vect[N] = temp;
  for (long long n = N - 1; n >= 1; n--) {
    temp = (vect[n + 1] * (n + 1)) % mod;
    vect[n] = temp;
  }
  return;
}

// nCkを求める(mod剰余)
// O(N+log(mod)+max(n-k,k))
long long C(int n, int k, int mod = 1000000007) {
  if (n == k)
    return 1;
  if (k == 0)
    return 1;
  long long temp = stair(n, mod);
  vector<long long> *vect = new vector<long long>;
  stairInv(max(n - k, k), *vect, mod);
  temp = (temp * vect->at(n - k)) % mod;
  temp = (temp * vect->at(k)) % mod;
  delete vect;
  return temp;
}

void solve() {
  C(3, 3);
  cin >> N >> K;
  dp.resize(K + 1);
  for (int k = 0; k <= K; k++) {
    dp[k].resize(K + 1, 0);
  }
  for (int k = 1; k <= K; k++)
    dp[k][1] = 1;
  dp[1][1] = 1;
  for (int k = 2; k <= K; k++) {
    for (int i = 2; i <= k; i++) {
      dp[k][i] = dp[k - 1][i] + dp[k - 1][i - 1];
      dp[k][i] %= MOD;
    }
  }
  for (int i = 1; i <= K; i++) {
    if (i > N - K + 1)
      cout << 0 << endl;
    else
      cout << (C(N - K + 1, i) * dp[K][i]) % MOD << endl;
  }
  return;
}