\(Description:\)
给出一个长度为n的序列,给出m次询问,每次询问包含三个数,区间左右端点和一个整数d
求区间\(\prod_{i=l}^{r}a_i\)是否可以被d整除,
\(Sample\) \(Input:\)
5 4
6 4 7 2 5
1 2 24
1 3 18
2 5 17
3 5 35
\(Sample\) \(Output:\)
Yes
No
No
Yes
\(Solution:\)
这题有点鬼。。。记录一下这个做法。
拿到题目后不久,我们就可以发现这题如果要整除,那么算出来的积必须包含d的质因数并且,次数要大于d的质因数次数。
那么考虑对每个数质因数分解,那么把每个质因数存下来,再用线段树维护十分困难,并且会时空双开花。。。
我们想到一种办法我们把小于\(\sqrt{n}\)的用vector存下他出现的位置。
然后在对d质因数分解的时候,lower_bound和upper_bound查询l,r在当前质因数的vector的位置
判断l,r之间的数的个数是否满足大于等于d的质因数次数。
因为保证递增所以不会wei掉。
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
const int N=2e5,M=447,K=86;
int a[N+5];
vector <int> v[N+5];
inline int query(int l,int r,int d){
return upper_bound(v[d].begin(),v[d].end(),r)-lower_bound(v[d].begin(),v[d].end(),l);
}
inline void divide(int x,int oo){
for(int i=2;i*i<=x;++i){
if(x%i==0){
while(x%i==0)
x/=i,v[i].push_back(oo);
}
if(x==1) break;
}
if(x>1) v[x].push_back(oo);
}
inline bool judge(int l,int r,int x){
for(int i=2;i*i<=x;++i){
if(x%i==0){
int num=0;
while(x%i==0) num++,x/=i;
if(query(l,r,i)<num) return false;
if(x==1) break;
}
}
if(x>1) if(query(l,r,x)<1) return false;
return true;
}
int main(){
if(fopen("tree.in","r")){
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
}
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) scanf("%d",a+i);
for(int i=1;i<=n;++i) divide(a[i],i);
for(int i=1;i<=m;++i){
int l=0,r=0,d=0;
scanf("%d%d%d",&l,&r,&d);
if(judge(l,r,d)) puts("Yes");
else puts("No");
}
return 0;
}
/*
5 4
6 4 7 2 5
1 2 24
1 3 18
2 5 17
3 5 35
*/