#P16317. [ICPC 2023 Jinan R] 计算智能

    ID: 18253 远端评测题 1500ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>2023Special Judge微积分ICPC济南

[ICPC 2023 Jinan R] 计算智能

Problem Description

Given two line segments on a 2D Cartesian plane, you need to randomly choose one point from each segment with equal probability, and compute the expected value of the Euclidean distance between the two points.

Input Format

There are multiple groups of testdata. The first line contains an integer TT (1≤T≤1051 \leq T \leq 10^5), indicating the number of test cases. For each test case:

The first line contains four integers x1x_1, y1y_1, x2x_2, and y2y_2 (−103≤x1,y1,x2,y2≤103-10^3 \le x_1, y_1, x_2, y_2 \le 10^3), meaning that the two endpoints of the first segment are (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2).

The second line contains four integers x3x_3, y3y_3, x4x_4, and y4y_4 (−103≤x3,y3,x4,y4≤103-10^3 \le x_3, y_3, x_4, y_4 \le 10^3), meaning that the two endpoints of the second segment are (x3,y3)(x_3, y_3) and (x4,y4)(x_4, y_4).

It is guaranteed that both segments have positive length.

Output Format

For each test case, output one number per line, representing the expected distance between the two randomly chosen points.

Your answer will be accepted if the relative error or absolute error does not exceed 10−910^{-9}. Specifically, let your answer be aa and the judge’s answer be bb. Your answer is accepted if and only if ∣a−b∣max⁡(1,∣b∣)≤10−9\frac{|a - b|}{\max(1, |b|)} \le 10^{-9}.

3
0 0 1 0
0 0 1 0
0 0 1 0
0 0 0 1
0 0 1 0
0 1 1 1
0.333333333333333333
0.765195716464212691
1.076635732895178009

Hint

Thanks to “computational intelligence”, we know that:

For the first sample, the expected distance is

$$\int_{0}^{1} \int_{0}^{1} |x_0 - x_1| \,\mathrm{d}x_0 \,\mathrm{d}x_1 = \frac{1}{3} \approx 0.333333333333333333;$$

For the second sample, the expected distance is

$$\int_{0}^{1} \int_{0}^{1} \sqrt{x^2+y^2} \,\mathrm{d}x \,\mathrm{d}y = \frac{\sqrt{2}+\ln(1+\sqrt{2})}{3} \approx 0.765195716464212691;$$

For the third sample, the expected distance is

$$\int_{0}^{1} \int_{0}^{1} \sqrt{(x_0-x_1)^2+1} \,\mathrm{d}x_0 \,\mathrm{d}x_1 = \frac{2-\sqrt{2}+3\ln(1+\sqrt{2})}{3} \approx 1.076635732895178009.$$

Translated by ChatGPT 5