結果

問題 No.1385 Simple Geometry 2
ユーザー koba-e964koba-e964
提出日時 2023-09-19 10:41:07
言語 Rust
(1.77.0)
結果
AC  
実行時間 207 ms / 500 ms
コード長 4,928 bytes
コンパイル時間 6,559 ms
コンパイル使用メモリ 156,008 KB
実行使用メモリ 36,816 KB
最終ジャッジ日時 2023-09-19 10:41:34
合計ジャッジ時間 26,652 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 6 ms
4,384 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 6 ms
4,384 KB
testcase_11 AC 6 ms
4,384 KB
testcase_12 AC 11 ms
4,384 KB
testcase_13 AC 194 ms
36,580 KB
testcase_14 AC 182 ms
36,656 KB
testcase_15 AC 184 ms
36,656 KB
testcase_16 AC 194 ms
36,644 KB
testcase_17 AC 182 ms
36,600 KB
testcase_18 AC 182 ms
36,676 KB
testcase_19 AC 180 ms
36,812 KB
testcase_20 AC 188 ms
36,708 KB
testcase_21 AC 187 ms
36,800 KB
testcase_22 AC 176 ms
36,692 KB
testcase_23 AC 185 ms
36,744 KB
testcase_24 AC 178 ms
36,640 KB
testcase_25 AC 206 ms
36,560 KB
testcase_26 AC 184 ms
36,616 KB
testcase_27 AC 188 ms
36,732 KB
testcase_28 AC 170 ms
36,664 KB
testcase_29 AC 184 ms
36,760 KB
testcase_30 AC 207 ms
36,708 KB
testcase_31 AC 182 ms
36,688 KB
testcase_32 AC 179 ms
36,796 KB
testcase_33 AC 182 ms
36,812 KB
testcase_34 AC 180 ms
36,716 KB
testcase_35 AC 182 ms
36,640 KB
testcase_36 AC 183 ms
36,716 KB
testcase_37 AC 183 ms
36,656 KB
testcase_38 AC 183 ms
36,656 KB
testcase_39 AC 182 ms
36,800 KB
testcase_40 AC 183 ms
36,660 KB
testcase_41 AC 182 ms
36,684 KB
testcase_42 AC 181 ms
36,716 KB
testcase_43 AC 183 ms
36,780 KB
testcase_44 AC 182 ms
36,692 KB
testcase_45 AC 183 ms
36,692 KB
testcase_46 AC 183 ms
36,808 KB
testcase_47 AC 183 ms
36,800 KB
testcase_48 AC 183 ms
36,692 KB
testcase_49 AC 183 ms
36,808 KB
testcase_50 AC 184 ms
36,696 KB
testcase_51 AC 184 ms
36,800 KB
testcase_52 AC 183 ms
36,808 KB
testcase_53 AC 183 ms
36,652 KB
testcase_54 AC 186 ms
36,656 KB
testcase_55 AC 185 ms
36,636 KB
testcase_56 AC 185 ms
36,712 KB
testcase_57 AC 184 ms
36,632 KB
testcase_58 AC 183 ms
36,696 KB
testcase_59 AC 162 ms
36,816 KB
testcase_60 AC 171 ms
36,760 KB
testcase_61 AC 174 ms
36,744 KB
testcase_62 AC 176 ms
36,788 KB
testcase_63 AC 188 ms
36,628 KB
testcase_64 AC 185 ms
36,812 KB
testcase_65 AC 184 ms
36,708 KB
testcase_66 AC 184 ms
36,692 KB
testcase_67 AC 183 ms
36,692 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `BufWriter`
 --> Main.rs:5:22
  |
5 | use std::io::{Write, BufWriter};
  |                      ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `Write`
 --> Main.rs:5:15
  |
5 | use std::io::{Write, BufWriter};
  |               ^^^^^

warning: 2 warnings emitted

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
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::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($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() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    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