結果

問題 No.535 自然数の収納方法
ユーザー koba-e964koba-e964
提出日時 2021-09-29 09:12:58
言語 Rust
(1.77.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 3,571 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 146,916 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-23 06:48:00
合計ジャッジ時間 1,900 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 23 ms
4,376 KB
testcase_09 AC 29 ms
4,380 KB
testcase_10 AC 34 ms
4,380 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 14 ms
4,384 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 11 ms
4,376 KB
testcase_15 AC 35 ms
4,384 KB
testcase_16 AC 23 ms
4,380 KB
testcase_17 AC 38 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 38 ms
4,376 KB
testcase_20 AC 39 ms
4,376 KB
testcase_21 AC 39 ms
4,380 KB
testcase_22 AC 39 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

/// Verified by https://atcoder.jp/contests/abc198/submissions/21774342
mod mod_int {
    use std::ops::*;
    pub trait Mod: Copy { fn m() -> i64; }
    #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
    pub struct ModInt<M> { pub x: i64, phantom: ::std::marker::PhantomData<M> }
    impl<M: Mod> ModInt<M> {
        // x >= 0
        pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) }
        fn new_internal(x: i64) -> Self {
            ModInt { x: x, phantom: ::std::marker::PhantomData }
        }
    }
    impl<M: Mod, T: Into<ModInt<M>>> Add<T> for ModInt<M> {
        type Output = Self;
        fn add(self, other: T) -> Self {
            let other = other.into();
            let mut sum = self.x + other.x;
            if sum >= M::m() { sum -= M::m(); }
            ModInt::new_internal(sum)
        }
    }
    impl<M: Mod, T: Into<ModInt<M>>> Sub<T> for ModInt<M> {
        type Output = Self;
        fn sub(self, other: T) -> Self {
            let other = other.into();
            let mut sum = self.x - other.x;
            if sum < 0 { sum += M::m(); }
            ModInt::new_internal(sum)
        }
    }
    impl<M: Mod, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> {
        fn add_assign(&mut self, other: T) { *self = *self + other; }
    }
    impl<M: Mod, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> {
        fn sub_assign(&mut self, other: T) { *self = *self - other; }
    }
    impl<M: Mod> Neg for ModInt<M> {
        type Output = Self;
        fn neg(self) -> Self { ModInt::new(0) - self }
    }
    impl<M> ::std::fmt::Display for ModInt<M> {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
            self.x.fmt(f)
        }
    }
    impl<M: Mod> From<i64> for ModInt<M> {
        fn from(x: i64) -> Self { Self::new(x) }
    }
} // mod mod_int

macro_rules! define_mod {
    ($struct_name: ident, $modulo: expr) => {
        #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
        struct $struct_name {}
        impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } }
    }
}
const MOD: i64 = 1_000_000_007;
define_mod!(P, MOD);
type MInt = mod_int::ModInt<P>;

// O(n^2)
fn calc(n: usize, dp: &[MInt]) -> Vec<MInt> {
    let mut dp = dp.to_vec();
    for i in 0..n - 1 {
        let mut acc = vec![MInt::new(0); n + 1];
        for j in 0..n {
            acc[j + 1] = acc[j] + dp[j];
        }
        let mut ep = vec![MInt::new(0); n];
        let diff = max(1, i);
        for j in 0..n {
            ep[j] = acc[min(n, j + diff)];
        }
        dp = ep;
    }
    dp
}

fn main() {
    let n: usize = get();
    let dp = vec![MInt::new(1); n];
    let dp1 = calc(n, &dp);
    let mut dp = vec![MInt::new(0); n];
    dp[0] = 1.into();
    let dp2 = calc(n, &dp);
    let mut sum = -dp2[n - 1];
    for i in 0..n {
        sum += dp1[i];
    }
    println!("{}", sum);
}
0