結果

問題 No.1138 No Bingo!
ユーザー ikdikd
提出日時 2020-08-08 21:28:19
言語 Rust
(1.77.0)
結果
AC  
実行時間 28 ms / 3,000 ms
コード長 5,678 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 176,312 KB
実行使用メモリ 8,084 KB
最終ジャッジ日時 2024-04-10 05:46:43
合計ジャッジ時間 2,153 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 14 ms
6,944 KB
testcase_04 AC 28 ms
8,072 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 16 ms
6,940 KB
testcase_10 AC 25 ms
7,296 KB
testcase_11 AC 27 ms
8,084 KB
testcase_12 AC 9 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 27 ms
7,680 KB
testcase_15 AC 12 ms
6,944 KB
testcase_16 AC 24 ms
7,408 KB
testcase_17 AC 23 ms
7,276 KB
testcase_18 AC 21 ms
7,040 KB
testcase_19 AC 17 ms
6,944 KB
testcase_20 AC 23 ms
7,308 KB
testcase_21 AC 24 ms
6,940 KB
testcase_22 AC 22 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 16 ms
6,940 KB
testcase_25 AC 10 ms
6,944 KB
testcase_26 AC 9 ms
6,944 KB
testcase_27 AC 14 ms
6,940 KB
testcase_28 AC 9 ms
6,940 KB
testcase_29 AC 9 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused imports: `AddAssign`, `Div`
  --> main.rs:56:25
   |
56 |     use std::ops::{Add, AddAssign, Div, Mul, Rem, Sub};
   |                         ^^^^^^^^^  ^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `i`
   --> main.rs:219:9
    |
219 |     for i in 0..10 {
    |         ^ help: if this is intentional, prefix it with an underscore: `_i`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: function `f` is never used
   --> main.rs:190:4
    |
190 | fn f(n: usize) -> i32 {
    |    ^
    |
    = note: `#[warn(dead_code)]` on by default

warning: 3 warnings emitted

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

mod mint {
    use std::ops::{Add, AddAssign, Div, Mul, Rem, Sub};

    #[derive(Copy, Clone)]
    pub struct Mint<T> {
        x: T,
        mo: T,
    }
    impl<T> Mint<T>
    where
        T: Copy,
        T: Rem<T, Output = T>,
        T: Add<T, Output = T>,
    {
        pub fn new(x: T, mo: T) -> Mint<T> {
            Mint {
                x: (x % mo + mo) % mo,
                mo: mo,
            }
        }
    }
    impl<T> Mint<T>
    where
        T: Copy,
    {
        pub fn val(&self) -> T {
            self.x
        }
        pub fn mo(&self) -> T {
            self.mo
        }
    }
    impl<T> Add<T> for Mint<T>
    where
        T: Copy,
        T: Add<Output = T>,
        T: Rem<Output = T>,
    {
        type Output = Mint<T>;
        fn add(self, rhs: T) -> Mint<T> {
            Mint::new(self.val() + rhs % self.mo(), self.mo())
        }
    }
    impl<T> Add<Mint<T>> for Mint<T>
    where
        T: Copy,
        Mint<T>: Add<T, Output = Mint<T>>,
    {
        type Output = Mint<T>;
        fn add(self, rhs: Mint<T>) -> Mint<T> {
            self + rhs.val()
        }
    }
    impl<T> Sub<T> for Mint<T>
    where
        T: Copy,
        T: Add<Output = T>,
        T: Sub<Output = T>,
        T: Rem<Output = T>,
    {
        type Output = Mint<T>;
        fn sub(self, rhs: T) -> Mint<T> {
            Mint::new(self.val() + self.mo() - rhs % self.mo(), self.mo())
        }
    }
    impl<T> Sub<Mint<T>> for Mint<T>
    where
        T: Copy,
        Mint<T>: Sub<T, Output = Mint<T>>,
    {
        type Output = Mint<T>;
        fn sub(self, rhs: Mint<T>) -> Mint<T> {
            self - rhs.val()
        }
    }
    impl<T> Mul<T> for Mint<T>
    where
        T: Copy,
        T: Add<Output = T>,
        T: Mul<Output = T>,
        T: Rem<Output = T>,
    {
        type Output = Mint<T>;
        fn mul(self, rhs: T) -> Mint<T> {
            Mint::new(self.val() * rhs % self.mo(), self.mo())
        }
    }
    impl<T> Mul<Mint<T>> for Mint<T>
    where
        T: Copy,
        Mint<T>: Mul<T, Output = Mint<T>>,
    {
        type Output = Mint<T>;
        fn mul(self, rhs: Mint<T>) -> Mint<T> {
            self * rhs.val()
        }
    }
    impl<T> std::fmt::Display for Mint<T>
    where
        T: Copy + std::fmt::Display,
    {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.val())
        }
    }
}

use mint::Mint;

pub trait NextPermutation {
    fn next_permutation(&mut self) -> bool;
}

impl<T: Ord> NextPermutation for [T] {
    fn next_permutation(&mut self) -> bool {
        if self.is_empty() {
            return false;
        }
        let mut i = self.len() - 1;
        while i > 0 && self[i - 1] >= self[i] {
            i -= 1;
        }
        if i == 0 {
            return false;
        }
        let mut j = self.len() - 1;
        while self[i - 1] >= self[j] {
            j -= 1;
        }
        self.swap(i - 1, j);
        self[i..].reverse();
        return true;
    }
}

fn f(n: usize) -> i32 {
    let mut v = (0..n).collect::<Vec<_>>();
    let mut res = 0;
    loop {
        if v.iter().zip(0..n).all(|(&x, i)| x != i && x != n - i - 1) {
            res += 1;
        }
        if !v.next_permutation() {
            break;
        }
    }
    res
}

fn main() {
    input! {
        n: usize,
    }
    let mo = 998244353;
    let mut a = Mint::new(1, mo);
    for i in 1..=n {
        a = a * i;
    }
    let mut b = vec![Mint::new(0, mo); n + 2];
    b[2] = Mint::new(1, mo);
    for i in 3..=n {
        b[i] = (b[i - 1] + b[i - 2]) * (i - 1);
    }
    let b = b[n];
    for i in 0..10 {
        // println!("{} {}", i, f(i));
        // 0 1
        // 1 0
        // 2 0
        // 3 0
        // 4 4
        // 5 16
        // 6 80
        // 7 672
        // 8 4752
        // 9 48768
        // https://oeis.org/A003471
    }
    let mut c = vec![Mint::new(0, mo); n + 2];
    c[0] = Mint::new(1, mo);
    for i in 4..=n {
        if i % 2 == 0 {
            c[i] = c[i - 1] * (i - 1) + c[i - 4] * 2 * (i - 2);
        } else {
            c[i] = c[i - 1] * (i - 1) + c[i - 2] * 2 * (i - 1);
        }
    }
    let c = c[n];
    println!("{}", a - b * 2 + c);
}
0