結果
問題 | No.2326 Factorial to the Power of Factorial to the... |
ユーザー |
|
提出日時 | 2023-05-28 14:45:53 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 10 ms / 2,000 ms |
コード長 | 4,380 bytes |
コンパイル時間 | 2,214 ms |
コンパイル使用メモリ | 194,284 KB |
最終ジャッジ日時 | 2025-02-13 12:01:59 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#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;}