Hdu 5726 GCD【思维+二分+区间Gcd】好题!

来源:互联网 发布:算法的效率只与 编辑:程序博客网 时间:2024/05/16 17:27

GCD

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


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
151 2 4 6 741 52 43 44 4
 

Sample Output
Case #1:1 82 42 46 1

题目大意:


给出一个长度为N的一个序列,有Q个查询,每个查询给你一个区间【L,R】表示询问这个区间的Gcd的值,以及和这个区间Gcd值相同的区间的个数。


思路:


①首先线段树或者是ST表都能够求区间Gcd.那么我们可以快速查询一个区间的Gcd的值。(线段树被卡了常- -用ST表过了= =)


②如果当前问题Q==1,我们显然可以O(n)枚举起点,然后根据Gcd的单调性(加入的元素越多,Gcd的值越小),来进行两次二分,使得找到当前问题的解,时间复杂度为O(nlogn*查询复杂度);

这里我们如果用ST表预处理的话,查询复杂度是O(1)的。

但是这个题有1e5个查询,显然O(Q*nlogn)是不行的。

那么我们考虑问题的本质。


③问题相关GCD,无非几种特性,要么去找素数,要么去看单调性(加入的元素越多,Gcd的值越小),再要么考虑暴力(加入的元素再多,我们gcd的值最多也就是log(maxnum)左右种答案);这个题显然要用到第三个特性,我们一旦确定了一个区间的起点,那么我们无论右端点在哪里,我们这个区间的值最多也就是20左右种。

那么我们考虑暴力二分。

O(n)枚举起点,进行20+次二分,使得每次找区间Gcd值变化的那个点,然后整个过程进行一下对每段区间的值的答案的统计。

时间复杂度约为O(n(logn)^2);


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>#include<map>#include<math.h>using namespace std;int n;int a[150000];struct node{    int x,y,val,pos;}b[150000];/******************************/int Gcd[400005][20];int gcd(int x,int y){    return y==0?x:gcd(y,x%y);}void ST(){    int len=floor(log10(double(n))/log10(double(2)));    for(int j=1;j<=len;j++)    {        for(int i=1;i<=n+1-(1<<j);i++)        {            Gcd[i][j]=gcd(Gcd[i][j-1],Gcd[i+(1<<(j-1))][j-1]);        }    }}int query(int a,int b){    int len= floor(log10(double(b-a+1))/log10(double(2)));    return gcd(Gcd[a][len],Gcd[b-(1<<len)+1][len]);}/******************************/int main(){    int t;    int kase=0;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=n;i++)Gcd[i][0]=a[i];        ST();        int q;scanf("%d",&q);        for(int i=1;i<=q;i++)        {            scanf("%d%d",&b[i].x,&b[i].y);            b[i].val=query(b[i].x,b[i].y);            b[i].pos=i;        }        map<int,long long int>s;        for(int i=1;i<=n;i++)        {            int pre=a[i];            int LL=i;            while(1)            {                int ans=-1;                int l=LL;                int r=n;                while(r-l>=0)                {                    int mid=(l+r)/2;                    if(query(i,mid)!=pre)                    {                        ans=mid;                        r=mid-1;                    }                    else l=mid+1;                }                if(ans==-1)                {                    s[pre]+=n-LL+1;                    break;                }                else                {                    s[pre]+=ans-1-LL+1;                    pre=query(i,ans);                    LL=ans;                }            }        }        printf("Case #%d:\n",++kase);        for(int i=1;i<=q;i++)printf("%d %lld\n",b[i].val,s[b[i].val]);    }}