Z0637. 静态区间

Description

给你N个数字a[1],a[2]..........a[n]

M次询问,每次给定一个区间[L,R]

求a[L]......a[R]的最大公约数

Format

Input

第一行给出数字N,M

第二行N个数字,其值<=1e9

接下来M对数字

N<=5e4

M<=1e5

Output

输出M个行,每行一个数字

Samples

输入数据 1

5 3
4 12 3 6 7
1 3
2 3
5 5

输出数据 1 

1
3
7

 思路

运用倍增思想,用一个数组统计公约数,最后输出

可以先看看这个

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}
int n,q;
int g[500500][16],s[16];
signed main(){
	n = read();q = read();
    s[0] = 1;
    for(int i=1;i<=15;i++)s[i] = s[i-1]*2;
    for(int i=1;i<=n;i++) g[i][0] = read();
    int x = log2(n);
    for(int i=1;i<=x;i++) for(int j=1;j+s[i]-1<=n;j++) g[j][i] = __gcd(g[j][i-1],g[j+s[i-1]][i-1]);
    for(int i=1;i<=q;i++){
        int l,r;
        l = read();r = read();
        x = log2(r-l+1);
        int t1 = __gcd(g[l][x],g[r-s[x]+1][x]);
        cout<<t1<<"\n";
    }
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值