結果

問題 No.391 CODING WAR
ユーザー K UK U
提出日時 2023-06-12 19:01:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 2,695 bytes
コンパイル時間 5,862 ms
コンパイル使用メモリ 262,712 KB
実行使用メモリ 14,892 KB
最終ジャッジ日時 2023-09-02 13:01:30
合計ジャッジ時間 6,852 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
14,804 KB
testcase_01 AC 21 ms
14,772 KB
testcase_02 AC 22 ms
14,676 KB
testcase_03 AC 22 ms
14,724 KB
testcase_04 AC 23 ms
14,764 KB
testcase_05 AC 22 ms
14,720 KB
testcase_06 AC 22 ms
14,756 KB
testcase_07 AC 21 ms
14,684 KB
testcase_08 AC 21 ms
14,764 KB
testcase_09 AC 41 ms
14,808 KB
testcase_10 AC 40 ms
14,712 KB
testcase_11 AC 21 ms
14,764 KB
testcase_12 AC 22 ms
14,680 KB
testcase_13 AC 36 ms
14,892 KB
testcase_14 AC 35 ms
14,772 KB
testcase_15 AC 36 ms
14,776 KB
testcase_16 AC 30 ms
14,772 KB
testcase_17 AC 32 ms
14,800 KB
testcase_18 AC 28 ms
14,676 KB
testcase_19 AC 28 ms
14,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
#ifdef LOCAL
#include <debug.h>
#define dbg(...) debug::dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) void(0)
#endif
using namespace std;

// #define int long long

#define rep(i, a, b) for(int i=static_cast<int>(a), i##_end__=static_cast<int>(b); i < i##_end__; i++)
#define rep_r(i, a, b) for(int i=static_cast<int>(a), i##_end__=static_cast<int>(b); i >= i##_end__; i--)
#define fore(i, a) for(auto& i: a)
#define all(x) std::begin(x), std::end(x)

using ll = long long; // __int128;
using ull = unsigned long long;

template<class C> int siz(const C& c) { return static_cast<int>(c.size()); }
template<class T, size_t N> constexpr int siz(const T (&)[N]) { return static_cast<int>(N); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> constexpr T pow2(T x){ return x * x; }
template<class T, class S> constexpr T divceil(T x, S div){ return (x + div - 1) / div; }
constexpr long long INF = 1LL << 60; // 1.15e18
// constexpr int MOD = (int)1e9 + 7;

// 二項係数ライブラリ
template<class T> struct BiCoef {
    vector<T> fact_, inv_, finv_;
    constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
        int mod = fact_[0].mod();
        for(int i = 2; i < n; i++){
            fact_[i] = fact_[i-1] * i;
            inv_[i] = -inv_[mod % i] * (mod / i);
            finv_[i] = finv_[i-1] * inv_[i];
        }
    }

    // 重複なし組合せ - n 個のものから r 個取り出して作る組の総数 nCr / nCk - O(1)
    constexpr T nCr(int n, int k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        assert(n < siz(fact_) && k < siz(finv_));
        return fact_[n] * finv_[k] * finv_[n - k];
    }

    // n(≥k) 個の玉を k 個の箱にいれる。ただし、各箱に入る玉の個数は 1 個以上 - O(k log n)
    constexpr T nPr_rep1(ll n, int k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        T res = 0;
        for (int i = 0; i <= k; ++i) {
            auto add = nCr(k, i) * T(i).pow(n);
            if ((k - i) % 2 == 0) res += add;
            else res -= add;
        }
        return res;
    }
};

void _main() {
    ll N;
    int M;
    cin >> N >> M;

    using mint = modint1000000007;
    BiCoef<mint> bc((int)1e6);
    auto ans = bc.nPr_rep1(N, M);
    cout << ans.val() << endl;
}

signed main() { 
    cin.tie(nullptr); ios::sync_with_stdio(false);
    cout << fixed << setprecision(15); cerr << fixed << setprecision(15);
    _main();
}
0