GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1240 Accepted Submission(s): 382
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
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
map<ll,ll> mapp;
map<ll,ll> map1;
map<ll,ll> map2;
int node[1000010*4];
int a[1000010];
int cnt=1;
map <ll,ll> ::iterator it;
//int gcd(int a,int b)
//{
// if(b==0)
// return a;
// return gcd(b,a%b);
//}
void build(int n,int l,int r)
{
if(l==r)
{
node[n]=a[cnt++];
return ;
}
int m=(l+r)/2;
build(n*2,l,m);
build(n*2+1,m+1,r);
node[n]=__gcd(node[n*2],node[n*2+1]);
}
int query(int p,int q,int n,int l,int r)
{
//printf("lalalall\n");
if(p<=l&&q>=r)
{
return node[n];
}
int m=(l+r)/2;
int ret=0;
if(p<=m)
ret=__gcd(ret,query(p,q,n*2,l,m));
if(q>m)
ret=__gcd(ret,query(p,q,n*2+1,m+1,r));
return ret;
}
int main()
{
int t;
scanf("%d",&t);
for(int kase=1;kase<=t;kase++)
{
memset(a,0,sizeof(a));
memset(node,0,sizeof(node));
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,1,n);
mapp.clear();
map1.clear();
map2.clear();
mapp[a[1]]++;
map1[a[1]]++;
for(int i=2;i<=n;i++)
{
ll mm=a[i];
mapp[mm]++;
map2[mm]++;
for(it=map1.begin();it!=map1.end();it++)
{
int temp=__gcd(mm,it->first);
mapp[temp]+=it->second;
map2[temp]+=it->second;
}
map1.clear();
map1=map2;
map2.clear();
}
printf("Case #%d:\n", kase);
scanf("%d",&m);
while(m--)
{
int p,q;
scanf("%d%d",&p,&q);
ll ans=query(p,q,1,1,n);
printf("%I64d %I64d\n",ans,mapp[ans]);
}
cnt=1;
}
return 0;
}
借鉴学长的代码,思想真的不错,用map来打表存值,线段树保存gcd。注意每次都把cnt初始化哦。。。嗯嗯,附上链接http://blog.csdn.net/gscsdlz?viewmode=contents