hdu 5726 gcd rmq 求相同gcd的区间数量

9 篇文章 0 订阅
8 篇文章 0 订阅

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

题意:给一个数组a,大小为n,接下来有m个询问,每次询问给出l、r,定义f[l,r]=gcd(al,al+1,…,ar),问f[l,r]的值 和 有多少对(l’,r’)使得f[l’,r’]=f[l,r]。n<=10万,m<=10万,1<=l<=r<=n,1<=l’<=r’。

思路:

  第一步比较简单,预处理一下,定义f[i][j]为:ai开始,连续2^j个数的最大公约数,所以f[1][0]=a[1],f[1][1]=gcd(a1,a2),f[1][2]=gcd(a1,a2,a3,a4)。其实就是动态规划,让i从1-n,让j从0-17,递推上去即可。

  递推公式如下:

  1. f[i][0]=a[i];

  2. f[i][j]=gcd(f[i][j-1],f[i+(1 << j-1)][j-1]);

  就如同f[1][2]=gcd(f[1][1],f[3][1])=gcd(gcd(f[1][0],f[2][0]),gcd(f[3][0],f[4][0]));

  通过上述预处理,查询时就只需O(logn)时间,如下:

  令k=log2(r-l+1),look(l,r)=gcd(f[l][k],f[r-(1 << k)+1][k]);

  注:f[l][k] 和 f[r-(1 << k)+1][k]可能会有重叠,但不影响最终的gcd值。

  比赛时第二步没想出来,太可惜了。。。

  第二步,我们可以枚举左端点 i 从1-n,对每个i,二分右端点,计算每种gcd值的数量,因为如果左端点固定,gcd值随着右端点的往右,呈现单调不增,而且gcd值每次变化,至少除以2,所以gcd的数量为nlog2(n)种,可以开map < int,long long > 存每种gcd值的数量,注意n大小为10万,所以数量有可能爆int。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int gd[100100][18];
map<int,long long> mp;

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

int a[100100];

void ST()
{
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            gd[i][j]=gcd(gd[i][j-1],gd[i+(1<<(j-1))][j-1]);
        }
    }
}

int rmq(int i,int j)
{
    int k=0;
    while((1<<(k+1))<=j-i+1) k++;
    return gcd(gd[i][k],gd[j-(1<<k)+1][k]);
}


int main()
{
    int t;
    scanf("%d",&t);
    for(int cc=1;cc<=t;cc++)
    {
        mp.clear();
        printf("Case #%d:\n",cc );
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            gd[i][0]=a[i];
        }
        ST();
        for(int i=1;i<=n;i++)
        {
            int l=i,r=n;
            int g=a[i];
            while(l<=n)
            {
            int ll=l;
            while(l<=r){
                int mid=(l+r)>>1;
                if(rmq(i,mid)==g)
                    l=mid+1;
                else r=mid-1;
            }
            mp[g]+=(r-ll+1);    
            g=gcd(g,a[l]);  
            r=n;
            }   
        }
        int q;
        scanf("%d",&q);
        while(q--){
            int c,d;
            scanf("%d%d",&c,&d);
            int res=rmq(c,d);
            printf("%d %lld\n",res,mp[res]);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值