QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 1024 MB 总分: 100 可 Hack ✓

#18368. 曼哈顿环

统计

小 G 是生活在 H 星球上的一个智慧生命。在这个星球上,有着无边无际的花田,小 G 喜欢在花田间跳跃。在花田里玩耍的第 $(10^9 + 7)$ 天,小 G 了解了曼哈顿距离,并立即用它发明了一个游戏。

小 G 的游戏规则如下:选择一块正方形的花田,并将其划分为 $n^2$ 个相同的小正方形,排成 $n$ 行 $n$ 列,然后在这些正方形的所有顶点之间跳跃。每次跳跃的曼哈顿距离必须恰好等于给定的正整数 $K$。此外,小 G 不想迷路,所以他希望在进行一系列跳跃后最终回到起点。

小 G 很快找到了一个简单的解决方案:从一个角上的顶点开始,跳到曼哈顿距离为 $K$ 的顶点,然后再跳回来。这立即给出了所有跳跃次数为偶数的解。但小 G 认为这太简单了——他想找到跳跃次数为奇数的解,并请求你的帮助。

根据经验,小 G 认为找到最小跳跃次数就足以构造出更多跳跃次数的解。因此,你只需要报告所有跳跃次数为奇数的解中的最小跳跃次数,或者报告不存在这样的解。由于小 G 还没有决定 $n$ 或 $K$,他会向你提出 $T$ 个询问,每个询问指定正整数 $n$ 和 $K$。

为了帮助你(一个人类)更好地理解 H 星球居民的神秘想法:将划分出的正方形的所有顶点视为笛卡尔平面上的格点,其两个坐标均为 $[0, n]$ 中的整数。我们称这些点为“特殊点”。在两个特殊点 $I(x_1, y_1)$ 和 $T(x_2, y_2)$ 之间的跳跃是合法的,当且仅当 $|x_1 - x_2| + |y_1 - y_2| = K$。

对于每个给定的正整数 $n$ 和 $K$ 的询问:确定是否存在一个起点和终点均为某个特殊点、遵循跳跃规则且跳跃次数为奇数的环。如果存在这样的环,输出最小跳跃次数;否则,输出 $-1$。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10^5$) —— 询问的数量。

接下来的 $T$ 行,每行包含两个整数 $n$ ($1 \le n \le 10^8$) 和 $K$ ($1 \le K \le 2n$)。

输出格式

输出 $T$ 行,每行包含一个整数。如果存在跳跃次数为奇数的环,输出最小跳跃次数;否则,输出 $-1$。

样例

输入样例 1

3
1 1
2 2
4 8

输出样例 1

-1
3
-1

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.