結果

問題 No.2079 aaabbc
ユーザー StrorkisStrorkis
提出日時 2022-09-25 22:25:25
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 7,306 bytes
コンパイル時間 10,801 ms
コンパイル使用メモリ 401,340 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-01 23:38:21
合計ジャッジ時間 11,536 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 0 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 0 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod input {
    use std::io::{BufRead, ErrorKind};

    pub trait ReadBytes {
        fn read_bytes(&mut self) -> Vec<u8>;
    }

    impl<R: BufRead> ReadBytes for R {
        #[inline]
        fn read_bytes(&mut self) -> Vec<u8> {
            let mut res = Vec::new();
            loop {
                let buf = match self.fill_buf() {
                    Ok(buf) => buf,
                    Err(e) if e.kind() == ErrorKind::Interrupted => continue,
                    Err(e) => panic!("{}", e), 
                };
                let (done, used, buf) = match buf.iter().position(u8::is_ascii_whitespace) {
                    Some(i) => (i > 0 || res.len() > 0, i + 1, &buf[..i]),
                    None => (buf.is_empty(), buf.len(), buf),
                };
                res.extend_from_slice(buf);
                self.consume(used);
                if done {
                    return res;
                }
            }
        }
    }

    #[macro_export]
    macro_rules! read {
        ($r:expr, [$t:tt; $n:expr]) => ((0..$n).map(|_| read!($r, $t)).collect::<Vec<_>>());
        ($r:expr, [$t:tt]) => (read!($r, [$t; read!($r, usize)]));
        ($r:expr, ($($t:tt),*)) => (($(read!($r, $t)),*));
        ($r:expr, Usize1) => (read!($r, usize) - 1);
        ($r:expr, String) => (String::from_utf8(read!($r, Bytes)).unwrap());
        ($r:expr, Bytes) => ($r.read_bytes());
        ($r:expr, $t:ty) => (read!($r, String).parse::<$t>().unwrap());
    }

    #[macro_export]
    macro_rules! input {
        ($r:expr, $($($v:ident)* : $t:tt),* $(,)?) => {
            $(let $($v)* = read!($r, $t);)*
        };
    }
}

pub mod mod_int {
    use std::fmt;
    use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign};
    use std::iter::{Sum, Product};

    macro_rules! forward_ref_binop {
        (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
            impl<'a> $imp<$u> for &'a $t {
                type Output = <$t as $imp<$u>>::Output;
    
                #[inline]
                fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
                    $imp::$method(*self, other)
                }
            }
    
            impl $imp<&$u> for $t {
                type Output = <$t as $imp<$u>>::Output;
    
                #[inline]
                fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
                    $imp::$method(self, *other)
                }
            }
    
            impl $imp<&$u> for &$t {
                type Output = <$t as $imp<$u>>::Output;
    
                #[inline]
                fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
                    $imp::$method(*self, *other)
                }
            }
        };
    }

    macro_rules! forward_ref_op_assign {
        (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
            impl $imp<&$u> for $t {
                #[inline]
                fn $method(&mut self, other: &$u) {
                    $imp::$method(self, *other);
                }
            }
        };
    }

    #[derive(Clone, Copy, Debug, Default)]
    pub struct ModInt(u64);

    impl ModInt {
        pub const MOD: u64 = 998_244_353;

        pub const ZERO: ModInt = Self(0);

        pub const ONE: ModInt = Self(1);

        pub fn new(mut x: u64) -> Self {
            if x >= Self::MOD {
                x %= Self::MOD;
            }
            Self(x)
        }
    }

    impl From<usize> for ModInt {
        fn from(x: usize) -> Self {
            Self::new(x as _)
        }
    }

    impl From<i64> for ModInt {
        fn from(mut x: i64) -> Self {
            let m = Self::MOD as i64;
            if x.abs() >= m {
                x %= m;
            }
            if x < 0 {
                x += m
            }
            Self::new(x as _)
        }
    }

    impl Add for ModInt {
        type Output = Self;

        #[inline]
        fn add(self, other: Self) -> Self {
            let mut x = self.0 + other.0;
            if x >= Self::MOD {
                x -= Self::MOD;
            }
            Self(x)
        }
    }

    forward_ref_binop! { impl Add, add for ModInt, ModInt }

    impl AddAssign for ModInt {
        #[inline]
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }

    forward_ref_op_assign! { impl AddAssign, add_assign for ModInt, ModInt }

    impl Sub for ModInt {
        type Output = Self;

        #[inline]
        fn sub(mut self, other: Self) -> Self {
            if self.0 < other.0 {
                self.0 += Self::MOD;
            }
            Self(self.0 - other.0)
        }
    }

    forward_ref_binop! { impl Sub, sub for ModInt, ModInt }

    impl SubAssign for ModInt {
        #[inline]
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }

    forward_ref_op_assign! { impl SubAssign, sub_assign for ModInt, ModInt }

    impl Mul for ModInt {
        type Output = Self;

        #[inline]
        fn mul(self, other: Self) -> Self {
            Self::new(self.0 * other.0)
        }
    }

    forward_ref_binop! { impl Mul, mul for ModInt, ModInt }

    impl MulAssign for ModInt {
        #[inline]
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }

    forward_ref_op_assign! { impl MulAssign, mul_assign for ModInt, ModInt }

    impl Sum for ModInt {
        fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
            iter.fold(
                ModInt::ZERO,
                |a, b| a + b,
            )
        }
    }

    impl Product for ModInt {
        fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
            iter.fold(
                ModInt::ONE,
                |a, b| a * b,
            )
        }
    }

    impl<'a> Sum<&'a ModInt> for ModInt {
        fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
            iter.fold(
                ModInt::ZERO,
                |a, b| a + b,
            )
        }
    }

    impl<'a> Product<&'a ModInt> for ModInt {
        fn product<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
            iter.fold(
                ModInt::ONE,
                |a, b| a * b,
            )
        }
    }

    impl fmt::Display for ModInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl ModInt {
        pub fn pow(mut self, mut n: u64) -> Self {
            let mut res = Self::ONE;
            while n > 0 {
                if n & 1 == 1 {
                    res *= self;
                }
                self *= self;
                n >>= 1;
            }
            res
        }
        
        pub fn inv(self) -> Self {
            self.pow(Self::MOD - 2)
        }
    }
}

use input::ReadBytes;
use mod_int::ModInt;

fn run<R: std::io::BufRead, W: std::io::Write>(reader: &mut R, _writer: &mut W) {
    input! {
        reader,
        n: u64,
    }

    let ans = ModInt::new(3).pow(n);
    println!("{}", ans);
}

fn main() {
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let mut reader = std::io::BufReader::new(stdin.lock());
    let mut writer = std::io::BufWriter::new(stdout.lock());
    run(&mut reader, &mut writer);
}
0