AtCoder Regular Contest 182
F
Graph of Mod of Linear
AtCoder
⌀ 3837
原题
← E→
翻译中…

Problem Statement

翻译中…

You are given integers $N$ and $Q$, and two integer sequences of length $Q$: $A=(A_1,A_2,\ldots,A_Q)$ and $B=(B_1,B_2,\ldots, B_Q)$.

翻译中…

For each $k=1,2,\ldots,Q$, solve the following problem:

翻译中…

There is an undirected graph with $N$ vertices numbered from $0$ to $N-1$ and $N$ edges. The $i$-th edge $(0 \le i < N)$ connects vertices $i$ and $(A_k \times i + B_k) \mod N$ bidirectionally. Find the number of connected components in this undirected graph.

翻译中…

Constraints

翻译中…
  • $1 \le N \le 10^6$
  • $1 \le Q \le 10^5$
  • $0 \le A_k < N$
  • $0 \le B_k < N$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N Q
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

翻译中…

Output

翻译中…

Print $Q$ lines. The $i$-th line should contain the answer for $k=i$.

翻译中…

样例

Input #1
6 3
2 1
0 1
1 0
Output #1
2
1
6
For $k=1$, the graph has the following two connected components: - A connected component with vertices $0,1,3,4$. - A connected component with vertices $2,5$. Thus, the answer for $k=1$ is $2$.
Input #2
11 3
9 1
5 3
8 0
Output #2
3
3
2
For $k=1$, the graph has the following three connected components: - A connected component with vertices $0,1,3,6,10$. - A connected component with vertices $2,5,7,8,9$. - A connected component with vertex $4$. Thus, the answer for $k=1$ is $3$.
Input #3
182 3
61 2
77 88
180 55
Output #3
36
14
9
题目信息
题号
arc182_f
时间限制
6 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。