HDU5726 GCD【RMQ+二分】

题目地址
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1167 Accepted Submission(s): 359

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.

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

蛋疼的多校联合Orz
翻了各路大神的题解之后才勉强撸出了代码。
人生第一次RMQ。
首先是对于第一问,用RMQ预处理出该问题的答案即可。虽然线段数也可以不过感觉稍微写丑了就会TLE想想还是不敢用,于是就去学习了一波RMQ,效果拔群。(果然还是倍增分块大法好23333
对于第二问,我们可以知道当l固定时,随着r的递增,gcd一直处于非上升的状态,于是考虑用二分来找出所有的gcd变化点。利用刚刚预处理好的RMQ便可以比较快地完成这个过程,对于每个l来说,gcd的变化点最多不会超过log(1e9)的(因为a的质因数最多只有log(a)个并且只有a为2的k次幂的时候才会达到log(a)个),所以总的时间复杂度可以认为是O(nlogn*log(1e9)),而log(1e9)=32,所以还是可以很快的。
然后对于每一个l,处理好变化点之后用一个map记录gcd为某个值的时候有多少个区间就行了。
于是对于每一个询问,便可以以近乎O(1)的时间来回答了。
以下贴代码,丧心病狂状态赶出来的代码,略丑Orz

#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
using namespace std;
long long d[100005][20];
long long a[100005];
int n;
map<int,long long> mp;
long long gcd(long long a,long long b)
{
    long long temp;
    while(b)
    {
        temp=b;
        b=a%b;
        a=temp;
    }
    return a;
}
void ST()
{
    int k=(int)(log2((double)n))+1;
    for(int i=n-1;i>=0;i--)
    {
        d[i][0]=a[i];
        for(int j=1;j<=k;j++)
        {
            if(i+(1<<(j-1))>=n)d[i][j]=d[i][j-1];
            else d[i][j]=gcd(d[i][j-1],d[i+(1<<(j-1))][j-1]);
        }
    }
}
long long Find(int i,int j)
{
    int k=(int)(log2(j-i+1));
    return gcd(d[i][k],d[j-(1<<k)+1][k]);
}
void PSolve()
{
    ST();
    for(int i=0;i<n;i++)
    {
        int j=i;
        long long g=a[i];
        while(1)
        {
            int l=j,r=n;
            while(r-l-1)
            {
                int m=(l+r)/2;
                if(Find(i,m)==g)l=m;
                else r=m;
            }
            mp[g]+=(r-j);
            if(r==n)break;
            g=Find(i,r);
            j=r;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lld",&a[i]);
        mp.clear();
        PSolve();
        int m;
        scanf("%d",&m);
        printf("Case #%d:\n",cas);
        for(int i=0;i<m;i++)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            l--;r--;
            printf("%lld %lld\n",Find(l,r),mp[Find(l,r)]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值