結果

問題 No.1750 ラムドスウイルスの感染拡大-hard
ユーザー togatogatogatoga
提出日時 2021-11-19 21:53:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 16,026 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 162,760 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-30 08:14:41
合計ジャッジ時間 7,140 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 11 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 75 ms
4,380 KB
testcase_09 AC 75 ms
4,384 KB
testcase_10 AC 76 ms
4,380 KB
testcase_11 AC 57 ms
4,384 KB
testcase_12 AC 80 ms
4,380 KB
testcase_13 AC 76 ms
4,384 KB
testcase_14 AC 399 ms
4,380 KB
testcase_15 AC 408 ms
4,380 KB
testcase_16 AC 412 ms
4,380 KB
testcase_17 AC 417 ms
4,380 KB
testcase_18 AC 432 ms
4,384 KB
testcase_19 AC 413 ms
4,384 KB
testcase_20 AC 285 ms
4,384 KB
testcase_21 AC 326 ms
4,380 KB
testcase_22 AC 57 ms
4,380 KB
testcase_23 AC 393 ms
4,380 KB
testcase_24 AC 44 ms
4,380 KB
testcase_25 AC 107 ms
4,384 KB
testcase_26 AC 34 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 56 ms
4,384 KB
testcase_31 AC 53 ms
4,380 KB
testcase_32 AC 51 ms
4,380 KB
testcase_33 AC 50 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use matrix::MatrixTrait;

use crate::mod_int::ModInt998244353;

pub mod utils {
    const DYX: [(isize, isize); 8] = [
        (0, 1),
        (1, 0),
        (0, -1),
        (-1, 0),
        (1, 1),
        (-1, 1),
        (1, -1),
        (-1, -1),
    ];
    pub fn try_adj(y: usize, x: usize, dir: usize, h: usize, w: usize) -> Option<(usize, usize)> {
        let ny = y as isize + DYX[dir].0;
        let nx = x as isize + DYX[dir].1;
        if ny >= 0 && nx >= 0 && ny < h as isize && nx < w as isize {
            Some((ny as usize, nx as usize))
        } else {
            None
        }
    }
}
/// Modular Integer
/// NOTE
/// If a modular isn't prime, you can't div.
/// If you want to calculate a combination and permutation, you have to use `mod_comb`.
pub mod mod_int {
    use std::marker::PhantomData;
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
    #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
    pub struct Mod1000000007;
    #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
    pub struct Mod998244353;
    pub trait Modulus: Copy + Eq + Copy {
        const VALUE: u32;
    }
    impl Modulus for Mod1000000007 {
        const VALUE: u32 = 1000000007;
    }
    impl Modulus for Mod998244353 {
        const VALUE: u32 = 998244353;
    }
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub struct ModInt<
        T: Copy + Clone + Add + AddAssign + Mul + MulAssign + Sub + SubAssign,
        M: Modulus,
    > {
        pub val: T,
        phantom: std::marker::PhantomData<fn() -> M>,
    }
    /// Implementation macros
    #[warn(unused_macros)]
    macro_rules ! mod_int_impl {($ ($ t : ty ) * ) => ($ (impl < M : Modulus > ModInt <$ t , M > {pub fn new (x : $ t ) -> Self {ModInt {val : x % M :: VALUE as $ t , phantom : PhantomData } } pub fn pow (self , e : usize ) -> ModInt <$ t , M > {let mut result = ModInt ::<$ t , M >:: new (1 ) ; let mut cur = self ; let mut e = e ; while e > 0 {if e & 1 == 1 {result *= cur ; } cur *= cur ; e >>= 1 ; } result } } impl < M : Modulus > Add < ModInt <$ t , M >> for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn add (self , rhs : ModInt <$ t , M > ) -> ModInt <$ t , M > {self + rhs . val } } impl < M : Modulus > Add < ModInt <$ t , M >> for $ t {type Output = ModInt <$ t , M >; fn add (self , rhs : ModInt <$ t , M > ) -> ModInt <$ t , M > {let x = self % M :: VALUE as $ t ; let val = (x + rhs . val ) % M :: VALUE as $ t ; ModInt {val , phantom : PhantomData } } } impl < M : Modulus > Add <$ t > for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn add (self , rhs : $ t ) -> ModInt <$ t , M > {let x = rhs % M :: VALUE as $ t ; let val = (self . val + x ) % M :: VALUE as $ t ; ModInt {val , phantom : PhantomData } } } impl < M : Modulus > Sub < ModInt <$ t , M >> for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn sub (self , rhs : ModInt <$ t , M > ) -> ModInt <$ t , M > {self - rhs . val } } impl < M : Modulus > Sub < ModInt <$ t , M >> for $ t {type Output = ModInt <$ t , M >; fn sub (self , rhs : ModInt <$ t , M > ) -> ModInt <$ t , M > {let val = self % M :: VALUE as $ t ; ModInt {val , phantom : PhantomData } - rhs } } impl < M : Modulus > Sub <$ t > for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn sub (self , rhs : $ t ) -> ModInt <$ t , M > {let rhs = if rhs >= M :: VALUE as $ t {rhs % M :: VALUE as $ t } else {rhs } ; let val = if self . val < rhs {self . val + M :: VALUE as $ t - rhs } else {self . val - rhs } ; ModInt {val , phantom : PhantomData } } } impl < M : Modulus > AddAssign < ModInt <$ t , M >> for ModInt <$ t , M > {fn add_assign (& mut self , rhs : ModInt <$ t , M > ) {* self = * self + rhs ; } } impl < M : Modulus > AddAssign <$ t > for ModInt <$ t , M > {fn add_assign (& mut self , rhs : $ t ) {* self = * self + rhs ; } } impl < M : Modulus > SubAssign < ModInt <$ t , M >> for ModInt <$ t , M > {fn sub_assign (& mut self , rhs : ModInt <$ t , M > ) {* self = * self - rhs ; } } impl < M : Modulus > SubAssign <$ t > for ModInt <$ t , M > {fn sub_assign (& mut self , rhs : $ t ) {* self = * self - rhs ; } } impl < M : Modulus > Div <$ t > for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn div (self , mut rhs : $ t ) -> ModInt <$ t , M > {if rhs >= M :: VALUE as $ t {rhs %= M :: VALUE as $ t ; } self * ModInt {val : rhs , phantom : PhantomData } . pow ((M :: VALUE - 2 ) as usize ) } } impl < M : Modulus > Div < ModInt <$ t , M >> for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn div (self , rhs : ModInt <$ t , M > ) -> ModInt <$ t , M > {self / rhs . val } } impl < M : Modulus > DivAssign <$ t > for ModInt <$ t , M > {fn div_assign (& mut self , rhs : $ t ) {* self = * self / rhs } } impl < M : Modulus > DivAssign < ModInt <$ t , M >> for ModInt <$ t , M > {fn div_assign (& mut self , rhs : ModInt <$ t , M > ) {* self = * self / rhs } } impl < M : Modulus > Mul < ModInt <$ t , M >> for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn mul (self , rhs : ModInt <$ t , M > ) -> ModInt <$ t , M > {self * rhs . val } } impl < M : Modulus > Mul < ModInt <$ t , M >> for $ t {type Output = ModInt <$ t , M >; fn mul (self , rhs : ModInt <$ t , M > ) -> ModInt <$ t , M > {rhs * self } } impl < M : Modulus > Mul <$ t > for ModInt <$ t , M > {type Output = ModInt <$ t , M >; fn mul (self , rhs : $ t ) -> ModInt <$ t , M > {ModInt {val : self . val * (rhs % M :: VALUE as $ t ) % M :: VALUE as $ t , phantom : PhantomData } } } impl < M : Modulus > MulAssign < ModInt <$ t , M >> for ModInt <$ t , M > {fn mul_assign (& mut self , rhs : ModInt <$ t , M > ) {* self = * self * rhs ; } } impl < M : Modulus > MulAssign <$ t > for ModInt <$ t , M > {fn mul_assign (& mut self , rhs : $ t ) {* self = * self * rhs ; } } impl < M : Modulus > Default for ModInt <$ t , M > {fn default () -> ModInt <$ t , M > {ModInt {val : 0 , phantom : PhantomData } } } ) * ) }
    mod_int_impl ! (usize i64 u64 i128 );
    #[allow(dead_code)]
    pub type ModInt1000000007 = ModInt<i64, Mod1000000007>;
    pub type ModInt998244353 = ModInt<i64, Mod998244353>;
}

pub mod matrix {
    use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
    pub trait MatrixTrait: Default + Clone + Copy {}
    #[derive(Clone, Default, Debug)]
    pub struct Matrix<T> {
        data: Vec<T>,
        height: usize,
        width: usize,
    }
    impl<T: Default + Copy> Matrix<T> {
        pub fn new(h: usize, w: usize) -> Matrix<T> {
            assert!(h != 0 && w != 0);
            Matrix {
                data: vec![T::default(); h * w],
                height: h,
                width: w,
            }
        }
        pub fn width(&self) -> usize {
            self.width
        }
        pub fn height(&self) -> usize {
            self.height
        }
        pub fn get(&self, y: usize, x: usize) -> &T {
            &self.data[y * self.width + x]
        }
        pub fn get_mut(&mut self, y: usize, x: usize) -> &mut T {
            &mut self.data[y * self.width + x]
        }
    }
    impl<T: Default + Copy> From<Vec<Vec<T>>> for Matrix<T> {
        fn from(x: Vec<Vec<T>>) -> Self {
            let h = x.len();
            let w = x[0].len();
            let mut matrix = Matrix::new(h, w);
            for i in 0..h {
                for j in 0..w {
                    *matrix.get_mut(i, j) = x[i][j];
                }
            }
            matrix
        }
    }
    impl<T: MatrixTrait + Mul<Output = T> + AddAssign + MulAssign> Matrix<T> {
        /// A^k
        /// O(hw^2logK)
        pub fn pow(&self, mut k: usize, one: T) -> Self {
            assert!(self.height() == self.width());
            let mut result = Self::new(self.height(), self.width());
            for i in 0..self.height() {
                *result.get_mut(i, i) = one;
            }
            let mut s = self.clone();
            while k > 0 {
                if k & 1 == 1 {
                    result = result.dot(&s);
                }
                s = s.dot(&s);
                k >>= 1;
            }
            result
        }
        /// Matrix A*B = C
        /// (A.height, A.width) = (n1, m1)
        /// (B.height, B.width) = (n2, m2)
        /// (m1 == n2)
        /// multiple A by B and return a new matrix(n1, m2) C = A * B
        /// O(A.height * A.width * B.width)
        pub fn dot(&self, rhs: &Matrix<T>) -> Matrix<T> {
            assert!(self.width() == rhs.height());
            let mut matrix = Matrix::new(self.height(), rhs.width());
            for i in 0..self.height() {
                for k in 0..self.width() {
                    for j in 0..rhs.width() {
                        *matrix.get_mut(i, j) += *self.get(i, k) * *rhs.get(k, j);
                    }
                }
            }
            matrix
        }
        /// A*d
        pub fn mul(&self, d: T) -> Matrix<T> {
            let mut matrix = self.clone();
            matrix.data.iter_mut().for_each(|x| *x *= d);
            matrix
        }
        /// A *= d
        pub fn mul_assign(&mut self, d: T) {
            self.data.iter_mut().for_each(|x| *x *= d);
        }
    }
    impl<T: MatrixTrait + Add<Output = T> + AddAssign> Matrix<T> {
        /// A + B
        pub fn add_mat(&self, rhs: &Matrix<T>) -> Matrix<T> {
            assert!(self.height() == rhs.height() && self.width() == rhs.width());
            let data: Vec<_> = self
                .data
                .iter()
                .zip(rhs.data.iter())
                .map(|(x, y)| *x + *y)
                .collect();
            Matrix {
                data,
                height: self.height,
                width: self.width,
            }
        }
        /// A += B
        pub fn add_mat_assign(&mut self, rhs: &Matrix<T>) {
            assert!(self.height() == rhs.height() && self.width() == rhs.width());
            self.data
                .iter_mut()
                .zip(rhs.data.iter())
                .for_each(|(x, y)| *x += *y);
        }
    }
    impl<T: MatrixTrait + Sub<Output = T> + SubAssign> Matrix<T> {
        /// A - B
        pub fn sub_mat(self, rhs: &Matrix<T>) -> Matrix<T> {
            assert!(self.height() == rhs.height() && self.width() == rhs.width());
            let data: Vec<_> = self
                .data
                .iter()
                .zip(rhs.data.iter())
                .map(|(x, y)| *x - *y)
                .collect();
            Matrix {
                data,
                height: self.height,
                width: self.width,
            }
        }
        /// A -= B
        pub fn sub_mat_assign(&mut self, rhs: &Matrix<T>) {
            assert!(self.height() == rhs.height() && self.width() == rhs.width());
            self.data
                .iter_mut()
                .zip(rhs.data.iter())
                .for_each(|(x, y)| *x -= *y);
        }
    }
    /// impl MatrixTrait for * {}
    impl MatrixTrait for i32 {}
    impl MatrixTrait for i64 {}
    impl MatrixTrait for i128 {}
    impl MatrixTrait for u32 {}
    impl MatrixTrait for u64 {}
    impl MatrixTrait for usize {}
}
impl MatrixTrait for ModInt998244353 {}
#[derive(Default)]
/// NOTE
/// declare variables to reduce the number of parameters for dp and dfs etc.
pub struct Solver {}
impl Solver {
    pub fn solve(&mut self) {
        let stdin = std::io::stdin();
        let mut scn = fastio::Scanner::new(stdin.lock());
        let n: usize = scn.read();
        let m: usize = scn.read();
        let t: usize = scn.read();
        let mut mat = matrix::Matrix::new(n, n);
        for _ in 0..m {
            let s: usize = scn.read();
            let t: usize = scn.read();
            *mat.get_mut(s, t) = ModInt998244353::new(1);
            *mat.get_mut(t, s) = ModInt998244353::new(1);
        }
        let result = mat.pow(t, ModInt998244353::new(1));
        
        println!("{}", result.get(0, 0).val);
    }
}
pub mod fastio {
    use std::collections::VecDeque;
    use std::io::BufWriter;
    use std::io::Write;
    pub struct Writer<W: std::io::Write> {
        writer: std::io::BufWriter<W>,
    }
    impl<W: std::io::Write> Writer<W> {
        pub fn new(write: W) -> Writer<W> {
            Writer {
                writer: BufWriter::new(write),
            }
        }
        pub fn flush(&mut self) {
            self.writer.flush().unwrap();
        }
        pub fn write<S: std::string::ToString>(&mut self, s: S) {
            self.writer.write(s.to_string().as_bytes()).unwrap();
        }
        pub fn writeln<S: std::string::ToString>(&mut self, s: S) {
            self.write(s);
            self.write('\n');
        }
    }
    pub struct Scanner<R> {
        stdin: R,
        buffer: VecDeque<String>,
    }
    impl<R: std::io::BufRead> Scanner<R> {
        pub fn new(s: R) -> Scanner<R> {
            Scanner {
                stdin: s,
                buffer: VecDeque::new(),
            }
        }
        pub fn read<T: std::str::FromStr>(&mut self) -> T {
            while self.buffer.is_empty() {
                let line = self.read_line();
                for w in line.split_whitespace() {
                    self.buffer.push_back(String::from(w));
                }
            }
            self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn read_line(&mut self) -> String {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            line.trim_end().to_string()
        }
        pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.read()).collect()
        }
        pub fn chars(&mut self) -> Vec<char> {
            self.read::<String>().chars().collect()
        }
    }
}
pub mod macros {
    #[macro_export]
    #[allow(unused_macros)]
    macro_rules ! max {($ x : expr ) => ($ x ) ; ($ x : expr , $ ($ y : expr ) ,+ ) => {std :: cmp :: max ($ x , max ! ($ ($ y ) ,+ ) ) } }
    #[macro_export]
    #[allow(unused_macros)]
    macro_rules ! min {($ x : expr ) => ($ x ) ; ($ x : expr , $ ($ y : expr ) ,+ ) => {std :: cmp :: min ($ x , min ! ($ ($ y ) ,+ ) ) } }
    #[macro_export]
    #[allow(unused_macros)]
    /// Display a line of variables
    macro_rules ! echo {() => {{use std :: io :: {self , Write } ; writeln ! (io :: stderr () , "{}:" , line ! () ) . unwrap () ; } } ; ($ e : expr , $ ($ es : expr ) ,+ $ (, ) ? ) => {{use std :: io :: {self , Write } ; write ! (io :: stderr () , "{}:" , line ! () ) . unwrap () ; write ! (io :: stderr () , " {} = {:?}" , stringify ! ($ e ) , $ e ) . unwrap () ; $ (write ! (io :: stderr () , " {} = {:?}" , stringify ! ($ es ) , $ es ) . unwrap () ; ) + writeln ! (io :: stderr () ) . unwrap () ; } } ; ($ e : expr ) => {{use std :: io :: {self , Write } ; let result = $ e ; writeln ! (io :: stderr () , "{}: {} = {:?}" , line ! () , stringify ! ($ e ) , result ) . unwrap () ; result } } ; }
    #[macro_export]
    #[allow(unused_macros)]
    /// Display a line of variables with colors
    macro_rules ! cecho {() => {{use std :: io :: {self , Write } ; writeln ! (io :: stderr () , "\x1b[31;1m{}\x1b[m:" , line ! () ) . unwrap () ; } } ; ($ e : expr , $ ($ es : expr ) ,+ $ (, ) ? ) => {{use std :: io :: {self , Write } ; write ! (io :: stderr () , "\x1b[31;1m{}\x1b[m:" , line ! () ) . unwrap () ; write ! (io :: stderr () , " \x1b[92;1m{}\x1b[m = {:?}" , stringify ! ($ e ) , $ e ) . unwrap () ; $ (write ! (io :: stderr () , " \x1b[92;1m{}\x1b[m = {:?}" , stringify ! ($ es ) , $ es ) . unwrap () ; ) + writeln ! (io :: stderr () ) . unwrap () ; } } ; ($ e : expr ) => {{use std :: io :: {self , Write } ; let result = $ e ; writeln ! (io :: stderr () , "\x1b[31;1m{}\x1b[m: \x1b[92;1m{}\x1b[m = {:?}" , line ! () , stringify ! ($ e ) , result ) . unwrap () ; result } } ; }
}
fn main() {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024)
        .spawn(|| Solver::default().solve())
        .unwrap()
        .join()
        .unwrap();
}
0