diff --git a/atcoder/abc379/a.rs b/atcoder/abc379/a.rs new file mode 100644 index 00000000..02eb4a5e --- /dev/null +++ b/atcoder/abc379/a.rs @@ -0,0 +1,15 @@ +fn getline() -> String { + let mut ret = String::new(); + std::io::stdin().read_line(&mut ret).ok().unwrap(); + ret +} + +fn main() { + let mut x: i64 = getline().trim().parse().ok().unwrap(); + for _ in 0..2 { + x *= 10; + x += x / 1000; + x %= 1000; + println!("{x}"); + } +} diff --git a/atcoder/abc379/b.rs b/atcoder/abc379/b.rs new file mode 100644 index 00000000..ea8365bd --- /dev/null +++ b/atcoder/abc379/b.rs @@ -0,0 +1,49 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, chars) => { + read_value!($next, String).chars().collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + input! { + n: usize, k: usize, + s: chars, + } + let mut cur = 0; + let mut ans = 0; + for c in s { + if c == 'O' { + cur += 1; + } else { + ans += cur / k; + cur = 0; + } + } + ans += cur / k; + println!("{ans}"); +} diff --git a/atcoder/abc379/c.rs b/atcoder/abc379/c.rs new file mode 100644 index 00000000..c79a2849 --- /dev/null +++ b/atcoder/abc379/c.rs @@ -0,0 +1,62 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + input! { + n: i64, m: usize, + x: [i64; m], + a: [i64; m], + } + let asum = a.iter().sum::(); + if asum != n { + println!("-1"); + return; + } + let mut xa = vec![]; + for i in 0..m { + xa.push((x[i], a[i])); + } + xa.sort(); + let mut tot = 0; + for i in (0..m).rev() { + let (x, a) = xa[i]; + if tot + a + x > n + 1 { + println!("-1"); + return; + } + tot += a; + } + let mut ans = n * (n + 1) / 2; + for (x, a) in xa { + ans -= x * a; + } + println!("{ans}"); +} diff --git a/atcoder/abc379/d.rs b/atcoder/abc379/d.rs new file mode 100644 index 00000000..97542408 --- /dev/null +++ b/atcoder/abc379/d.rs @@ -0,0 +1,47 @@ +use std::collections::*; +use std::io::Read; + +fn get_word() -> String { + let stdin = std::io::stdin(); + let mut stdin=stdin.lock(); + let mut u8b: [u8; 1] = [0]; + loop { + let mut buf: Vec = Vec::with_capacity(16); + loop { + let res = stdin.read(&mut u8b); + if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { + break; + } else { + buf.push(u8b[0]); + } + } + if buf.len() >= 1 { + let ret = String::from_utf8(buf).unwrap(); + return ret; + } + } +} + +fn get() -> T { get_word().parse().ok().unwrap() } + +fn main() { + let q: i32 = get(); + let mut s = BTreeSet::new(); + let mut bias = 0; + for idx in 0..q { + let ty: i32 = get(); + if ty == 1 { + s.insert((-bias, idx)); + } else if ty == 2 { + let t: i64 = get(); + bias += t; + } else { + let h: i64 = get(); + let tmp = s.range((h - bias, 0)..).cloned().collect::>(); + println!("{}", tmp.len()); + for t in tmp { + s.remove(&t); + } + } + } +} diff --git a/atcoder/abc379/e.rs b/atcoder/abc379/e.rs new file mode 100644 index 00000000..972d62ee --- /dev/null +++ b/atcoder/abc379/e.rs @@ -0,0 +1,45 @@ +fn getline() -> String { + let mut ret = String::new(); + std::io::stdin().read_line(&mut ret).ok().unwrap(); + ret +} + +fn quo(a: i64, b: i64) -> i64 { + assert!(b > 0); + let mut r = a % b; + if r < 0 { + r += b; + } + (a - r) / b +} + +fn main() { + getline(); + let s = getline().trim().bytes().collect::>(); + let n = s.len(); + let mut dig = vec![0; n + 1]; + for i in 0..n { + let d = (s[i] - b'0') as i64 * (i as i64 + 1); + dig[i] += d; + dig[n] -= d; + } + for i in (0..n).rev() { + let q = quo(dig[i + 1], 10); + dig[i] += q; + dig[i + 1] -= q * 10; + } + // /= 9 + let mut carry = 0; + for i in 0..n + 1 { + let q = quo(dig[i] + carry * 10, 9); + carry = (dig[i] + carry * 10) - q * 9; + dig[i] = q; + } + while dig[0] == 0 { + dig.remove(0); + } + for d in dig { + print!("{d}"); + } + println!(); +} diff --git a/atcoder/abc379/f.rs b/atcoder/abc379/f.rs new file mode 100644 index 00000000..b1ff4396 --- /dev/null +++ b/atcoder/abc379/f.rs @@ -0,0 +1,70 @@ +use std::io::{Write, BufWriter}; +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, usize1) => (read_value!($next, usize) - 1); + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + let out = std::io::stdout(); + let mut out = BufWriter::new(out.lock()); + macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} + input! { + n: usize, q: usize, + h: [i64; n], + lr: [(usize1, usize1); q], + } + let mut qs = vec![vec![]; n]; + for i in 0..q { + let (l, r) = lr[i]; + qs[l].push((r, i)); + } + let mut st: Vec = vec![]; + let mut ans = vec![0; q]; + for i in (0..n).rev() { + for &(r, idx) in qs[i].iter() { + let from = match st.binary_search_by(|x| r.cmp(x)) { + Ok(x) => x, + Err(x) => x, + }; + ans[idx] = from; + } + while let Some(v) = st.pop() { + if h[i] < h[v] { + st.push(v); + break; + } + } + st.push(i); + } + for a in ans { + puts!("{a}\n"); + } +} diff --git a/atcoder/abc379/g.rs b/atcoder/abc379/g.rs new file mode 100644 index 00000000..69a5d35d --- /dev/null +++ b/atcoder/abc379/g.rs @@ -0,0 +1,190 @@ +use std::collections::*; +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, chars) => { + read_value!($next, String).chars().collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +/// Verified by https://atcoder.jp/contests/abc198/submissions/21774342 +mod mod_int { + use std::ops::*; + pub trait Mod: Copy { fn m() -> i64; } + #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] + pub struct ModInt { pub x: i64, phantom: ::std::marker::PhantomData } + impl ModInt { + // x >= 0 + pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) } + fn new_internal(x: i64) -> Self { + ModInt { x: x, phantom: ::std::marker::PhantomData } + } + pub fn pow(self, mut e: i64) -> Self { + debug_assert!(e >= 0); + let mut sum = ModInt::new_internal(1); + let mut cur = self; + while e > 0 { + if e % 2 != 0 { sum *= cur; } + cur *= cur; + e /= 2; + } + sum + } + #[allow(dead_code)] + pub fn inv(self) -> Self { self.pow(M::m() - 2) } + } + impl Default for ModInt { + fn default() -> Self { Self::new_internal(0) } + } + impl>> Add for ModInt { + type Output = Self; + fn add(self, other: T) -> Self { + let other = other.into(); + let mut sum = self.x + other.x; + if sum >= M::m() { sum -= M::m(); } + ModInt::new_internal(sum) + } + } + impl>> Sub for ModInt { + type Output = Self; + fn sub(self, other: T) -> Self { + let other = other.into(); + let mut sum = self.x - other.x; + if sum < 0 { sum += M::m(); } + ModInt::new_internal(sum) + } + } + impl>> Mul for ModInt { + type Output = Self; + fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) } + } + impl>> AddAssign for ModInt { + fn add_assign(&mut self, other: T) { *self = *self + other; } + } + impl>> SubAssign for ModInt { + fn sub_assign(&mut self, other: T) { *self = *self - other; } + } + impl>> MulAssign for ModInt { + fn mul_assign(&mut self, other: T) { *self = *self * other; } + } + impl Neg for ModInt { + type Output = Self; + fn neg(self) -> Self { ModInt::new(0) - self } + } + impl ::std::fmt::Display for ModInt { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + self.x.fmt(f) + } + } + impl From for ModInt { + fn from(x: i64) -> Self { Self::new(x) } + } +} // mod mod_int + +macro_rules! define_mod { + ($struct_name: ident, $modulo: expr) => { + #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct $struct_name {} + impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } } + } +} +const MOD: i64 = 998_244_353; +define_mod!(P, MOD); +type MInt = mod_int::ModInt

; + +fn calc(h: usize, w: usize, s: Vec>) -> MInt { + assert!(h >= w); + assert!(w <= 14); + let mut dp = HashMap::new(); + dp.insert(vec![], MInt::new(1)); + for i in 0..w { + let mut ep = HashMap::new(); + for (k, v) in dp { + for c in '1'..='3' { + if s[0][i] != '?' && s[0][i] != c { + continue; + } + if k.len() > 0 && k[k.len() - 1] == c { + continue; + } + let mut kcp = k.clone(); + kcp.push(c); + ep.insert(kcp, v); + } + } + dp = ep; + } + for i in 1..h { + for j in 0..w { + let mut ep = HashMap::new(); + for (k, v) in dp { + for c in '1'..='3' { + if s[i][j] != '?' && s[i][j] != c { + continue; + } + if j > 0 && k[j - 1] == c { + continue; + } + if k[j] == c { + continue; + } + let mut kcp = k.clone(); + kcp[j] = c; + *ep.entry(kcp).or_insert(MInt::new(0)) += v; + } + } + dp = ep; + } + } + let mut tot = MInt::new(0); + for (_, v) in dp { + tot += v; + } + tot +} + +// Tags: treewidth, pathwidth, dp +fn main() { + input! { + h: usize, w: usize, + s: [chars; h], + } + let ans = if h < w { + let mut tr = vec![vec!['.'; h]; w]; + for i in 0..h { + for j in 0..w { + tr[j][i] = s[i][j]; + } + } + calc(w, h, tr) + } else { + calc(h, w, s) + }; + println!("{ans}"); +}