GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1587 Accepted Submission(s): 515
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
题意:给你n个数,然后给你一对l,r,求这n个数的子区间中gcd值等于gcd(l,l+1,...,r)的个数
思路:首先我们考虑求出一个区间的gcd,这里可以用线段树求出,主要的就是查询符合条件的子区间的个数,这里需要预处理所有子区间了,小于N的子区间的gcd种类数最多为logN个,所以说我们用map存储gcd的个数和种类,以a[i]为右端点进行扫描,右端点不断向后移动,以a[i]为右端点的gcd=gcd(以a[i-1]为右端点的gcd,a[i]),我们通过记录以前一个元素为右端点的gcd,然后扫描就可以了,因为最多会有logn个,这样就可以求出所有子区间的gcd种类和个数,最后查询的时候O(1)的复杂度,总的复杂度O(nlognlogn)。
ac代码:
Case #1: 1 8 2 4 2 4 6 1
题意:给你n个数,然后给你一对l,r,求这n个数的子区间中gcd值等于gcd(l,l+1,...,r)的个数
思路:首先我们考虑求出一个区间的gcd,这里可以用线段树求出,主要的就是查询符合条件的子区间的个数,这里需要预处理所有子区间了,小于N的子区间的gcd种类数最多为logN个,所以说我们用map存储gcd的个数和种类,以a[i]为右端点进行扫描,右端点不断向后移动,以a[i]为右端点的gcd=gcd(以a[i-1]为右端点的gcd,a[i]),我们通过记录以前一个元素为右端点的gcd,然后扫描就可以了,因为最多会有logn个,这样就可以求出所有子区间的gcd种类和个数,最后查询的时候O(1)的复杂度,总的复杂度O(nlognlogn)。
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
struct s
{
int num,l,r;
}tree[MAXN*4];
//mp1存储以a[i-1]为右端点的gcd种类及个数,mp2记录以a[i]为右端点的gcd种类及个数,为临时中间变量,cnt,记录gcd种类及个数
map<ll,ll>mp1,mp2,cnt;
map<ll,ll>::iterator it;
ll a[MAXN];
void build(int i,int L,int R)
{
tree[i].l=L;tree[i].r=R;
if(L==R)
{
tree[i].num=a[L];
}
else
{
int mid=(L+R)/2;
build(i*2,L,mid);build(i*2+1,mid+1,R);
tree[i].num=gcd(tree[i*2].num,tree[i*2+1].num);
}
}
ll query(int i,int L,int R)
{
if(tree[i].l==L&&tree[i].r==R)
{
return tree[i].num;
}
if(R<=tree[i*2].r)
return query(i*2,L,R);
else if(L>=tree[i*2+1].l)
return query(i*2+1,L,R);
else
{
int mid=(tree[i].l+tree[i].r)/2;
return gcd(query(i*2,L,mid),query(i*2+1,mid+1,R));
}
}
int main()
{
int t;scanf("%d",&t);
int cas=0;
while(t--)
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
build(1,1,n);mp1.clear();mp2.clear();cnt.clear();
for(int i=1;i<=n;i++)
{
cnt[a[i]]++;
mp2[a[i]]++;
for(it=mp1.begin();it!=mp1.end();it++)
{
ll k=gcd(a[i],it->first);
cnt[k]+=it->second;
mp2[k]+=it->second;
}
mp1.clear();
for(it=mp2.begin();it!=mp2.end();it++)
{
mp1[it->first]=it->second;
}
mp2.clear();
}
int m;scanf("%d",&m);
printf("Case #%d:\n",++cas);
while(m--)
{
int L,R;scanf("%d%d",&L,&R);
ll ans=query(1,L,R);
printf("%I64d %I64d\n",ans,cnt[ans]);
}
}
return 0;
}