求助卡常,玄 2 关。
查看原帖
求助卡常,玄 2 关。
1238611
harmis_yz楼主2024/3/1 15:08

差 1 个点,差了 0.13s。

代码:

#include<bits/stdc++.h>
using namespace std;
#define re register
#define il inline

il long long read(){
	long long x=0;
	int f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x*f;
}
il int min(int a,int b){return (a<b?a:b);}
il long long gcd(long long a,long long b){
	return !b?a:gcd(b,a%b);
}

const int N=301;
const long long p=1e9+7;
struct node{
	int l,r,id;
}Q[N];
int n,q,len;
long long a[N],ans[N];
long long b_[N],b[N];
long long S,S_,s;

il int get(int x){
	return (x-1)/len+1;
}
il bool cmp(node a,node b){
	if((a.l-1)/len!=(b.l-1)/len) return a.l<b.l;
	return a.r<b.r;
}
il long long mul(long long a,long long b,long long m)
{
    long long t=a*b-(long long)((long double)a/m*b)*m;
    return t<0?t+m:(t>=m?t-m:t);
}

signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int t=read();while(t--){
		n=read(),q=read(),len=sqrt(n),S=1;
		for(re int i=1;i<=n;++i) a[i]=read();
		for(re int i=1;i<=q;++i) Q[i]={read(),read(),i};
		sort(Q+1,Q+q+1,cmp);
		int l=1,r=0,lst_bk=0;
		for(re int i=1;i<=q;++i){
			if(get(Q[i].l)==get(Q[i].r)){
				S_=1;
				for(re int j=Q[i].l;j<=Q[i].r;++j){
					s=1;
					for(re int k=Q[i].l;k<j;++k)
						s=mul(s,b_[k],a[j]);	
					b_[j]=a[j]/gcd(a[j],s);
					S_=(S_*(b_[j]%p))%p;
				}
				ans[Q[i].id]=S_;
				continue;
			}
			if(lst_bk!=get(Q[i].l)){
				lst_bk=get(Q[i].l);
				l=min(lst_bk*len,n)+1,r=l-1;
				S=1;
			}
			for(;r<Q[i].r;){
				++r;s=1;
				for(re int k=l;k<r;++k)
					s=mul(s,b[k],a[r]);
				b[r]=a[r]/gcd(a[r],s);
				S=(S*(b[r]%p))%p;
			}
			S_=S;
			for(re int l_=l;l_>Q[i].l;){
				--l_;s=1;
				for(re int k=l_+1;k<=r;++k)
					s=mul(s,b[k],a[l_]);
				b[l_]=a[l_]/gcd(a[l_],s);
				S_=(S_*(b[l_]%p))%p;
			}
			ans[Q[i].id]=S_;
		}
		for(re int i=1;i<=q;++i) printf("%lld\n",ans[i]);
	}
	return 0;
}
2024/3/1 15:08
加载中...