这里是想用 线段树 维护 区间LCM ,再查询输出。
但是只有10分,剩下点全WA。
有没有大佬看看是做法有问题还是代码有问题?
拜谢
#include<bits/stdc++.h>
using namespace std;
const int maxn=300+5;
const long long mod=1e9+7;
int G,n,q;
long long a[maxn],tree[maxn<<2];
long long inv(long long a) {
long long re=1,times=a;
long long p=mod-2;
while(p) {
if(p&1) re=(re*times)%mod;
times=(times*times)%mod;
p=p>>1;
}
return re;
}
inline long long merge(long long a,long long b) {
if(a==-1) return b;
else if(b==-1) return a;
return ( ( (a*b) %mod) *inv( __gcd(a,b) ) )%mod;
}
void build(int spot,int L,int R) {
if(L==R) {
tree[spot]=a[L]%mod;
return ;
}
int mid=(L+R)>>1,lson=spot<<1,rson=lson|1;
build(lson,L,mid);
build(rson,mid+1,R);
tree[spot]=merge(tree[lson],tree[rson]);
return ;
}
long long query(int spot,int L,int R,int x,int y) {
if(y<L||R<x) return -1;
if(x<=L&&R<=y) return tree[spot];
int mid=(L+R)>>1,lson=spot<<1,rson=lson|1;
return merge(query(lson,L,mid,x,y),query(rson,mid+1,R,x,y));
}
int main() {
scanf("%d",&G);
while(G--) {
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,1,n);
while(q--) {
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",query(1,1,n,x,y));
}
}
return 0;
}