#GL0000370. Ước chung lớn nhất của mảng (GCD of an Array)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ước chung lớn nhất của mảng (GCD of an Array)
GCD of an Array
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of positive integers and operations performed in order. An operation provides and replaces by , leaving every other element unchanged.
After each operation, find . Array elements may become extremely large, so output only the answer modulo .
For example, starting with , multiplying the first element by gives , whose GCD is . Multiplying the second element by leaves the GCD equal to . Multiplying the third element by then makes the GCD equal to .
Important: Do not reduce individual array elements modulo before computing their GCD. The modulus is applied only to the reported answer.
Input
The first line contains . The second line contains values . Each of the next lines contains , where indices are -based.
Output
Print exactly lines. Line is the GCD after operation , modulo .
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Example 1
Input
4 3
1 6 8 12
1 12
2 3
3 3
Output
2
2
6
Explanation
The arrays after the three operations are , , and ; their GCDs are .