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";
}
}