题目:
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3531 Accepted Submission(s): 1265
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
Source
Recommend
给定n≤10^5个数,|Ai|≤10^9,以及Q ≤ 10^5次询问
定义gcd(l, r)=gcd(al, al+1, ⋯, ar)
对于每次询问输出gcd(l, r),以及整个区间内有多少个区间的gcd(l’, r’)为这个值。
分析:
gcd(l, r)不难求 由于左端点固定时区间gcd也具有单调性(单调不增,而且每次减小至少衰减为原来的1/2),可以用st表预处理一下,然后以O(1)的时间复杂度进行查询
关键是第二个问题(对于我而言略难..二分学得不扎实...过程想了好一会儿才想清楚) 整个区间内有多少个区间的gcd(l’, r’)为这个值
思路还是预处理 对于从1到n的每一个点,分别以该点为左端点,
可以用map<int,long long>记录预处理结果 first表示每种不同的gcd值,second表示具有该gcd值的区间个数
代码:
Orz http://blog.csdn.net/became_a_wolf/article/details/52529697
代码清晰易懂(尽管讲解有些小的瑕疵) 几乎照搬了....
#include<bits/stdc++.h>
using namespace std;
int n,m,a[100010];
int f[100010][17];
map<int,long long> mp;
int gcd(int a,int b){ return b?gcd(b,a%b):a; }
void rmq(){
for(int j=1;j<=n;j++) f[j][0]=a[j];
for(int i=1;i<18;i++){
for(int j=1;j<=n;j++){
if(j+(1<<i)-1 <= n) f[j][i]=gcd(f[j][i-1],f[j+(1<<i-1)][i-1]);
}
}
}
int look(int l,int r){
int k=(int)log2((double)(r-l+1));
return gcd(f[l][k],f[r-(1<<k)+1][k]);
}
void setTable(){
mp.clear();//多组样例 记得每次清空mp
for(int i=1;i<=n;++i){//枚举每一个左端点 记录所有以该点为左端点可能出现的区间gcd值
int findnum=f[i][0],j=i;//设置初始(最大)查找值为该点本身值
while(j<=n){
int l=j,r=n;
while(l<r){//二分寻找最远的右端点使得gcd[l,r]=findnum
int mid=(l+r+1)>>1;
if(look(i,mid)==findnum) l=mid;
else r=mid-1;
}
mp[findnum]+=l-j+1;
j=l+1;
findnum=look(i,j);//记录下一个gcd值
}
}
}
int main(){//2480MS 15036K
int t,l,r;
int cas=1;
scanf("%d",&t);
while(t--){
printf("Case #%d:\n",cas++);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
rmq();
setTable();
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d%d",&l,&r);
int g=look(l,r);
printf("%d %I64d\n",g,mp[g]);
}
}
return 0;
}