結果

問題 No.1385 Simple Geometry 2
ユーザー koba-e964koba-e964
提出日時 2023-09-19 10:42:27
言語 Rust
(1.77.0)
結果
AC  
実行時間 195 ms / 500 ms
コード長 4,194 bytes
コンパイル時間 2,319 ms
コンパイル使用メモリ 153,360 KB
実行使用メモリ 37,728 KB
最終ジャッジ日時 2023-09-19 10:42:54
合計ジャッジ時間 18,619 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 6 ms
4,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 182 ms
37,652 KB
testcase_14 AC 186 ms
37,492 KB
testcase_15 AC 186 ms
37,548 KB
testcase_16 AC 193 ms
37,564 KB
testcase_17 AC 195 ms
37,548 KB
testcase_18 AC 192 ms
37,552 KB
testcase_19 AC 188 ms
37,548 KB
testcase_20 AC 187 ms
37,552 KB
testcase_21 AC 193 ms
37,540 KB
testcase_22 AC 176 ms
37,656 KB
testcase_23 AC 191 ms
37,668 KB
testcase_24 AC 182 ms
37,632 KB
testcase_25 AC 182 ms
37,612 KB
testcase_26 AC 192 ms
37,580 KB
testcase_27 AC 193 ms
37,556 KB
testcase_28 AC 175 ms
37,556 KB
testcase_29 AC 193 ms
37,612 KB
testcase_30 AC 179 ms
37,580 KB
testcase_31 AC 180 ms
37,556 KB
testcase_32 AC 179 ms
37,560 KB
testcase_33 AC 182 ms
37,580 KB
testcase_34 AC 178 ms
37,620 KB
testcase_35 AC 191 ms
37,484 KB
testcase_36 AC 180 ms
37,492 KB
testcase_37 AC 183 ms
37,544 KB
testcase_38 AC 183 ms
37,544 KB
testcase_39 AC 181 ms
37,568 KB
testcase_40 AC 183 ms
37,560 KB
testcase_41 AC 191 ms
37,548 KB
testcase_42 AC 178 ms
37,728 KB
testcase_43 AC 182 ms
37,560 KB
testcase_44 AC 183 ms
37,484 KB
testcase_45 AC 181 ms
37,544 KB
testcase_46 AC 182 ms
37,656 KB
testcase_47 AC 192 ms
37,544 KB
testcase_48 AC 180 ms
37,556 KB
testcase_49 AC 180 ms
37,560 KB
testcase_50 AC 180 ms
37,652 KB
testcase_51 AC 182 ms
37,548 KB
testcase_52 AC 182 ms
37,580 KB
testcase_53 AC 182 ms
37,660 KB
testcase_54 AC 180 ms
37,544 KB
testcase_55 AC 180 ms
37,580 KB
testcase_56 AC 180 ms
37,544 KB
testcase_57 AC 182 ms
37,556 KB
testcase_58 AC 193 ms
37,712 KB
testcase_59 AC 156 ms
37,680 KB
testcase_60 AC 179 ms
37,484 KB
testcase_61 AC 171 ms
37,624 KB
testcase_62 AC 170 ms
37,552 KB
testcase_63 AC 181 ms
37,564 KB
testcase_64 AC 192 ms
37,668 KB
testcase_65 AC 183 ms
37,656 KB
testcase_66 AC 181 ms
37,524 KB
testcase_67 AC 179 ms
37,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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::<Vec<_>>()
    };
    ($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<T = f64> {
        pub x: T,
        pub y: T,
    }
    
    impl<T> Complex<T> {
        pub fn new(x: T, y: T) -> Self { Complex { x: x, y: y } }
    }
    impl<T> Add for Complex<T>
        where T: Add<Output = T> {
        type Output = Self;
        fn add(self, other: Self) -> Self { 
            Self::new(self.x + other.x, self.y + other.y)
        }
    }
    impl<T> Sub for Complex<T>
        where T: Sub<Output = T> {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            Self::new(self.x - other.x, self.y - other.y)
        }
    }
    impl<T: Copy> Mul for Complex<T>
        where T: Add<Output = T> +
              Sub<Output = T> +
              Mul<Output = T> {
        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<T: Copy + Neg<Output = T>> Complex<T> {
        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<i}(i - j - 1)conj(a[j]) + |a|^2 i(i-1)/2) / 2 である。
// これの計算のためには q[i] = \sum_{j < i} ja[j] と r[i] = \sum_{j < i} a[j] がわかっていればよく、
// これらを使うと (p[i] - conj(a[i]) q[i] + a[i]conj(q[j]) - a[i](i-1)conj(r[i]) + |a|^2 i(i-1)/2) / 2 である。
fn main() {
    input! {
        n: usize, l: f64,
        t: [f64; n],
    }
    use complex::*;
    let mut a = vec![Complex::new(0.0, 0.0); n];
    for i in 0..n {
        let angle = std::f64::consts::PI * t[i] * 2.0 / l;
        a[i] = Complex::new(angle.cos(), angle.sin());
    }
    let mut p = vec![Complex::new(0.0, 0.0); n + 1];
    let mut q = vec![Complex::new(0.0, 0.0); n + 1];
    let mut r = vec![Complex::new(0.0, 0.0); n + 1];
    for i in 0..n {
        r[i + 1] = r[i] + a[i];
        q[i + 1] = q[i] + a[i] * Complex::new(i as f64, 0.0);
        if i > 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));
}
0