HDU5726 线段树求解区间GCD
GCD
Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5302????Accepted Submission(s): 1908
Problem Description
Give you a sequence of?N(N≤100,000)?integers :?a1,...,an(0<ai≤1000,000,000). There are?Q(Q≤100,000)?queries. For each query?l,r?you have to calculate?gcd(al,,al+1,...,ar)?and count the number of pairs(l′,r′)(1≤l<r≤N)such that?gcd(al′,al′+1,...,ar′)?equal?gcd(al,al+1,...,ar).
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,?a1,...,an(0<ai≤1000,000,000).
The third line contains a number?Q, denoting the number of queries.
For the next?Q?lines, i-th line contains two number , stand for the?li,ri, stand for the i-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?gcd(al,al+1,...,ar)?and the second number stands for the number of pairs(l′,r′)?such that?gcd(al′,al′+1,...,ar′)?equal?gcd(al,al+1,...,ar).
Sample Input
1 5 1 2 4 6 7 4 1 5 2 4 3 4 4 4Sample Output
Case #1: 1 8 2 4 2 4 6 1Author
HIT
Source
2016 Multi-University Training Contest 1
Recommend
wange2014???|???We have carefully selected several similar problems for you:??6396?6395?6394?6393?6392?
線段樹求解區間GCD
題意:
給n個數,q次查詢,首先問區間[l,r]的gcd(a[l],a[l+1],a[l+2]....a[r-1],a[r]),然后輸出與區間[l,r]的gcd相等的區間的個數
第一問用線段樹求解區間,第二問就是先預處理出以i為開頭的區間的各自的GCD的值,用線段樹來維護區間GCD和查詢
?
總結
以上是生活随笔為你收集整理的HDU5726 线段树求解区间GCD的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU1878 欧拉回路
- 下一篇: 沉默是金 矩阵快速幂