No.849 Divide and Rule of the Yuki Kingdom
タグ : / 解いたユーザー数 52
作問者 :
chocorusk
/ テスター :
tempura_pp
Problem Statement
The Yuki Kingdom can be regarded as an infinitely extending coordinate plane, and there is exactly $1$ city on each of its lattice points (points whose $x$ and $y$ coordinates are both integers). In the Yuki Kingdom, every city has the following $4$ types of warp panels installed.
- By stepping on warp panel $1$ in city $(x, y)$, you can warp to city $(x+a, y+b)$.
- By stepping on warp panel $2$ in city $(x, y)$, you can warp to city $(x-a, y-b)$.
- By stepping on warp panel $3$ in city $(x, y)$, you can warp to city $(x+c, y+d)$.
- By stepping on warp panel $4$ in city $(x, y)$, you can warp to city $(x-c, y-d)$.
In the Yuki Kingdom live $N$ citizens numbered $1, 2, \ldots , N$, and citizen $i$ ($1\leq i\leq N$) lives in city $(x_i, y_i)$. The king, Yuki-kun, has decided to divide the $N$ citizens into groups so that the following conditions are satisfied.
- Citizen $i$ and citizen $j$ belong to the same group if and only if it is possible to move from city $(x_i, y_i)$ to city $(x_j, y_j)$ by stepping on warp panels a finite number of times (possibly $0$ times).
- Each group contains at least $1$ citizen.
It can be proved that these conditions determine the grouping uniquely and without contradiction. How many groups will the citizens be divided into?
Input
$a\ b\ c\ d$ $N$ $x_1\ y_1$ $\ldots$ $x_N\ y_N$
- $0\leq a, b, c, d\leq 10^9$
- $(a, b)\neq (0, 0)$
- $(c, d)\neq (0, 0)$
- $(a, b)\neq (c, d)$
- $1\leq N\leq 10^5$
- $0\leq x_i, y_i\leq 10^9$
- All inputs are integers.
Output
Output the number of groups the citizens are divided into.
Sample
Sample 1
Input
1 2 2 2 4 1 2 3 3 1 0 3 1
Output
2
- It is possible to move from city $(1, 2)$ to city $(1, 0)$ via $(1, 2)\to (3, 4)\to (2, 2)\to (1, 0)$, so citizen $1$ and citizen $3$ belong to the same group.
- It is possible to move from city $(3, 1)$ to city $(3, 3)$ via $(3, 1)\to (4, 3)\to (5, 5)\to (3, 3)$, so citizen $4$ and citizen $2$ belong to the same group.
- It is impossible to move from city $(1, 2)$ to city $(3, 3)$ using warp panels, so citizen $1$ and citizen $2$ belong to different groups.
Sample 2
Input
0 1 1 0 2 314159265 358979323 314159265 358979323
Output
1
Multiple citizens may live in the same city.
Sample 3
Input
2 0 3 0 5 1 0 2 3 5 4 4 3 1 2
Output
4
Sample 4
Input
7 6 8 9 10 80 0 53 48 62 51 15 27 55 18 31 79 6 81 95 76 44 99 28 84
Output
5
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。