Submission #520036

#TimeUsernameProblemLanguageResultExecution timeMemory
520036KoDZapina (COCI20_zapina)C++17
110 / 110
87 ms320 KiB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

constexpr int MOD = 1000000007;

struct Fp {
    int x;
    Fp(int x = 0) : x(x) {}
    Fp& operator+=(const Fp& t) {
        if ((x += t.x) >= MOD) x -= MOD;
        return *this;
    }
    Fp& operator-=(const Fp& t) {
        if ((x -= t.x) < 0) x += MOD;
        return *this;
    }
    Fp& operator*=(const Fp& t) {
        x = (long long)x * t.x % MOD;
        return *this;
    }
    Fp operator+(const Fp& t) const {
        return Fp(*this) += t;
    }
    Fp operator-(const Fp& t) const {
        return Fp(*this) -= t;
    }
    Fp operator*(const Fp& t) const {
        return Fp(*this) *= t;
    }
    Fp pow(int e) const {
        Fp ret(1), mul(*this);
        while (e > 0) {
            if (e & 1) ret *= mul;
            mul *= mul;
            e >>= 1;
        }
        return ret;
    }
    Fp inv() const {
        return pow(MOD - 2);
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N;
    std::cin >> N;
    vector<Fp> fact(N + 1), inv_fact(N + 1);
    fact[0] = 1;
    for (int i = 1; i <= N; ++i) {
        fact[i] = fact[i - 1] * i;
    }
    inv_fact[N] = fact[N].inv();
    for (int i = N; i >= 1; --i) {
        inv_fact[i - 1] = inv_fact[i] * i;
    }
    vector<Fp> dp(N + 1);
    dp[0] = 1;
    for (int i = 1; i <= N; ++i) {
        vector<Fp> next(N + 1);
        for (int j = 0; j <= N; ++j) {
            for (int k = 0; k <= j; ++k) {
                if (k != i) {
                    next[j] += dp[j - k] * inv_fact[k];
                }
            }
        }
        dp = std::move(next);
    }
    std::cout << (Fp(N).pow(N) - fact[N] * dp[N]).x << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...