hdu5726

本文介绍了一种高效的区间GCD预处理算法,通过构建线段树并利用质因数分解特性,在log级别时间内处理所有可能的区间GCD值,并使用哈希表记录不同GCD值出现的次数,加速查询过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们注意观察gcd(a_{l},a_{l+1},…,a_{r})gcd(a​l​​ ,a​l+1​​ ,…,a​r ),当l固定不动的时候,r=l…nr=l…n时,我们可以容易的发现,随着rr的増大,gcd(a_{l},a_{l+1},…,a_{r})gcd(a​l​​ ,a​l+1​​ ,…,a​r​​ )是递减的,同时gcd(a_{l},a_{l+1},…,a_{r})gcd(a​l ,a​l+1​​ ,…,a​r​​ )最多 有log\ 1000,000,000log 1000,000,000个不同的值,为什么呢?因为a_{l}a​l
​​ 最多也就有log\ 1000,000,000log 1000,000,000个质因数
所以我们可以在log级别的时间处理出所有的以L开头的左区间的gcd(a_{l},a_{l+1},…,a_{r})gcd(a​l​​ ,al+1​​ ,…,a​r​​ ) 那么我们就可以在n\ log\ 1000,000,000n log 1000,000,000的时间内预处理出所有的gcd(a_{l},a_{l+1},…,a_{r})gcd(a​l​​ ,a​l+1​​ ,…,a​r​​ )然后我们可以用一个map来记录,gcd值为key的有多少个 然后我们就可以对于每个询问只需要查询对应gcd(a_{l},a_{l+1},…,a_{r})gcd(a​l​​ ,a​l+1​​ ,…,a​r​​ )为多少,然后再在map 里面查找对应答案即可.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

typedef long long ll;
const int maxn=1000010;
struct node
{
    int l,r;
    ll gcd;
} Tree[maxn<<2];
ll a[maxn];
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
void pushup(int rt)
{
    Tree[rt].gcd=gcd(Tree[rt*2].gcd,Tree[rt*2+1].gcd);
}
void Build(int l,int r,int rt)
{
    Tree[rt].l=l,Tree[rt].r=r;
    if(l==r)
    {
        Tree[rt].gcd=a[l];
        return ;
    }
    int mid=(l+r)/2;
    Build(l,mid,rt*2);
    Build(mid+1,r,rt*2+1);
    pushup(rt);
}
ll queryans(int L,int R,int rt)
{
    if(L<=Tree[rt].l&&Tree[rt].r<=R)
    {
        return Tree[rt].gcd;
    }
    int mid=(Tree[rt].l+Tree[rt].r)/2;
    ll ans=0;
    if(L<=mid) ans=gcd(ans,queryans(L,R,rt*2));
    if(mid<R)  ans=gcd(ans,queryans(L,R,rt*2+1));
    return ans;
}
int n;
map<ll,ll>ans;
map<ll,ll>mp1;//mp1代表以x[i]结尾的所有区间的gcd的个数
map<ll,ll>mp2;//临时变量

int main()
{
    int T;
    scanf("%d",&T);
    int cas=1;
    while(T--)
    {
        scanf("%d",&n);
        ans.clear();
        mp1.clear();
        mp2.clear();
        for(int i=1; i<=n; i++) scanf("%I64d",&a[i]);
        Build(1,n,1);
        //puts("success");
        mp1[a[1]]++;
        ans[a[1]]++;
        for(int i=2; i<=n; i++)
        {
            ll now=a[i];
            mp2[now]++;
            ans[now]++;
            for(auto it=mp1.begin(); it!=mp1.end(); it++)
            {
                int nex=gcd(now,it->first);
                ans[nex]+=it->second;
                mp2[nex]+=it->second;
            }
            mp1.clear();
            for(auto it=mp2.begin(); it!=mp2.end(); it++)
            {
                mp1[it->first]=it->second;
            }
            mp2.clear();
        }
        int q;
        printf("Case #%d:\n",cas++);
        scanf("%d",&q);
        while(q--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            ll temp=queryans(l,r,1);
            printf("%I64d %I64d\n",temp,ans[temp]);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值