結果

問題 No.2326 Factorial to the Power of Factorial to the...
ユーザー Ryoga.exeRyoga.exe
提出日時 2023-05-28 14:45:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 4,380 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 198,904 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 10:33:11
合計ジャッジ時間 2,887 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1e9 + 7;

template<long long m, std::enable_if_t<(1 <= m)>* = nullptr>
struct ModInt {
public:
    using value_type = unsigned long long;
    using signed_value_type = std::make_signed_t<value_type>;
private:
    value_type m_value = 0;
    static constexpr value_type umod() noexcept { return static_cast<value_type>(m); }
public:
    ModInt() = default;
    ModInt(const ModInt&) = default;
    ModInt(ModInt&&) noexcept = default;
    constexpr ModInt(const signed_value_type x) noexcept : m_value((x % m + m) % m) {}
    constexpr ModInt& operator =(const ModInt&) = default;
    constexpr ModInt& operator =(ModInt&&) noexcept = default;
    constexpr ModInt& operator =(const signed_value_type x) { m_value = (x % m + m) % m; return *this; }
    inline constexpr value_type value() const noexcept { return m_value; }
    explicit inline constexpr operator value_type() const noexcept { return m_value; }
    explicit inline constexpr operator signed_value_type() const noexcept { return static_cast<signed_value_type>(m_value); }
    explicit inline constexpr operator int() const noexcept { return static_cast<int>(m_value); }
    static ModInt raw(const value_type& x) { ModInt tmp; tmp.m_value = x; return tmp; }
    ModInt& operator++() { m_value++; if (m_value == umod()) m_value = 0; return *this; }
    ModInt& operator--() { if (m_value == 0) m_value = umod(); m_value--; return *this; }
    ModInt operator++(int) { ModInt tmp(*this); ++(*this); return tmp; }
    ModInt operator--(int) { ModInt tmp(*this); --(*this); return tmp; }
    ModInt& operator +=(const ModInt& rhs) { m_value += rhs.m_value; if (m_value >= umod()) m_value -= umod(); return *this; }
    ModInt& operator -=(const ModInt& rhs) { m_value -= rhs.m_value; if (m_value >= umod()) m_value += umod(); return *this; }
    ModInt& operator *=(const ModInt& rhs) { m_value = (m_value * rhs.m_value) % umod(); return *this; }
    ModInt& operator /=(const ModInt& rhs) { return *this = *this * rhs.inv(); }
    ModInt& operator ^=(const long long rhs) { return *this = pow(rhs); }
    ModInt operator +() const { return *this; }
    ModInt operator -() const { return ModInt() - *this; }
    ModInt pow(long long n) const {
        ModInt res(1), mul = *this;
        while(n > 0) {
            if (n & 1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    ModInt inv() const {
        long long a = m_value, b = m, u = 1, v = 0;
        while(b) {
            long long t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return ModInt(u);
    }
    friend inline constexpr ModInt operator +(const ModInt& lhs, const ModInt& rhs) { return ModInt(lhs) += rhs; }
    friend inline constexpr ModInt operator -(const ModInt& lhs, const ModInt& rhs) { return ModInt(lhs) -= rhs; }
    friend inline constexpr ModInt operator *(const ModInt& lhs, const ModInt& rhs) { return ModInt(lhs) *= rhs; }
    friend inline constexpr ModInt operator /(const ModInt& lhs, const ModInt& rhs) { return ModInt(lhs) /= rhs; }
    friend inline constexpr bool operator==(const ModInt& lhs, const ModInt& rhs) { return lhs.m_value == rhs.m_value; }
    friend inline constexpr bool operator!=(const ModInt& lhs, const ModInt& rhs) { return !(lhs == rhs); }
    friend inline std::ostream& operator <<(std::ostream& out, const ModInt& mint) { return out << mint.value(); }
    friend inline std::istream& operator >>(std::istream& is, ModInt& mint) {
        long long tmp;
        is >> tmp;
        mint = ModInt(tmp);
        return is;
    }
};

template<class T>
T Legendre(const long long n, const long long p) noexcept {
    T res = 0;
    long long q = p;
    while (true) {
        auto diff = n / q;
        if (diff == 0) {
            break;
        }
        res += diff;
        q *= p;
    }
    return res;
}

template<class T>
T Factorial(long long n) {
    T res = 1;
    for (int i = 1; i <= n; i++) {
        res *= i;
    }
    return res;
}

int main() {
    using mint = ModInt<MOD>;
    ll n, p;
    cin >> n >> p;
    auto k = Legendre<mint>(n, p);
    auto e = Factorial<mint>(n);
    ModInt<MOD - 1> a = 1;
    for (int i = 1; i <= n; i++) {
        a *= i;
    }
    cout << k * e.pow(a.value()) << endl;
}
0