No.2324 Two Countries within UEC
Problem Statement
The Country of Sepa has $N$ towns, and the Country of Yakitori has $M$ towns. The towns of the Country of Sepa are numbered $1, 2, \cdots , N$, and the towns of the Country of Yakitori are numbered $1, 2, \cdots , M$. The friendship level between town $x$ of the Country of Sepa and town $y$ of the Country of Yakitori is $x \times y \mod P$.
You are given $Q$ queries. For the $i$-th query, output the number of towns in the Country of Yakitori whose friendship level with town $x_i$ of the Country of Sepa is $f_i$.Input
$N \ M \ P \ Q$ $x_1\ f_1$ $x_2\ f_2$ $\ \ \ \ \vdots$ $x_Q\ f_Q$
Constraints
- $1 \leq N, M \leq 10^9$
- $2 \leq P \leq 10^9$
- $1 \leq Q \leq 10^5$
- $1 \leq x_i \leq N$
- $0 \leq f_i \leq P - 1$
- $P$ is prime
- All inputs are integers
Output
On the $i$-th line, output the answer to the $i$-th query.
Sample
Sample 1
Input
5 5 11 3 2 8 4 7 5 3
Output
1 0 1
The only town with friendship level $8$ with town $2$ of the Country of Sepa is town $4$ of the Country of Yakitori, so output $1$. $\\$ No town in the Country of Yakitori has friendship level $7$ with town $4$ of the Country of Sepa, so output $0$. $\\$ The only town with friendship level $3$ with town $5$ of the Country of Sepa is town $5$ of the Country of Yakitori, so output $1$.
Sample 2
Input
85 82 17 8 55 1 23 2 31 7 83 0 8 0 76 16 32 1 14 1
Output
5 5 5 4 4 5 5 5
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
sepa38