結果

問題 No.1302 Random Tree Score
ユーザー noshi91noshi91
提出日時 2020-11-27 21:44:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 12 ms / 3,000 ms
コード長 5,436 bytes
コンパイル時間 4,322 ms
コンパイル使用メモリ 154,800 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-01 05:12:02
合計ジャッジ時間 5,270 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 7 ms
4,380 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 11 ms
4,380 KB
testcase_06 AC 11 ms
4,380 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 12 ms
4,380 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 10 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 12 ms
4,384 KB
testcase_16 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `perm` is never used
   --> Main.rs:205:12
    |
205 |     pub fn perm(&self, n: usize, r: usize) -> Fp {
    |            ^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: associated function `homo` is never used
   --> Main.rs:213:12
    |
213 |     pub fn homo(&self, n: usize, r: usize) -> Fp {
    |            ^^^^

warning: 2 warnings emitted

ソースコード

diff #

use std::io::{self, Read as _, Write as _};

struct Scanner<'a>(std::str::SplitWhitespace<'a>);

impl<'a> Scanner<'a> {
    fn new(s: &'a str) -> Self {
        Self(s.split_whitespace())
    }

    fn next<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let s = self.0.next().expect("found EOF");
        match s.parse() {
            Ok(v) => v,
            Err(msg) => {
                println!(
                    "parse error. T = {}, s = \"{}\": {:?}",
                    std::any::type_name::<T>(),
                    s,
                    msg
                );
                panic!()
            }
        }
    }
}

pub mod fp {
    use std::convert::From;
    use std::iter;
    use std::ops;

    pub const P: u32 = 998244353;

    #[derive(Copy, Clone, Debug)]
    pub struct Fp(pub u32);

    impl Fp {
        pub fn pow(mut self, mut exp: u32) -> Fp {
            let mut res = Fp(1);
            while exp != 0 {
                if exp % 2 != 0 {
                    res *= self;
                }
                self *= self;
                exp /= 2;
            }
            res
        }
    }

    impl From<i64> for Fp {
        fn from(mut x: i64) -> Fp {
            x %= P as i64;
            if x < 0 {
                x += P as i64;
            }
            Fp(x as u32)
        }
    }

    impl iter::Product for Fp {
        fn product<I>(iter: I) -> Fp
        where
            I: Iterator<Item = Fp>,
        {
            iter.fold(Fp(1), |b, i| b * i)
        }
    }

    impl iter::Sum for Fp {
        fn sum<I>(iter: I) -> Fp
        where
            I: Iterator<Item = Fp>,
        {
            iter.fold(Fp(0), |b, i| b + i)
        }
    }

    impl ops::Add<Fp> for Fp {
        type Output = Fp;
        fn add(mut self, rhs: Fp) -> Fp {
            self += rhs;
            self
        }
    }

    impl ops::AddAssign<Fp> for Fp {
        fn add_assign(&mut self, rhs: Fp) {
            self.0 += rhs.0;
            if self.0 >= P {
                self.0 -= P;
            }
        }
    }

    impl ops::Mul<Fp> for Fp {
        type Output = Fp;
        fn mul(mut self, rhs: Fp) -> Fp {
            self *= rhs;
            self
        }
    }

    impl ops::MulAssign<Fp> for Fp {
        fn mul_assign(&mut self, rhs: Fp) {
            self.0 = (self.0 as u64 * rhs.0 as u64 % P as u64) as u32;
        }
    }

    impl ops::Neg for Fp {
        type Output = Fp;
        fn neg(self) -> Fp {
            Fp(match self.0 {
                0 => 0,
                s => P - s,
            })
        }
    }

    impl ops::Sub<Fp> for Fp {
        type Output = Fp;
        fn sub(mut self, rhs: Fp) -> Fp {
            self -= rhs;
            self
        }
    }

    impl ops::SubAssign<Fp> for Fp {
        fn sub_assign(&mut self, rhs: Fp) {
            if self.0 < rhs.0 {
                self.0 += P;
            }
            self.0 -= rhs.0;
        }
    }

    impl ops::Div<Fp> for Fp {
        type Output = Fp;
        fn div(self, rhs: Fp) -> Fp {
            self * Fp::pow(rhs, P - 2)
        }
    }

    impl ops::DivAssign<Fp> for Fp {
        fn div_assign(&mut self, rhs: Fp) {
            *self = *self / rhs;
        }
    }

    use std::str::FromStr;

    impl FromStr for Fp {
        type Err = <u32 as FromStr>::Err;
        fn from_str(s: &str) -> Result<Fp, <Fp as FromStr>::Err> {
            u32::from_str(s).map(|x| Fp(x))
        }
    }

    use std::fmt;

    impl fmt::Display for Fp {
        fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            self.0.fmt(f)
        }
    }
}

use fp::Fp;

struct FpUtility {
    fact_: Box<[Fp]>,
    inv_fact_: Box<[Fp]>,
}

impl FpUtility {
    pub fn new(n: usize) -> Self {
        let mut fact_ = vec![Fp(0); n + 1];
        let mut inv_fact_ = vec![Fp(0); n + 1];
        fact_[0] = Fp(1);
        for i in 1..=n {
            fact_[i] = fact_[i - 1] * Fp(i as u32);
        }
        inv_fact_[n] = Fp(1) / fact_[n];
        for i in (1..=n).rev() {
            inv_fact_[i - 1] = inv_fact_[i] * Fp(i as u32);
        }
        Self {
            fact_: fact_.into_boxed_slice(),
            inv_fact_: inv_fact_.into_boxed_slice(),
        }
    }

    pub fn fact(&self, n: usize) -> Fp {
        self.fact_[n]
    }

    pub fn inv_fact(&self, n: usize) -> Fp {
        self.inv_fact_[n]
    }

    pub fn perm(&self, n: usize, r: usize) -> Fp {
        self.fact_[n] * self.inv_fact_[n - r]
    }

    pub fn binom(&self, n: usize, r: usize) -> Fp {
        self.fact_[n] * self.inv_fact_[r] * self.inv_fact_[n - r]
    }

    pub fn homo(&self, n: usize, r: usize) -> Fp {
        self.fact_[n + r - 1] * self.inv_fact_[r] * self.inv_fact_[n - 1]
    }
}

fn main() {
    let mut stdin = String::new();
    std::io::stdin().read_to_string(&mut stdin).unwrap();
    let mut sc = Scanner::new(&stdin);
    let stdout = io::stdout();
    let mut stdout = io::BufWriter::new(stdout.lock());
    // writeln!(stdout, "");

    let n: usize = sc.next();
    let c = FpUtility::new(n);

    let mut ans = Fp(0);
    for k in 0..=n - 2 {
        let l = n - 2 - k;
        ans += c.binom(n, k) * Fp(n as u32).pow(l as u32) * c.inv_fact(l);
    }

    writeln!(
        stdout,
        "{}",
        ans * c.fact(n - 2) / Fp(n as u32).pow((n - 2) as u32)
    )
    .unwrap();

    stdout.flush().unwrap();
}
0