結果

問題 No.1417 100の倍数かつ正整数(2)
ユーザー StrorkisStrorkis
提出日時 2021-03-05 22:17:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 9 ms / 3,000 ms
コード長 5,060 bytes
コンパイル時間 1,812 ms
コンパイル使用メモリ 178,284 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 09:52:47
合計ジャッジ時間 2,898 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 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 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 4 ms
5,376 KB
testcase_34 AC 7 ms
5,376 KB
testcase_35 AC 8 ms
5,376 KB
testcase_36 AC 8 ms
5,376 KB
testcase_37 AC 9 ms
5,376 KB
testcase_38 AC 6 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(dead_code)]
mod mod_int {
    #[derive(Clone, Copy, Debug)]
    pub struct ModInt(pub i64);

    impl ModInt {
        const MOD: i64 = 1_000_000_007;

        pub fn new(x: i64) -> ModInt {
            Self(x % Self::MOD)
        }
    }

    impl std::ops::Add for ModInt {
        type Output = Self;

        fn add(self, other: Self) -> Self {
            let x = self.0 + other.0;
            if x < Self::MOD { Self(x) } else { Self(x - Self::MOD) }
        }
    }

    impl std::ops::AddAssign for ModInt {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }

    impl std::ops::Sub for ModInt {
        type Output = Self;

        fn sub(self, other: Self) -> Self {
            if self.0 < other.0 {
                Self(self.0 + Self::MOD - other.0)
            } else {
                Self(self.0 - other.0)
            }
        }
    }

    impl std::ops::SubAssign for ModInt {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }

    impl std::ops::Mul for ModInt {
        type Output = Self;

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

    impl std::ops::MulAssign for ModInt {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }

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

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

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

use mod_int::ModInt;

fn run<'a, F: FnMut() -> &'a str, W: std::io::Write>(scan: &mut F, writer: &mut W) {
    macro_rules! scan {
        ([$t:tt; $n:expr]) => ((0..$n).map(|_| scan!($t)).collect::<Vec<_>>());
        (($($t:tt),*)) => (($(scan!($t)),*));
        (Usize1) => (scan!(usize) - 1);
        (Bytes) => (scan().as_bytes().to_vec());
        ($t:ty) => (scan().parse::<$t>().unwrap());
    }
    macro_rules! println {
        ($($arg:tt)*) => (writeln!(writer, $($arg)*).ok());
    }

    let n = scan!(Bytes);

    // 0: line, 1: out
    // count 2
    // count 5
    let mut dp = vec![vec![vec![ModInt(0); 3]; 3]; 2];
    dp[0][0][0] = ModInt(1);
    for n in n.into_iter().map(|n| n - b'0') {
        let mut next = vec![vec![vec![ModInt(0); 3]; 3]; 2];
        next[1][0][0] = ModInt(1);
        for d in 1..10 {
            if d < n {
                for i in 0..2 {
                    for j in 0..3 {
                        for k in 0..3 {
                            match d {
                                2 | 6 => next[1][(j + 1).min(2)][k] += dp[i][j][k],
                                4 | 8 => next[1][(j + 2).min(2)][k] += dp[i][j][k],
                                5 => next[1][j][(k + 1).min(2)] += dp[i][j][k],
                                _ => next[1][j][k] += dp[i][j][k],
                            }
                        }
                    }
                }
            } else if d == n {
                for i in 0..2 {
                    for j in 0..3 {
                        for k in 0..3 {
                            match d {
                                2 | 6 => next[i][(j + 1).min(2)][k] += dp[i][j][k],
                                4 | 8 => next[i][(j + 2).min(2)][k] += dp[i][j][k],
                                5 => next[i][j][(k + 1).min(2)] += dp[i][j][k],
                                _ => next[i][j][k] += dp[i][j][k],
                            }
                        }
                    }
                }
            } else {
                for j in 0..3 {
                    for k in 0..3 {
                        match d {
                            2 | 6 => next[1][(j + 1).min(2)][k] += dp[1][j][k],
                            4 | 8 => next[1][(j + 2).min(2)][k] += dp[1][j][k],
                            5 => next[1][j][(k + 1).min(2)] += dp[1][j][k],
                            _ => next[1][j][k] += dp[1][j][k],
                        }
                    }
                }
            }
        }
        dp = next;
    }
    println!("{}", dp[0][2][2] + dp[1][2][2]);
}

fn main() {
    let ref mut buf = Vec::new();
    std::io::Read::read_to_end(&mut std::io::stdin(), buf).ok();
    let mut scanner = unsafe {
        std::str::from_utf8_unchecked(buf).split_ascii_whitespace()
    };
    let ref mut scan = || scanner.next().unwrap();

    let stdout = std::io::stdout();
    let ref mut writer = std::io::BufWriter::new(stdout.lock());

    run(scan, writer);
}
0