結果

問題 No.1035 Color Box
ユーザー snow39snow39
提出日時 2021-08-10 22:26:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 4,517 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 110,628 KB
実行使用メモリ 27,484 KB
最終ジャッジ日時 2023-10-24 09:23:34
合計ジャッジ時間 3,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
27,484 KB
testcase_01 AC 24 ms
26,704 KB
testcase_02 AC 24 ms
26,704 KB
testcase_03 AC 24 ms
26,704 KB
testcase_04 AC 24 ms
26,704 KB
testcase_05 AC 23 ms
26,704 KB
testcase_06 AC 23 ms
26,704 KB
testcase_07 AC 23 ms
26,704 KB
testcase_08 AC 31 ms
27,484 KB
testcase_09 AC 26 ms
26,956 KB
testcase_10 AC 23 ms
26,704 KB
testcase_11 AC 23 ms
26,704 KB
testcase_12 AC 23 ms
26,704 KB
testcase_13 AC 29 ms
27,220 KB
testcase_14 AC 30 ms
27,220 KB
testcase_15 AC 33 ms
27,484 KB
testcase_16 AC 24 ms
26,704 KB
testcase_17 AC 24 ms
26,704 KB
testcase_18 AC 24 ms
26,704 KB
testcase_19 AC 32 ms
27,484 KB
testcase_20 AC 24 ms
26,704 KB
testcase_21 AC 24 ms
26,704 KB
testcase_22 AC 23 ms
26,704 KB
testcase_23 AC 23 ms
26,704 KB
testcase_24 AC 24 ms
26,704 KB
testcase_25 AC 23 ms
26,704 KB
testcase_26 AC 24 ms
26,704 KB
testcase_27 AC 23 ms
26,704 KB
testcase_28 AC 23 ms
26,704 KB
testcase_29 AC 23 ms
26,704 KB
testcase_30 AC 24 ms
26,704 KB
testcase_31 AC 23 ms
26,704 KB
testcase_32 AC 23 ms
26,704 KB
testcase_33 AC 23 ms
26,704 KB
testcase_34 AC 23 ms
26,704 KB
testcase_35 AC 23 ms
26,704 KB
testcase_36 AC 24 ms
26,704 KB
testcase_37 AC 24 ms
26,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T>
void chmin(T& a, const T& b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
    if (a < b) a = b;
}

template <long long mod>
class modint {
private:
    using T = long long;
    T a;

public:
    constexpr modint(const long long x = 0) noexcept : a((x % mod + mod) % mod) {}
    constexpr T& value() noexcept {
        return a;
    }
    constexpr const T& value() const noexcept {
        return a;
    }
    constexpr modint operator-() const noexcept {
        return modint(0) -= *this;
    }
    constexpr modint operator+(const modint& rhs) const noexcept {
        return modint(*this) += rhs;
    }
    constexpr modint operator-(const modint& rhs) const noexcept {
        return modint(*this) -= rhs;
    }
    constexpr modint operator*(const modint& rhs) const noexcept {
        return modint(*this) *= rhs;
    }
    constexpr modint operator/(const modint& rhs) const noexcept {
        return modint(*this) /= rhs;
    }
    constexpr modint& operator+=(const modint& rhs) noexcept {
        a += rhs.a;
        if (a >= mod) a -= mod;
        return *this;
    }
    constexpr modint& operator-=(const modint& rhs) noexcept {
        if (a < rhs.a) a += mod;
        a -= rhs.a;
        return *this;
    }
    constexpr modint& operator*=(const modint& rhs) noexcept {
        a = a * rhs.a % mod;
        return *this;
    }
    constexpr modint& operator/=(const modint& rhs) noexcept {
        return *this *= rhs.inv();
    }
    constexpr bool operator==(const modint& rhs) const noexcept {
        return a == rhs.a;
    }
    constexpr bool operator!=(const modint& rhs) const noexcept {
        return not(*this == rhs);
    }
    constexpr modint pow(long long k) const noexcept {
        modint ret(1);
        modint x = k > 0 ? *this : this->inv();
        k = abs(k);
        while (k > 0) {
            if (k & 1) ret *= x;
            x *= x;
            k >>= 1;
        }
        return ret;
    }
    constexpr modint inv() const noexcept {
        return pow(mod - 2);
    }
    friend std::ostream& operator<<(std::ostream& os, const modint& X) noexcept {
        return os << X.a;
    }
    friend std::istream& operator>>(std::istream& is, modint& X) noexcept {
        is >> X.a;
        X.a %= mod;
        if (X.a < 0) X.a += mod;
        return is;
    }
};

template <typename T>
struct Combination {
    int MAX_N = 1'000'010;
    vector<T> fac, ifac, inv;
    Combination(int MAX_N = 1'000'010) : MAX_N(MAX_N), fac(MAX_N), ifac(MAX_N), inv(MAX_N) {
        fac[0] = 1, ifac[0] = 1, inv[0] = 1;
        for (int i = 1; i < MAX_N; ++i) {
            fac[i] = fac[i - 1] * i;
        }
        ifac[MAX_N - 1] = T(1) / fac[MAX_N - 1];
        for (int i = MAX_N - 1; i > 0; --i) {
            ifac[i - 1] = ifac[i] * i;
        }
        for (int i = MAX_N - 1; i > 0; --i) {
            inv[i] = ifac[i] * fac[i - 1];
        }
    }

    T perm(int n, int k) {
        if (n < k || n < 0 || k < 0)
            return 0;
        else
            return fac[n] * ifac[n - k];
    }

    T comb(int n, int k) {
        if (n < k || n < 0 || k < 0)
            return 0;
        else
            return fac[n] * ifac[k] * ifac[n - k];
    }

    T hcomb(int n, int r) {  // this size is really ok??
        if (n == 0 && r == 0)
            return 1;
        else if (n < 0 || r < 0)
            return 0;
        else
            return comb(n + r - 1, r);
    }

    T binom(ll _n, int k) {
        if (_n < k || _n < 0 || k < 0) return 0;

        T n = _n;
        T res = 1;
        for (int i = 0; i < k; i++) res = res * (n - i);
        res *= ifac[k];
        return res;
    }
};

using mint = modint<1000000007>;

void solve() {
    Combination<mint> math;
    int N, M;
    cin >> N >> M;

    vector<mint> dp(M + 1, 0);
    for (int i = 1; i <= M; i++) {
        mint res = math.comb(M, i) * mint(i).pow(N);
        res -= dp[i - 1];

        dp[i] = res;
    }

    mint ans = dp[M];
    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0