Yukicoder No.523 LED

,

http://yukicoder.me/problems/no/523

solution

普通に組み合わせ。 $\sum_{0 \le i \lt n} {}_{2(n-i)}C_2 = \frac{2n!}{2^n}$。 $O(N)$。

implementation

#include <cstdio>
#include <vector>
#include <cassert>
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
using ll = long long;
using namespace std;

ll powmod(ll x, ll y, ll p) { // O(log y)
    assert (0 <= x and x < p);
    assert (0 <= y);
    ll z = 1;
    for (ll i = 1; i <= y; i <<= 1) {
        if (y & i) z = z * x % p;
        x = x * x % p;
    }
    return z;
}
ll inv(ll x, ll p) { // p must be a prime, O(log p)
    assert ((x % p + p) % p != 0);
    return powmod(x, p-2, p);
}
template <int mod>
int fact(int n) {
    static vector<int> memo(1,1);
    if (memo.size() <= n) {
        int l = memo.size();
        memo.resize(n+1);
        repeat_from (i,l,n+1) memo[i] = memo[i-1] *(ll) i % mod;
    }
    return memo[n];
}

constexpr int mod = 1e9+7;
int main() {
    int n; scanf("%d", &n);
    int ans = fact<mod>(2*n) * inv(powmod(2, n, mod), mod) % mod;
    printf("%d\n", ans);
    return 0;
}