A15948 | The Butcher
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Anton plays his favorite game "Defense of The Ancients 2" for his favorite hero — The Butcher. Now he wants to make his own dinner. To do this he will take a rectangle of height $h$ and width $w$ , then make a vertical or horizontal cut so that both resulting parts have integer sides. After that, he will put one of the parts in the box and cut the other again, and so on.
More formally, a rectangle of size $h \times w$ can be cut into two parts of sizes $x \times w$ and $(h - x) \times w$ , where $x$ is an integer from $1$ to $(h - 1)$ , or into two parts of sizes $h \times y$ and $h \times (w - y)$ , where $y$ is an integer from $1$ to $(w - 1)$ .
He will repeat this operation $n - 1$ times, and then put the remaining rectangle into the box too. Thus, the box will contain $n$ rectangles, of which $n - 1$ rectangles were put in the box as a result of the cuts, and the $n$ -th rectangle is the one that the Butcher has left after all $n - 1$ cuts.
Unfortunately, Butcher forgot the numbers $h$ and $w$ , but he still has $n$ rectangles mixed in random order. Note that Butcher didn't rotate the rectangles, but only shuffled them. Now he wants to know all possible pairs $(h, w)$ from which this set of rectangles can be obtained. And you have to help him do it!
It is guaranteed that there exists at least one pair $(h, w)$ from which this set of rectangles can be obtained.
More formally, a rectangle of size $h \times w$ can be cut into two parts of sizes $x \times w$ and $(h - x) \times w$ , where $x$ is an integer from $1$ to $(h - 1)$ , or into two parts of sizes $h \times y$ and $h \times (w - y)$ , where $y$ is an integer from $1$ to $(w - 1)$ .
He will repeat this operation $n - 1$ times, and then put the remaining rectangle into the box too. Thus, the box will contain $n$ rectangles, of which $n - 1$ rectangles were put in the box as a result of the cuts, and the $n$ -th rectangle is the one that the Butcher has left after all $n - 1$ cuts.
Unfortunately, Butcher forgot the numbers $h$ and $w$ , but he still has $n$ rectangles mixed in random order. Note that Butcher didn't rotate the rectangles, but only shuffled them. Now he wants to know all possible pairs $(h, w)$ from which this set of rectangles can be obtained. And you have to help him do it!
It is guaranteed that there exists at least one pair $(h, w)$ from which this set of rectangles can be obtained.
输入格式
Each test consists of multiple test cases. The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of rectangles obtained.
The $i$ -th of the next $n$ lines contains two integers $a_i$ and $b_i$ ( $1 \le a_i, b_i \le 10^6$ ) — the height and width of the $i$ -th rectangle.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of rectangles obtained.
The $i$ -th of the next $n$ lines contains two integers $a_i$ and $b_i$ ( $1 \le a_i, b_i \le 10^6$ ) — the height and width of the $i$ -th rectangle.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, on the first line output a single integer $m$ — the number of pairs $(h, w)$ denoting the sizes of rectangles from which the given rectangles can be obtained. Two rectangles are considered different if they have different heights or widths.
On each of the following $m$ lines print output integers $h_i$ and $w_i$ — the height and width of the rectangle from which the given rectangles can be obtained. You can output the rectangles in any order.
On each of the following $m$ lines print output integers $h_i$ and $w_i$ — the height and width of the rectangle from which the given rectangles can be obtained. You can output the rectangles in any order.
输入输出样例
输入 #1
4 3 1 2 3 5 1 3 3 1 1 1 1 1 1 1 10 10 4 3 2 5 5 2 2 8 7
输出 #1
1 4 5 2 1 3 3 1 1 10 10 1 13 7
In the first test case, Butcher could only have a rectangle of size $4 \times 5$ . Then the cuts could look like this (first the green cut was made, then the red one):
In the second test case, Butcher could have either a rectangle of $1 \times 3$ or $3 \times 1$ . The cuts would have looked like this (first the green cut was made, then the red cut):
In the third test case, Butcher did not make any cuts, so the rectangle is $10 \times 10$ .
In the second test case, Butcher could have either a rectangle of $1 \times 3$ or $3 \times 1$ . The cuts would have looked like this (first the green cut was made, then the red cut):
In the third test case, Butcher did not make any cuts, so the rectangle is $10 \times 10$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted