結果
問題 | No.1302 Random Tree Score |
ユーザー |
![]() |
提出日時 | 2020-11-27 22:08:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 31 ms / 3,000 ms |
コード長 | 4,856 bytes |
コンパイル時間 | 2,145 ms |
コンパイル使用メモリ | 196,512 KB |
最終ジャッジ日時 | 2025-01-16 07:30:36 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 14 |
ソースコード
#include <bits/stdc++.h>template <uint32_t Modulus>class ModularInt {using M = ModularInt;public:static_assert(int(Modulus) >= 1, "Modulus must be in the range [1, 2^31)");static constexpr int modulus() { return Modulus; }static M raw(uint32_t v) { return *reinterpret_cast<M*>(&v); }ModularInt() : v_(0) {}ModularInt(int64_t v) : v_((v %= Modulus) < 0 ? v + Modulus : v) {}template <class T>explicit operator T() const {return v_;}M& operator++() { return v_ = ++v_ == Modulus ? 0 : v_, *this; }M& operator--() { return --(v_ ? v_ : v_ = Modulus), *this; }M operator+() const { return *this; }M operator-() const { return raw(v_ ? Modulus - v_ : 0); }M& operator*=(M o) { return v_ = uint64_t(v_) * o.v_ % Modulus, *this; }M& operator/=(M o) {auto [inv, gcd] = extgcd(o.v_, Modulus);assert(gcd == 1);return *this *= inv;}M& operator+=(M o) {return v_ = int(v_ += o.v_ - Modulus) < 0 ? v_ + Modulus : v_, *this;}M& operator-=(M o) {return v_ = int(v_ -= o.v_) < 0 ? v_ + Modulus : v_, *this;}friend M operator++(M& a, int) { return std::exchange(a, ++M(a)); }friend M operator--(M& a, int) { return std::exchange(a, --M(a)); }friend M operator*(M a, M b) { return a *= b; }friend M operator/(M a, M b) { return a /= b; }friend M operator+(M a, M b) { return a += b; }friend M operator-(M a, M b) { return a -= b; }friend std::istream& operator>>(std::istream& is, M& x) {int64_t v;return is >> v, x = v, is;}friend std::ostream& operator<<(std::ostream& os, M x) { return os << x.v_; }friend bool operator==(M a, M b) { return a.v_ == b.v_; }friend bool operator!=(M a, M b) { return a.v_ != b.v_; }private:static std::array<int, 2> extgcd(int a, int b) {std::array x{1, 0};while (b) std::swap(x[0] -= a / b * x[1], x[1]), std::swap(a %= b, b);return {x[0], a};}uint32_t v_;};template <class T, class N>constexpr T power(T a, N n) {static_assert(std::is_integral_v<N>);assert(n >= 0);T res = n & 1 ? a : 1;while (n >>= 1) {a *= a;if (n & 1) res *= a;}return res;}#pragma region my_templatestruct Rep {struct I {int i;void operator++() { ++i; }int operator*() const { return i; }bool operator!=(I o) const { return i < *o; }};const int l_, r_;Rep(int l, int r) : l_(l), r_(r) {}Rep(int n) : Rep(0, n) {}I begin() const { return {l_}; }I end() const { return {r_}; }};struct Per {struct I {int i;void operator++() { --i; }int operator*() const { return i; }bool operator!=(I o) const { return i > *o; }};const int l_, r_;Per(int l, int r) : l_(l), r_(r) {}Per(int n) : Per(0, n) {}I begin() const { return {r_ - 1}; }I end() const { return {l_ - 1}; }};template <class F>struct Fix : private F {Fix(F f) : F(f) {}template <class... Args>decltype(auto) operator()(Args&&... args) const {return F::operator()(*this, std::forward<Args>(args)...);}};template <class T = int>T scan() {T res;std::cin >> res;return res;}template <class T, class U = T>bool chmin(T& a, U&& b) {return b < a ? a = std::forward<U>(b), true : false;}template <class T, class U = T>bool chmax(T& a, U&& b) {return a < b ? a = std::forward<U>(b), true : false;}#ifndef LOCAL#define DUMP(...) void(0)template <int OnlineJudge, int Local>constexpr int OjLocal = OnlineJudge;#endifusing namespace std;#define ALL(c) begin(c), end(c)#pragma endregionusing Mint = ModularInt<998244353>;constexpr int N = 1 << 20;auto fact = [] {std::vector<Mint> res(N + 1);res[0] = 1;for (int i = 1; i <= N; ++i) res[i] = i * res[i - 1];return res;}();auto ifact = [] {std::vector<Mint> res(N + 1);res[N] = 1 / fact[N];for (int i = N; i--;) res[i] = res[i + 1] * (i + 1);return res;}();Mint binom(int n, int k) {assert(n <= N);return 0 <= k and k <= n ? fact[n] * ifact[k] * ifact[n - k] : 0;}Mint homo(int n, int k) { return n or k ? binom(n + k - 1, k) : 1; }auto minv = [] {std::vector<Mint> res(N + 1);for (int i = 1; i <= N; ++i) res[i] = ifact[i] * fact[i - 1];return res;}();template <>Mint& Mint::operator/=(Mint o) {assert(o.v_);return *this *= o.v_ <= N ? minv[o.v_] : extgcd(o.v_, modulus())[0];}int main() {cin.tie(nullptr)->sync_with_stdio(false);cout << fixed << setprecision(20);int n = scan();Mint ans;for (int k : Rep(n - 1))ans += binom(n, k) * fact[n - 2] * ifact[n - k - 2] *power<Mint>(n, n - k - 2);ans /= power<Mint>(n, n - 2);cout << ans << '\n';}