HDU 5726 GCD

GCD

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4141    Accepted Submission(s): 1457

Problem Description
Give you a sequence of N(N100,000) integers : a1,...,an(0<ai1000,000,000). There are Q(Q100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs(l,r)(1l<rN)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<ai1000,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 4
 
Sample Output
Case #1:
1 8
2 4
2 4
6 1
 
Author
HIT
Source
 
题解:
第一步:
    RMQ,不解释。
第二步:
     我们可以枚举左端点 i 从1-n,对每个i,二分右端点,计算每种gcd值的数量,因为如果左端点固定,gcd值随着右端点的往右,呈现单调不增,而且gcd值每次变化,至少除以2,所以gcd的数量为nlog2(n)种,可以开map<int,long long>存每种gcd值的数量,注意n大小为10万,所以数量有可能爆int。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
int t,n,q,l,r;
map<int,ll> mp;
int a[100005];
int f[100005][25];

int gcd(int a ,int b)
{
   return b==0?a:gcd(b,a%b);
}

void RMQ()
{
    rep(j,1,20)
        rep(i,1,n)
            if(i + (1 << j)-1<=n)
                f[i][j] = gcd(f[i][j-1], f[i + (1 << (j - 1))][j - 1]);
}

int query(int l,int r)
{
    int k=(int)(log(r-l+1.0)/log(2.0));
   return gcd(f[l][k], f[r - (1 << k) +1][k]);
}
void pre()
{
    int l,r,mid,pos;
    rep(i,1,n)
    {
         pos=i;
        while(pos<=n)
        {
            l=pos; r=n;
            int k=query(i,pos);
            while(l<r)
            {
                mid=(l+r+1)/2;
                if (query(i,mid)<k) r=mid-1;
                   else l=mid;
            }
            mp[k]+=(l-pos+1);  pos=l+1;;
        }
    }
}
int main()
{
    scanf("%d",&t);
    rep(cas,1,t)
    {
        mp.clear();
        scanf("%d",&n);
        rep(i,1,n)
        {
            scanf("%d",&a[i]);
            f[i][0]=a[i];
        }
        RMQ();
        pre();
        scanf("%d",&q);
        printf("Case #%d:\n",cas);
        rep(i,1,q)
        {
            scanf("%d%d",&l,&r);
            int ans=query(l,r);
            printf("%d %lld\n",ans,mp[ans]);
        }
    }
    return 0;
}

 

posted on 2017-07-28 13:27  Yxter  阅读(118)  评论(0编辑  收藏  举报

导航