結果

問題 No.1138 No Bingo!
ユーザー ikdikd
提出日時 2020-08-08 21:33:43
言語 Rust
(1.77.0)
結果
AC  
実行時間 18 ms / 3,000 ms
コード長 5,608 bytes
コンパイル時間 1,185 ms
コンパイル使用メモリ 171,376 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-04-10 05:55:10
合計ジャッジ時間 2,050 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 10 ms
5,376 KB
testcase_04 AC 18 ms
8,064 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 11 ms
5,632 KB
testcase_10 AC 17 ms
7,424 KB
testcase_11 AC 18 ms
7,808 KB
testcase_12 AC 7 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 18 ms
7,808 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 17 ms
7,424 KB
testcase_17 AC 16 ms
7,296 KB
testcase_18 AC 16 ms
6,912 KB
testcase_19 AC 12 ms
5,632 KB
testcase_20 AC 17 ms
7,424 KB
testcase_21 AC 16 ms
7,040 KB
testcase_22 AC 15 ms
6,656 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 8 ms
5,376 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 10 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 6 ms
5,376 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:216:9
    |
216 |     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:187:4
    |
187 | 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,
    {
        pub fn new(x: T, mo: T) -> Mint<T> {
            Mint { x, 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(), 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(),
                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: 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(), 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