From e01517d7915a7d44575c4052a9cabfb39e2a70d5 Mon Sep 17 00:00:00 2001 From: koba-e964 <3303362+koba-e964@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:44:07 +0900 Subject: [PATCH] Add yukicoder/1385.rs yukicoder/2455.rs yukicoder/2477.rs --- comm/Complex.rs | 12 +++-- yukicoder/1385.rs | 113 +++++++++++++++++++++++++++++++++++++++++ yukicoder/2455.rs | 64 ++++++++++++++++++++++++ yukicoder/2477.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 311 insertions(+), 3 deletions(-) create mode 100644 yukicoder/1385.rs create mode 100644 yukicoder/2455.rs create mode 100644 yukicoder/2477.rs diff --git a/comm/Complex.rs b/comm/Complex.rs index cc579e9e..42bac7f1 100644 --- a/comm/Complex.rs +++ b/comm/Complex.rs @@ -1,7 +1,8 @@ -/// Complex numbers. -/// Verified by: ATC001-C (http://atc001.contest.atcoder.jp/submissions/1175487) +// Complex numbers. +// Verified by: ATC001-C (http://atc001.contest.atcoder.jp/submissions/1175487) +// yukicoder 1385 (https://yukicoder.me/submissions/914070) mod complex { - use std::ops::{Add, Sub, Mul}; + use std::ops::{Add, Sub, Mul, Neg}; #[derive(Clone, Copy, Debug)] pub struct Complex { pub x: T, @@ -35,4 +36,9 @@ mod complex { self.x * other.y + self.y * other.x) } } + impl> Complex { + pub fn conj(self) -> Self { + Self::new(self.x, -self.y) + } + } } // complex diff --git a/yukicoder/1385.rs b/yukicoder/1385.rs new file mode 100644 index 00000000..1bd1f0f1 --- /dev/null +++ b/yukicoder/1385.rs @@ -0,0 +1,113 @@ +// 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")); +} + +/// Complex numbers. +/// Verified by: ATC001-C (http://atc001.contest.atcoder.jp/submissions/1175487) +mod complex { + use std::ops::{Add, Sub, Mul, Neg}; + #[derive(Clone, Copy, Debug)] + pub struct Complex { + pub x: T, + pub y: T, + } + + impl Complex { + pub fn new(x: T, y: T) -> Self { Complex { x: x, y: y } } + } + impl Add for Complex + where T: Add { + type Output = Self; + fn add(self, other: Self) -> Self { + Self::new(self.x + other.x, self.y + other.y) + } + } + impl Sub for Complex + where T: Sub { + type Output = Self; + fn sub(self, other: Self) -> Self { + Self::new(self.x - other.x, self.y - other.y) + } + } + impl Mul for Complex + where T: Add + + Sub + + Mul { + type Output = Self; + fn mul(self, other: Self) -> Self { + Self::new(self.x * other.x - self.y * other.y, + self.x * other.y + self.y * other.x) + } + } + impl> Complex { + pub fn conj(self) -> Self { + Self::new(self.x, -self.y) + } + } +} // complex + +// https://yukicoder.me/problems/no/1385 (3) +// 三角形の面積は 点の座標に関する線形関数 + 定数 として表せるので、累積和ができる。 +// 具体的には、複素数 a, b, c に対してこれらを頂点とする三角形の面積は |Im((b-c)conj(a-c))|/2 である。 +// 絶対値の中身は a, b, c が反時計回りに並んでいる時に正。 +// 点列を時計回りに a[0], ..., a[N-1] として、j < k < i のときの a = a[j], b = a[k], c = a[i] としたときの和を計算することにする。 +// p[i] = \sum_{j < k < i} conj(a[j])a[k] が計算できていれば、c = a[i] のときの Im 内部の和は +// (p[i] - conj(a[i]) \sum_{j < i} ja[j] - a[i] \sum_{j 0 { + p[i + 1] = p[i] + a[i] * r[i].conj(); + } + } + let mut ans = Complex::new(0.0, 0.0); + for i in 0..n { + ans = ans + p[i] - a[i].conj() * q[i] + a[i] * q[i].conj() + - a[i] * r[i].conj() * Complex::new(i as f64 - 1.0, 0.0); + } + let nn = n as f64; + println!("{}", ans.y * 3.0 / nn / (nn - 1.0) / (nn - 2.0)); +} diff --git a/yukicoder/2455.rs b/yukicoder/2455.rs new file mode 100644 index 00000000..c7a661a2 --- /dev/null +++ b/yukicoder/2455.rs @@ -0,0 +1,64 @@ +use std::cmp::*; +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() } + +// If p10 = 10^k, this function returns a k-digit number +// that is obtained as floor(n * 10^?). +fn pick_digits(mut n: i64, p10: i64) -> i64 { + if n < p10 / 10 { + while n < p10 / 10 { + n *= 10; + } + return n; + } + while n >= p10 { + n /= 10; + } + n +} + +fn main() { + let t: i32 = get(); + for _ in 0..t { + let n: i64 = get(); + let k: i64 = get(); + let mut p = 1; + while p <= n { + p *= 10; + } + let mut ans = 0; + while p > 1 { + p /= 10; + // [p, cur) + let cur = pick_digits(k, 10 * p); + let l = p; + let r = min(cur + 1, min(n + 1, if cur > k { cur } else { cur + 1 })); + if l < r { + ans += r - l; + } + } + println!("{}", ans); + } +} diff --git a/yukicoder/2477.rs b/yukicoder/2477.rs new file mode 100644 index 00000000..69cf1c7a --- /dev/null +++ b/yukicoder/2477.rs @@ -0,0 +1,125 @@ +use std::cmp::*; +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),* )) => { ($(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 divide_into_segment_ranges(n: usize, rng: std::ops::Range, mut f: F) { + assert!(n.is_power_of_two()); + let (mut a, mut b) = (rng.start, rng.end); + a += n - 1; + b += n - 1; + while a < b { + if (a & 1) == 0 { + f(a); + } + if (b & 1) == 0 { + f(b - 1); + } + a = a / 2; + b = (b - 1) / 2; + } +} + +// 範囲に辺を張りたいので、重み 0 の辺を使って ->->-> という流れを作ってそれの上に辺を張ることにする。 +// -> それだと (a, b) に対して複数の c がある場合にうまくいかないので、代わりにセグメント木を使う。 +// Tags: manual-segment-trees +fn main() { + input! { + n: usize, m: usize, + uvw: [(usize1, usize1, i64); m], + k: usize, + abc: [(usize1, usize1, usize1); k], + } + let mut g = vec![vec![]; n]; + for &(u, v, w) in &uvw { + g[u].push((v, w)); + } + let mut offset = vec![0; n + 1]; + for i in 0..n { + g[i].sort(); + offset[i + 1] = offset[i] + g[i].len(); + } + let mut map = vec![HashMap::new(); n]; + for &(a, b, c) in &abc { + if let Ok(idx) = g[b].binary_search_by_key(&c, |x| x.0) { + map[a].entry(b).or_insert(vec![]).push(idx); + } + } + for i in 0..n { + for e in map[i].values_mut() { + e.sort(); + } + } + let m2 = m.next_power_of_two(); + let mut sup = vec![vec![]; 2 * m2]; // segment tree + for i in 0..m2 - 1 { + sup[i].push((2 * i + 1, 0)); + sup[i].push((2 * i + 2, 0)); + } + for a in 0..n { + for i in 0..g[a].len() { + let (b, w) = g[a][i]; + let idx = offset[a] + i; + if let Some(v) = map[a].get(&b) { + if !v.is_empty() { + for i in 0..v.len() - 1 { + divide_into_segment_ranges(m2, offset[b] + v[i] + 1..offset[b] + v[i + 1], |x| sup[m2 + idx - 1].push((x, w))); + } + divide_into_segment_ranges(m2, offset[b]..offset[b] + v[0], |x| sup[m2 + idx - 1].push((x, w))); + divide_into_segment_ranges(m2, offset[b] + v[v.len() - 1] + 1..offset[b + 1], |x| sup[m2 + idx - 1].push((x, w))); + } + } else { + if b == n - 1 { + sup[m2 - 1 + idx].push((2 * m2 - 1, w)); + } else { + divide_into_segment_ranges(m2, offset[b]..offset[b + 1], |x| sup[m2 + idx - 1].push((x, w))); + } + } + } + } + let mut que = BinaryHeap::new(); + for i in 0..g[0].len() { + que.push((Reverse(0), m2 - 1 + i)); + } + const INF: i64 = 1 << 50; + let mut dist = vec![INF; 2 * m2]; + while let Some((Reverse(d), v)) = que.pop() { + if dist[v] <= d { continue; } + dist[v] = d; + for &(w, c) in &sup[v] { + que.push((Reverse(d + c), w)); + } + } + let ans = dist[2 * m2 - 1]; + println!("{}", if ans >= INF { -1 } else { ans }); +}