【题解】4.13模拟赛,线段树(bzoj 口算训练)

\(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
*/

转载于:https://www.cnblogs.com/JCNL666/p/10701880.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值