結果
| 問題 |
No.1417 100の倍数かつ正整数(2)
|
| コンテスト | |
| ユーザー |
Strorkis
|
| 提出日時 | 2021-03-05 22:17:34 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 9 ms / 3,000 ms |
| コード長 | 5,060 bytes |
| コンパイル時間 | 22,930 ms |
| コンパイル使用メモリ | 400,888 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-07 02:31:06 |
| 合計ジャッジ時間 | 17,187 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#[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);
}
Strorkis