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.
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).
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 4
Sample Output
Case #1: 1 8 2 4 2 4 6 1
Author
HIT
题目大意:
给你n个数,求l到r区间内的最大公约数ans,并且求任意区间的最大公约数为ans的数量
解题思路:
第一问很裸的RMQ问题,如果裸这个的话我认为是可以线段树搞的,不过洋少跟我说线段树会挂我就没写了。
第二问很蛋疼。我想了很久,看着官方题解想了很久。毕竟智商略低0.0其实很简单,对于从l到r这个区间,如果固定l区间,r区间不断变化的时候,gcd的值在不断下降,而且每次下降应该都不小于一半,那么这个下降速度是很快的。那么只需要枚举左端点l,然后二分l到n的区间,找到最大公约数为gcd的最大区间,然后记录这个gcd的数量,并且把gcd更新,这样时间复杂度能控制为nlog(a)
这道题还是很有意思的,值的推敲。
ps:写的时候sabi了一下,先计算区间个数了,然后计算的RMQ预处理。于是各种各样姿势的TLE,这比赛没法打了,,,,
代码:
#include