#DSU0000023. Đồ thị khổng lồ (Large Graph)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đồ thị khổng lồ (Large Graph)
Large Graph
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of positive integers and an integer . Construct an matrix by circular shifts: the first row is , the second row is shifted one position to the right, the third row is shifted two positions, and so on. An element moving past the right end reappears at the left end.
Consider an undirected graph with one vertex for each matrix cell, thus vertices in total. Two different cells and are joined by an edge if both and . The distance is ordinary Manhattan distance on matrix coordinates; it does not wrap across matrix boundaries.
Determine the number of connected components of this graph. Only the count is required; you do not need to output the components.
Input
The first line contains integers . The second line contains integers .
Output
Print one integer: the number of connected components.
Subtasks
- Subtask 1 (20 points): , , .
- Subtask 2 (30 points): , , .
- Subtask 3 (50 points): , , .
Examples
Input
3 3
3 4 9
Output
2
Explanation
The matrix consists of cyclic shifts of . Cells holding and are connected through steps with gcd greater than one; cells holding form the other component. The answer is .