问题
hdu 6287 口算训练 - https://acm.hdu.edu.cn/showproblem.php?pid=6287
分析
- 通过素因子的数量判断是否整除
- 使用主席树维护区间素因子数量
- 线性筛维护素因子
- 线性筛维护素因子的序
- 线性筛分解素因子
代码
#include<bits/stdc++.h>
using namespace std;
const int MXN = 1e5+5;
int N, M, tot = -1, ver[MXN];
int pri[MXN], vis[MXN], pos[MXN], n = 0; // 素数表、最大素因子及其序号
struct TreeNode{
int l, r; // 左右子树
int s; // 素因子出现次数
}tree[MXN<<8];
void init(){
memset(pri, 0, sizeof pri);
memset(vis, 0, sizeof vis);
for(int i = 2; i < MXN; ++i){
if(!vis[i]) pri[++n] = i, vis[i] = i, pos[i] = n;
for(int j = 1; j <= n; ++j){
if(i*pri[j] >= MXN || pri[j] > vis[i]) break;
vis[i*pri[j]] = pri[j], pos[i*pri[j]] = j;
}
}
}
int build(int l, int r){
int root = ++tot;
TreeNode &rt = tree[root];
rt.s = 0, rt.l = root, rt.r = root;
return root;
}
int update(int node, int l, int r, int p, int val){
int root = ++tot;
TreeNode &rt = tree[root];
rt = tree[node];
if(l == r){
rt.s += val;
return root;
}
int mid = (l+r)>>1;
if(p <= mid) rt.l = update(rt.l, l, mid, p, val);
else rt.r = update(rt.r, mid+1, r, p, val);
rt.s = tree[rt.l].s + tree[rt.r].s;
return root;
}
int query(int last, int now, int l, int r, int p){
TreeNode &t1 = tree[now], &t2 = tree[last];
if(l == r)
return t1.s - t2.s;
int mid = (l+r)>>1;
if(p <= mid) return query(t2.l, t1.l, l, mid, p);
else return query(t2.r, t1.r, mid+1, r, p);
}
int main(){
int t, x, y, z, p, l, r, ok;
init();
scanf("%d", &t);
while(t--){
scanf("%d%d", &N, &M);
memset(ver, 0, sizeof ver);
ver[0] = build(1, n);
for(int i = 1; i <= N; ++i){
ver[i] = ver[i-1];
scanf("%d", &x);
while(x > 1){
y = 0, z = vis[x], p = pos[x];
while(x%z == 0) ++y, x = x/z;
ver[i] = update(ver[i], 1, n, p, y);
}
}
for(int i = 1; i <= M; ++i){
scanf("%d%d%d", &l, &r, &x);
ok = 1;
while(x > 1){
y = 0, z = vis[x], p = pos[x];
while(x%z == 0) ++y, x = x/z;
if(y > query(ver[l-1], ver[r], 1, n, p)) ok = 0, x = 1;
}
printf("%s\n", ok?"Yes":"No");
}
}
return 0;
}