码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5726 GCD

时间:2016-08-05 22:47:28      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

传送门

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.我们

 

 
 

HDU 5726 GCD

标签:

原文地址:http://www.cnblogs.com/Patt/p/5742814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!