GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Give you a sequence of $N(N≤100,000)$ integers : $a_1,\cdots,a_n(0<a_i≤1000,000,000)$. There are $Q(Q≤100,000)$ queries. For each query $l,r$ you have to calculate $\text{gcd}(a_l,,a_{l+1},\cdots,a_r)$ and count the number of pairs$(l′,r′)(1≤l<r≤N)$such that $\text{gcd}(a_{l′},a_{l′+1},\cdots,a_{r′})$ equal $\text{gcd}(a_l,a_{l+1},...,a_{r})$.
Input
The first line of input contains a number $T$, which stands for the number of test cases you need to solve.
The first line of each case contains a number $N$, denoting the number of integers.
The second line contains $N$ integers, $a_1,\cdots,a_n(0<a_i≤1000,000,000)$.
The third line contains a number $Q$, denoting the number of queries.
For the next $Q$ lines, $i\text{-th}$ line contains two number , stand for the $l_i,r_i$, stand for the $i\text{-th}$ queries.
Output
For each case, you need to output “Case #:t” at the beginning.(with quotes, t means the number of the test case, begin from 1).
For each query, you need to output the two numbers in a line. The first number stands for $\text{gcd}(a_l,a_{l+1}, \cdots,a_r)$ and the second number stands for the number of pairs$(l′,r′)$ such that $\text{gcd}(a_{l′},a_{l′+1},\cdots,a_{r′})$ equal $\text{gcd}(a_l,a_{l+1},\cdots,a_r)$.
Sample Input
1
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4
Sample Output
Case #1:
1 8
2 4
2 4
6 1
Author
HIT
Source
2016 Multi-University Training Contest 1
题意:
支持查询: (1) 区间gcd, (2) gcd值等于k的区间数
Solution:
区间gcd的查询线段树即可解决, 另外还能支持单点修改. 但这题要求支持查询gcd值等于k的区间个数, 线段树就有点乏力了, 因为这个信息大概不太好通过合并区间信息来得到. 我们来考虑区间gcd的性质:
令$\text{gcd}_r(l)$表示, l到r的gcd.我们