結果
| 問題 |
No.3071 Double Speedrun
|
| コンテスト | |
| ユーザー |
urectanc
|
| 提出日時 | 2025-03-24 18:05:56 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 4,181 ms / 6,000 ms |
| コード長 | 8,663 bytes |
| コンパイル時間 | 13,351 ms |
| コンパイル使用メモリ | 383,188 KB |
| 実行使用メモリ | 10,428 KB |
| 最終ジャッジ日時 | 2025-03-24 18:06:33 |
| 合計ジャッジ時間 | 36,433 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 |
ソースコード
use std::collections::HashMap;
use proconio::{input, marker::Chars};
type Mint = modint::ModInt998244353;
fn main() {
input! {
h: usize, w: usize,
s: [Chars; h]
}
let encode =
|i0: usize, j0: usize, i1: usize, j1: usize| (i0 << 27) | (j0 << 18) | (i1 << 9) | j1;
const MASK: usize = (1 << 9) - 1;
let decode = |x: usize| (x >> 27 & MASK, x >> 18 & MASK, x >> 9 & MASK, x & MASK);
let mut dp = HashMap::new();
dp.insert(encode(1, 0, 0, 1), Mint::new(1));
for _ in 0..h + w - 4 {
let mut ndp = HashMap::new();
let mut transition = |i0: usize, j0: usize, i1: usize, j1: usize, v: &Mint| {
if i0 >= h || j0 >= w || i1 >= h || j1 >= w {
return;
}
if i0 == i1 && j0 == j1 {
return;
}
if s[i0][j0] == '#' || s[i1][j1] == '#' {
return;
}
let key = encode(i0, j0, i1, j1);
*ndp.entry(key).or_insert(Mint::new(0)) += v;
};
for (&k, v) in &dp {
let (i0, j0, i1, j1) = decode(k);
transition(i0 + 1, j0, i1 + 1, j1, &v);
transition(i0 + 1, j0, i1, j1 + 1, &v);
transition(i0, j0 + 1, i1 + 1, j1, &v);
transition(i0, j0 + 1, i1, j1 + 1, &v);
}
dp = ndp;
}
let ans = dp
.get(&encode(h - 1, w - 2, h - 2, w - 1))
.copied()
.unwrap_or(Mint::new(0));
println!("{ans}");
}
#[allow(dead_code)]
mod modint {
use std::{
fmt::{Debug, Display},
iter::{Product, Sum},
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
str::FromStr,
};
pub type ModInt998244353 = ModInt<998244353>;
pub type ModInt1000000007 = ModInt<1000000007>;
type Val = u64;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct ModInt<const M: Val> {
val: Val,
}
impl<const M: Val> ModInt<M> {
const IS_PRIME: bool = is_prime(M as u32);
pub const fn modulus() -> Val {
M
}
pub const fn new(val: Val) -> Self {
assert!(M < (1 << 31));
Self {
val: val.rem_euclid(M),
}
}
pub const fn new_unchecked(val: Val) -> Self {
Self { val }
}
pub const fn val(&self) -> Val {
self.val
}
pub fn pow(self, mut exp: u64) -> Self {
let mut result = Self::new(1);
let mut base = self;
while exp > 0 {
if exp & 1 == 1 {
result *= base;
}
base *= base;
exp >>= 1;
}
result
}
pub fn inv(self) -> Self {
assert!(Self::IS_PRIME);
self.pow(M as u64 - 2).into()
}
}
impl<const M: Val> Display for ModInt<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.val)
}
}
impl<const M: Val> Debug for ModInt<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.val)
}
}
impl<const M: Val> FromStr for ModInt<M> {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let value = s.parse::<Val>()?;
Ok(ModInt::new(value))
}
}
impl<const M: Val> Neg for ModInt<M> {
type Output = Self;
fn neg(mut self) -> Self::Output {
if self.val > 0 {
self.val = M - self.val;
}
self
}
}
impl<const M: Val, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> {
fn add_assign(&mut self, rhs: T) {
self.val += rhs.into().val;
if self.val >= M {
self.val -= M;
}
}
}
impl<const M: Val, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> {
fn sub_assign(&mut self, rhs: T) {
self.val = self.val.wrapping_sub(rhs.into().val);
if self.val > M {
self.val = self.val.wrapping_add(M);
}
}
}
impl<const M: Val, T: Into<ModInt<M>>> MulAssign<T> for ModInt<M> {
fn mul_assign(&mut self, rhs: T) {
self.val = self.val * rhs.into().val % M;
}
}
impl<const M: Val, T: Into<ModInt<M>>> DivAssign<T> for ModInt<M> {
fn div_assign(&mut self, rhs: T) {
*self *= rhs.into().inv();
}
}
macro_rules! impl_binnary_operators {
($({ $trait: ident, $trait_assign: ident, $fn: ident, $fn_assign: ident, $type: ty }),*) => {$(
impl<const M: Val, T: Into<$type>> $trait<T> for $type {
type Output = $type;
fn $fn(mut self, rhs: T) -> $type {
self.$fn_assign(rhs.into());
self
}
}
impl<const M: Val> $trait<&$type> for $type {
type Output = $type;
fn $fn(self, rhs: &$type) -> $type {
self.$fn(*rhs)
}
}
impl<const M: Val, T: Into<$type>> $trait<T> for &$type {
type Output = $type;
fn $fn(self, rhs: T) -> $type {
(*self).$fn(rhs.into())
}
}
impl<const M: Val> $trait<&$type> for &$type {
type Output = $type;
fn $fn(self, rhs: &$type) -> $type {
(*self).$fn(*rhs)
}
}
impl<const M: Val> $trait_assign<&$type> for $type {
fn $fn_assign(&mut self, rhs: &$type) {
*self = self.$fn(*rhs);
}
}
)*};
}
impl_binnary_operators!(
{Add, AddAssign, add, add_assign, ModInt<M>},
{Sub, SubAssign, sub, sub_assign, ModInt<M>},
{Mul, MulAssign, mul, mul_assign, ModInt<M>},
{Div, DivAssign, div, div_assign, ModInt<M>}
);
impl<const M: Val> Sum for ModInt<M> {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::new(0), Add::add)
}
}
impl<const M: Val> Product for ModInt<M> {
fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::new(1), Mul::mul)
}
}
impl<'a, const M: Val> Sum<&'a Self> for ModInt<M> {
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Self::new(0), Add::add)
}
}
impl<'a, const M: Val> Product<&'a Self> for ModInt<M> {
fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Self::new(1), Mul::mul)
}
}
macro_rules! impl_rem_euclid_signed {
($($ty:tt),*) => {
$(
impl<const M: Val> From<$ty> for ModInt<M> {
fn from(value: $ty) -> ModInt<M> {
Self::new_unchecked((value as i64).rem_euclid(M as i64) as Val)
}
}
)*
};
}
impl_rem_euclid_signed!(i8, i16, i32, i64, isize);
macro_rules! impl_rem_euclid_unsigned {
($($ty:tt),*) => {
$(
impl<const M: Val> From<$ty> for ModInt<M> {
fn from(value: $ty) -> ModInt<M> {
Self::new_unchecked((value as u64).rem_euclid(M as u64) as Val)
}
}
)*
};
}
impl_rem_euclid_unsigned!(u8, u16, u32, u64, usize);
const fn is_prime(n: u32) -> bool {
const fn is_sprp(n: u32, a: u32) -> bool {
let (n, a) = (n as u64, a as u64);
let mut d = n >> (n - 1).trailing_zeros();
let mut y = {
let (mut res, mut base, mut e) = (1, a, d);
while e > 0 {
if e & 1 == 1 {
res = res * base % n;
}
base = base * base % n;
e >>= 1;
}
res
};
while d != n - 1 && y != 1 && y != n - 1 {
y = y * y % n;
d <<= 1;
}
y == n - 1 || d & 1 == 1
}
if matches!(n, 2 | 7 | 61) {
return true;
}
if n <= 1 || n % 2 == 0 {
return false;
}
is_sprp(n, 2) && is_sprp(n, 7) && is_sprp(n, 61)
}
}
urectanc