結果

問題 No.1259 スイッチ
ユーザー finefine
提出日時 2020-10-16 22:34:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 4,409 bytes
コンパイル時間 1,622 ms
コンパイル使用メモリ 167,052 KB
実行使用メモリ 19,164 KB
最終ジャッジ日時 2023-09-28 04:20:26
合計ジャッジ時間 6,569 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
19,088 KB
testcase_01 AC 16 ms
19,012 KB
testcase_02 AC 15 ms
19,160 KB
testcase_03 AC 16 ms
18,964 KB
testcase_04 AC 16 ms
19,108 KB
testcase_05 AC 15 ms
19,012 KB
testcase_06 AC 16 ms
19,084 KB
testcase_07 AC 15 ms
19,164 KB
testcase_08 AC 16 ms
19,012 KB
testcase_09 AC 16 ms
19,048 KB
testcase_10 AC 72 ms
18,976 KB
testcase_11 AC 107 ms
18,964 KB
testcase_12 AC 70 ms
18,964 KB
testcase_13 AC 46 ms
18,964 KB
testcase_14 AC 59 ms
19,100 KB
testcase_15 AC 44 ms
19,016 KB
testcase_16 AC 90 ms
19,004 KB
testcase_17 AC 58 ms
19,004 KB
testcase_18 AC 100 ms
18,980 KB
testcase_19 AC 102 ms
19,072 KB
testcase_20 AC 74 ms
18,956 KB
testcase_21 AC 108 ms
18,980 KB
testcase_22 AC 42 ms
18,972 KB
testcase_23 AC 95 ms
19,028 KB
testcase_24 AC 75 ms
19,100 KB
testcase_25 AC 73 ms
19,012 KB
testcase_26 AC 118 ms
18,940 KB
testcase_27 AC 92 ms
18,964 KB
testcase_28 AC 74 ms
19,032 KB
testcase_29 AC 91 ms
19,004 KB
testcase_30 AC 21 ms
18,980 KB
testcase_31 AC 22 ms
18,968 KB
testcase_32 AC 19 ms
18,956 KB
testcase_33 AC 26 ms
18,968 KB
testcase_34 AC 20 ms
18,932 KB
testcase_35 AC 24 ms
19,012 KB
testcase_36 AC 23 ms
19,016 KB
testcase_37 AC 21 ms
19,016 KB
testcase_38 AC 21 ms
19,072 KB
testcase_39 AC 25 ms
19,160 KB
testcase_40 AC 25 ms
19,140 KB
testcase_41 AC 22 ms
19,012 KB
testcase_42 AC 26 ms
18,980 KB
testcase_43 AC 23 ms
19,160 KB
testcase_44 AC 25 ms
19,160 KB
testcase_45 AC 27 ms
19,052 KB
testcase_46 AC 21 ms
19,092 KB
testcase_47 AC 27 ms
19,044 KB
testcase_48 AC 21 ms
18,968 KB
testcase_49 AC 25 ms
19,024 KB
testcase_50 AC 67 ms
18,976 KB
testcase_51 AC 42 ms
18,944 KB
testcase_52 AC 105 ms
18,976 KB
testcase_53 AC 53 ms
18,932 KB
testcase_54 AC 103 ms
18,964 KB
testcase_55 AC 40 ms
18,964 KB
testcase_56 AC 57 ms
19,008 KB
testcase_57 AC 97 ms
19,032 KB
testcase_58 AC 69 ms
18,960 KB
testcase_59 AC 83 ms
19,008 KB
testcase_60 AC 113 ms
18,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

// https://noshi91.hatenablog.com/entry/2019/03/31/174006
template <std::uint_fast64_t Modulus>
struct ModInt {
    using u64 = std::uint_fast64_t;

    static constexpr u64 MOD = Modulus;

    u64 val;

    constexpr ModInt(const u64 x = 0) noexcept : val(x % MOD) {}

    constexpr ModInt operator+() const noexcept { return ModInt(*this); }
    constexpr ModInt operator-() const noexcept {
        ModInt res(*this);
        if (res.val != 0) res.val = MOD - res.val;
        return res;
    }

    constexpr bool operator==(const ModInt& rhs) const noexcept { return val == rhs.val; }
    constexpr bool operator!=(const ModInt& rhs) const noexcept { return val != rhs.val; }

    // prefix increment/decrement
    constexpr ModInt& operator++() noexcept { return *this += ModInt(1); }
    constexpr ModInt& operator--() noexcept { return *this -= ModInt(1); }

    // postfix increment/decrement
    constexpr ModInt operator++(int) noexcept {
        ModInt tmp(*this);
        ++*this;
        return tmp;
    }
    constexpr ModInt operator--(int) noexcept {
        ModInt tmp(*this);
        --*this;
        return tmp;
    }

    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 {
        val += rhs.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr ModInt& operator-=(const ModInt& rhs) noexcept {
        if (val < rhs.val) val += MOD;
        val -= rhs.val;
        return *this;
    }
    constexpr ModInt& operator*=(const ModInt& rhs) noexcept {
        val = val * rhs.val % MOD;
        return *this;
    }

    // prime Modulus only
    constexpr ModInt& operator/=(const ModInt& rhs) noexcept {
        return *this *= rhs.inv();
    }

    // prime Modulus only
    constexpr ModInt inv() const noexcept {
        return pow(*this, MOD - 2);
    }
};

template<std::uint_fast64_t Modulus>
constexpr ModInt<Modulus> pow(ModInt<Modulus> x, std::uint_fast64_t n) {
    ModInt<Modulus> res(1);
    while (n) {
        if (n & 1) res *= x;
        x *= x;
        n >>= 1;
    }
    return res;
}

template<std::uint_fast64_t Modulus>
istream& operator>>(istream& is, ModInt<Modulus>& x) {
    std::uint_fast64_t val;
    is >> val;
    x = ModInt<Modulus>(val);
    return is;
}

template<std::uint_fast64_t Modulus>
ostream& operator<<(ostream& os, const ModInt<Modulus>& x) {
    return os << x.val;
}

using mint = ModInt<1000000007>;

// g++ -Wl,--stack,[STACK SIZE] [file] でスタックサイズを増やすか、
// グローバル変数として宣言して使うこと!!!
template<size_t MAXV, class T = mint>
struct Comb {
    array<T, MAXV + 1> fact, fact_inv;

    constexpr Comb() noexcept {
        fact[0] = 1;
        for (size_t i = 1; i <= MAXV; i++) {
            fact[i] = fact[i - 1] * i;
        }

        fact_inv[MAXV] = fact[MAXV].inv();
        for (size_t i = MAXV; i > 0; i--) {
            fact_inv[i - 1] = fact_inv[i] * i;
        }
    }

    constexpr T operator()(int n, int k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        return fact[n] * fact_inv[k] * fact_inv[n - k];
    }

    constexpr T perm(int n, int k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        return fact[n] * fact_inv[n - k];
    }
};

const Comb<1000000> comb;

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

    int n;
    ll K;
    int m;
    cin >> n >> K >> m;

    if (n == 1) {
        cout << 1 << newl;
        return 0;
    }

    mint ans = 0;
    if (m == 1) {
        for (int i = 1; i <= n; i++) {
            if (K % i != 0) continue;
            ans += comb.perm(n - 1, i - 1) * pow(mint(n), n - i);
        }
    } else {
        for (int i = 2; i <= n; i++) {
            ans += comb.perm(n - 2, i - 2) * (i - (K % i == 0)) * pow(mint(n), n - i);
        }
    }
    cout << ans << newl;

    return 0;
}
0