hdu 5726 RMQ+二分

GCD

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


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

题意:

这是2016多校第一场的1004题,当时没写出来,题意就是求给定区间的gcd值以及与gcd为该值的区间的个数。

思路:

随着区间的扩大,区间gcd值一定是减小的,在减小的情况下,每次至少要减小一个因数,因此最多只会有因数个区间内gcd值是不同的。然后具体处理就是了,具体看代码。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
#include<queue>
#include<vector>
#include<list>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;

#define INF 0x3f3f3f3f
const int MAXN =100010;
const int mod=1000007;
int dp[MAXN][20];
int mm[MAXN];
void initRMQ(int n,int b[])
{
    mm[0]=-1;
    for(int i=1;i<=n;i++)
    {
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        dp[i][0]=b[i];
    }
    for(int j=1;j<=mm[n];j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
            dp[i][j]=__gcd(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq(int x,int y)
{
    int k=mm[y-x+1];
    return __gcd(dp[x][k],dp[y-(1<<k)+1][k]);
}
int n,q;
int a[MAXN];
struct node
{
    int d;
    long long cnt;
};
list<node> lis[mod];
long long & get(int d)
{
    int h=d%mod;
    for(list<node>::iterator it=lis[h].begin();it!=lis[h].end();it++)
    {
        if((*it).d==d) return (*it).cnt;
    }
    lis[h].push_front((node){d,0});
    for(list<node>::iterator it=lis[h].begin();it!=lis[h].end();it++)
    {
        if((*it).d==d) return (*it).cnt;
    }
}
int half_find(int s,int l,int r,int d)
{
    int mid;
    while(l<=r)
    {

        mid=(l+r)>>1;
        if(rmq(s,mid)>=d) l=mid+1;
        else r=mid-1;
    }
    return l;
}
int main()
{
    int t,tt=0;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<mod;i++) lis[i].clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        initRMQ(n,a);
        for(int i=1;i<=n;i++)
        {
            int l,r=i,d=a[i];
            while(r<=n)
            {
                l=r;
                r=n;
                r=half_find(i,l,r,d);

                long long& cnt=get(d);
                cnt+=r-l;
                d=rmq(i,r);
            }
        }
        printf("Case #%d:\n",++tt);
        scanf("%d",&q);
        for(int i=1;i<=q;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            int d=rmq(x,y);
            long long cnt=get(d);
            printf("%d %lld\n",d,cnt);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值