D. GCD of an Array

在这里插入图片描述
线段树维护每个素数区间最小个数,新学了个动态开点。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int mod=1e9+7;
const int N=2e7+11;
const int maxn=2e5+11;
const double eps=0.00000001;
int n,q,cnt,a[maxn],ind,x,p[maxn],isp[maxn],root[maxn];
int lson[N],rson[N],val[N];
ll gcd=1;
void Prime(){
	for(int i=2;i<=200000;++i){
		if(!isp[i]) p[++p[0]]=i;
		for(int j=1;j<=p[0]&&1ll*p[j]*i<=200000;++j){
			isp[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}
ll ksm(ll a,ll n){
	ll ans=1;
	while(n){
		if(n&1) ans=ans*a%mod;
		a=a*a%mod;
		n>>=1;
	}
	return ans;
}
void update(int &now,int l,int r,int x,int add){
	if(!now) now=++cnt;
	if(l==r){
		val[now]+=add;
		return;
	}
	int mid=(l+r)>>1;
	if(x<=mid) update(lson[now],l,mid,x,add);
	else update(rson[now],mid+1,r,x,add);
	val[now]=min(val[lson[now]],val[rson[now]]);
}
void gg(int i,int j,int c){
	int tmp=ksm(p[j],val[root[j]]);
	gcd=gcd*ksm(tmp,mod-2)%mod;
	update(root[j],1,n,i,c);
	tmp=ksm(p[j],val[root[j]]);
	gcd=gcd*tmp%mod;
}
void solve(){
	Prime();
	cin>>n>>q;
	for(int i=1;i<=n;++i){
		cin>>a[i];
		for(int j=1;p[j]<=sqrt(a[i]);++j){
			int c=0;
			while(a[i]%p[j]==0){
				++c;
				a[i]/=p[j];
			}
			if(c==0) continue;
			gg(i,j,c);
		}
		if(a[i]>1){
			int j=lower_bound(p+1,p+1+p[0],a[i])-p;
			gg(i,j,1);
		}
	}
	while(q--){
		cin>>ind>>x;
		for(int j=1;p[j]<=sqrt(x);++j){
			int c=0;
			while(x%p[j]==0){
				x/=p[j];
				++c;
			}
			if(c==0) continue;
			gg(ind,j,c);
		}
		if(x>1){
			int j=lower_bound(p+1,p+1+p[0],x)-p;
			gg(ind,j,1);
		}
		cout<<gcd<<"\n";
	}
}
int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);
	solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值