Problem p02769 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)

typedef long long LL;
const LL mod = 1000000007;
LL n, k;
LL pow(LL x, LL n) {
  LL ret = 1;
  while (n) {
    if (n & 1) {
      ret *= x;
      ret %= mod;
    }
    x *= x;
    x %= mod;
    n >>= 1;
  }
  return ret;
}

LL inv(LL x) { return pow(x, mod - 2); }

LL C(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  LL ret = n;
  for (int i = 2; i <= k; ++i) {
    ret *= (n - i + 1);
    ret %= mod;
    ret *= inv(i);
    ret %= mod;
  }
  return ret;
}

LL Cnext(LL n, LL k, LL Cnkm1) {
  LL ret = Cnkm1 * (n - k + 1);
  ret %= mod;
  ret *= inv(k);
  ret %= mod;
  return ret;
}

const int maxn = 200005;
LL a[maxn], b[maxn];

int main() {
#ifdef __APPLE__
  freopen("main.in", "r", stdin);
#endif
  cin >> n >> k;
  if (k == 1) {
    cout << n << endl;
    return 0;
  }
  if (k >= n)
    k = n - 1;
  /*a[0] = 1;
  for (int i = 1; i <= k; ++i) {
      LL next = Cnext(n - 1 + i - 1, i, a[i - 1]);
      a[i] = (a[i - 1] + next) % mod;
  }
  LL ans = 0;
  for (int i = 0; i <= k; ++i) {
      ans += a[i]; ans %= mod;
  }
  cout << ans << endl;*/
  for (int i = 0; i <= n - 1; ++i)
    a[i] = C(n - 1, i);
  for (int i = 0; i <= n; ++i)
    b[i] = C(n, i);
  LL ans = 0;
  for (int i = 0; i <= k; ++i) {
    LL cur = b[i];
    cur *= a[n - i - 1];
    cur %= mod;
    ans += cur;
    ans %= mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)

typedef long long LL;
const LL mod = 1000000007;
LL n, k;
LL pow(LL x, LL n) {
  LL ret = 1;
  while (n) {
    if (n & 1) {
      ret *= x;
      ret %= mod;
    }
    x *= x;
    x %= mod;
    n >>= 1;
  }
  return ret;
}

LL inv(LL x) { return pow(x, mod - 2); }

LL C(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  LL ret = n;
  for (int i = 2; i <= k; ++i) {
    ret *= (n - i + 1);
    ret %= mod;
    ret *= inv(i);
    ret %= mod;
  }
  return ret;
}

LL Cnext(LL n, LL k, LL Cnkm1) {
  LL ret = Cnkm1 * (n - k + 1);
  ret %= mod;
  ret *= inv(k);
  ret %= mod;
  return ret;
}

const int maxn = 200005;
LL a[maxn], b[maxn];

int main() {
#ifdef __APPLE__
  freopen("main.in", "r", stdin);
#endif
  cin >> n >> k;
  if (k == 1) {
    cout << n << endl;
    return 0;
  }
  if (k >= n)
    k = n - 1;
  /*a[0] = 1;
  for (int i = 1; i <= k; ++i) {
      LL next = Cnext(n - 1 + i - 1, i, a[i - 1]);
      a[i] = (a[i - 1] + next) % mod;
  }
  LL ans = 0;
  for (int i = 0; i <= k; ++i) {
      ans += a[i]; ans %= mod;
  }
  cout << ans << endl;*/

  a[0] = 1;
  for (int i = 1; i <= n - 1; ++i) {
    a[i] = Cnext(n - 1, i, a[i - 1]);
  }

  b[0] = 1;
  for (int i = 1; i <= n; ++i) {
    b[i] = Cnext(n, i, b[i - 1]);
  }
  LL ans = 0;
  for (int i = 0; i <= k; ++i) {
    LL cur = b[i];
    cur *= a[n - i - 1];
    cur %= mod;
    ans += cur;
    ans %= mod;
  }
  cout << ans << endl;
}