結果
問題 | No.2742 Car Flow |
ユーザー |
![]() |
提出日時 | 2024-04-21 12:50:04 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 6 ms / 2,000 ms |
コード長 | 3,962 bytes |
コンパイル時間 | 13,687 ms |
コンパイル使用メモリ | 378,360 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-13 11:21:13 |
合計ジャッジ時間 | 15,614 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 50 |
ソースコード
#![allow(non_snake_case)]use proconio::{input};mod mod_int {use core::iter::Sum;use core::ops::*;#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]pub struct ModInt<const MOD: i32> {value: i32}impl<const MOD: i32> ModInt<MOD> {fn normalize(v: i64) -> i32 {if 0 <= v && v < MOD as i64 {v as i32} else {let v1 = (v % MOD as i64) as i32;if v1 < 0 { v1 + MOD } else { v1 }}}pub fn new(v: i64) -> ModInt<MOD> { ModInt{ value: Self::normalize(v) } }pub fn inverse(self) -> Self {let mut x = MOD as i64;let mut y = self.value as i64;let mut p = 1;let mut q = 0;let mut r = 0;let mut s = 1;while y != 0 {let u = x / y;let x0 = y; y = x - y * u; x = x0;let r0 = p - r * u;let s0 = q - s * u;p = r; r = r0; q = s; s = s0;}ModInt::new(q)}}impl<const MOD: i32> std::fmt::Display for ModInt<MOD> {fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {write!(f, "{}", self.value)}}impl<const MOD: i32> Add<Self> for ModInt<MOD> {type Output = Self;fn add(self, rhs: Self) -> Self {let value = self.value as i64 + rhs.value as i64;let value = (if value < MOD as i64 { value } else { value - MOD as i64 }) as i32;ModInt{ value }}}impl<const MOD: i32> Add<i64> for ModInt<MOD> {type Output = Self;fn add(self, rhs: i64) -> Self {self + ModInt::new(rhs)}}impl<const MOD: i32> Sub<Self> for ModInt<MOD> {type Output = Self;fn sub(self, rhs: Self) -> Self {let value = self.value as i64 - rhs.value as i64;let value = (if value >= 0 { value } else { value + MOD as i64 }) as i32;ModInt{ value }}}impl<const MOD: i32> Sub<i64> for ModInt<MOD> {type Output = Self;fn sub(self, rhs: i64) -> Self {self - ModInt::new(rhs)}}impl<const MOD: i32> AddAssign<Self> for ModInt<MOD> {fn add_assign(&mut self, rhs: Self) {*self = *self + rhs}}impl<const MOD: i32> AddAssign<i64> for ModInt<MOD> {fn add_assign(&mut self, rhs: i64) {*self = *self + rhs}}impl<const MOD: i32> Mul<Self> for ModInt<MOD> {type Output = Self;fn mul(self, rhs: Self) -> Self {Self::new(self.value as i64 * rhs.value as i64)}}impl<const MOD: i32> MulAssign<Self> for ModInt<MOD> {fn mul_assign(&mut self, rhs: Self) {*self = *self * rhs}}impl<const MOD: i32> Div<Self> for ModInt<MOD> {type Output = Self;fn div(self, rhs: Self) -> Self {self * rhs.inverse()}}impl<const MOD: i32> Div<i64> for ModInt<MOD> {type Output = Self;fn div(self, rhs: i64) -> Self {self / ModInt::new(rhs)}}impl<const MOD: i32> Sum<Self> for ModInt<MOD> {fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {iter.fold(Self::new(0), |a, b| a + b)}}}const MOD: i32 = 998244353;type MInt = mod_int::ModInt::<MOD>;fn main() {input!{ n: usize, a: [usize; n] }// {// let mut b = a.clone();// for _ in 0..1000 {// let mut c = b.clone();// for i in 0..n {// c[i] = if {// (b[(i + n - 1) % n] == 1 && b[i] == 0) ||// (b[i] == 1 && b[(i + 1) % n] == 1)// } {// 1// } else {// 0// }// }// for x in &c { eprint!("{x} "); }// eprintln!("");// b = c;// }// }let s = a.iter().sum::<usize>();if s <= n / 2 {println!("{}", MInt::new(s as i64) / MInt::new(n as i64))} else {println!("{}", MInt::new((n - s) as i64) / MInt::new(n as i64))}}