結果
| 問題 |
No.2872 Depth of the Parentheses
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-10-14 22:28:24 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 55 ms / 2,000 ms |
| コード長 | 4,040 bytes |
| コンパイル時間 | 14,415 ms |
| コンパイル使用メモリ | 376,804 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-14 22:28:42 |
| 合計ジャッジ時間 | 16,149 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 20 WA * 5 |
ソースコード
use proconio::input;
fn main() {
input! {
x:usize,
k:usize,
}
if 10 < k {
return;
}
let p = ModInt::<MOD>::new(100).pow(2 * k * (MOD - 2));
let mut ans = ModInt::new(0);
for i in 0..1 << (2 * k) {
let mut stack = vec![];
let mut valid = true;
let mut dep = 0;
let mut q = ModInt::new(1);
for j in 0..2 * k {
if i & (1 << j) != 0 {
if stack.is_empty() {
valid = false;
break;
}
stack.pop();
q *= ModInt::new(100 - x);
} else {
stack.push('(');
dep = dep.max(stack.len());
q *= ModInt::new(x);
}
}
if !stack.is_empty() {
valid = false;
}
if valid {
ans += ModInt::new(dep) * q * p;
}
}
println!("{}", ans);
}
const MOD: usize = 998244353;
use modint::*;
mod modint {
use std::fmt;
use std::ops;
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct ModInt<const MOD: usize> {
pub val: usize,
}
impl<const MOD: usize> ModInt<MOD> {
pub fn new(val: usize) -> Self {
Self { val: val % MOD }
}
pub fn pow(mut self, mut e: usize) -> Self {
let mut res = Self::new(1);
while 0 < e {
if e & 1 != 0 {
res *= self;
}
self *= self;
e >>= 1;
}
res
}
}
impl<const MOD: usize> From<usize> for ModInt<MOD> {
fn from(value: usize) -> Self {
Self { val: value % MOD }
}
}
impl<const MOD: usize> fmt::Display for ModInt<MOD> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.val)
}
}
impl<const MOD: usize> fmt::Debug for ModInt<MOD> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.val)
}
}
impl<const MOD: usize> ops::Neg for ModInt<MOD> {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
val: (MOD - self.val) % MOD,
}
}
}
impl<const MOD: usize> ops::Add for ModInt<MOD> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
val: (self.val + rhs.val) % MOD,
}
}
}
impl<const MOD: usize> ops::AddAssign for ModInt<MOD> {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl<const MOD: usize> ops::Mul for ModInt<MOD> {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self {
val: self.val * rhs.val % MOD,
}
}
}
impl<const MOD: usize> ops::MulAssign for ModInt<MOD> {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}
impl<const MOD: usize> ops::Sub for ModInt<MOD> {
type Output = Self;
fn sub(mut self, rhs: Self) -> Self::Output {
if self.val < rhs.val {
self.val += MOD;
}
Self {
val: (self.val - rhs.val) % MOD,
}
}
}
impl<const MOD: usize> ops::SubAssign for ModInt<MOD> {
fn sub_assign(&mut self, rhs: Self) {
if self.val < rhs.val {
self.val += MOD;
}
*self = *self - rhs;
}
}
impl<const MOD: usize> ops::Div for ModInt<MOD> {
type Output = Self;
fn div(self, rhs: Self) -> Self {
assert!(rhs.val != 0);
self * rhs.pow(MOD - 2)
}
}
impl<const MOD: usize> ops::DivAssign for ModInt<MOD> {
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs
}
}
}