QOJ.ac

QOJ

Límite de tiempo: 1.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#8030. 在网格世界中旅行

Estadísticas

考虑一个有 $n$ 行 $m$ 列的网格图案。共有 $(n + 1) \times (m + 1)$ 个网格点,它们是 $n + 1$ 条水平线和 $m + 1$ 条垂直线的交点。我们将水平线从上到下编号为 $0$ 到 $n$,将垂直线从左到右编号为 $0$ 到 $m$。水平线 $i$ 和垂直线 $j$ 的交点记为 $(i, j)$ ($0 \le i \le n, 0 \le j \le m$)。

在网格世界中旅行时存在一些限制。当你位于点 $(x, y)$ 时,你可以选择一个目的地 $(x', y')$ 并沿着连接 $(x, y)$ 和 $(x', y')$ 的线段行走。我们将此操作称为一次“行走”。如果在线段上存在除 $(x, y)$ 和 $(x', y')$ 之外的另一个网格点,则该行走是被禁止的。你可以进行任意多次行走,但两次连续行走的路径方向不能相同。(具体来说,如果你从 $(x_0, y_0)$ 走到 $(x_1, y_1)$,然后从 $(x_1, y_1)$ 走到 $(x_2, y_2)$,你必须确保 $(x_0 - x_1)(y_1 - y_2) \neq (x_1 - x_2)(y_0 - y_1)$。)从 $(x, y)$ 到 $(x', y')$ 的行走长度定义为两个端点之间的欧几里得距离,即 $\sqrt{(x - x')^2 + (y - y')^2}$。

你计划从 $(0, 0)$ 出发,通过若干次行走到达 $(n, m)$。由于这些烦人的规则,你可能需要一些转折点来达到目标。请找出你行走的总长度的最小值。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$,表示测试数据的组数。对于每组测试数据:

第一行包含两个整数 $n, m$ ($1 \le n, m \le 10^6$),表示网格图的大小。

保证所有测试数据的 $\max(n, m)$ 之和不超过 $10^6$。

输出格式

对于每组测试数据,输出行走总长度的最小值。如果你的答案与标准答案的绝对误差或相对误差不超过 $10^{-9}$,则被视为正确。

样例

样例输入 1

2
2 2
2 3

样例输出 1

3.236067977499790
3.605551275463989

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.