Yash loves playing with trees and gets especially excited when they have something to do with PRime numbers. On his 20th birthday he was granted with a rooted tree of n nodes to answer queries on. Hearing of prime numbers on trees, Yash gets too intoxicated with excitement and asks you to help out and answer queries on trees for him. Tree is rooted at node 1. Each node i has some value aiassociated with it. Also, integer m is given.
There are queries of two types:
for given node v and integer value x, increase all ai in the subtree of node v by value xfor given node v, find the number of prime numbers p less than m, for which there exists a node u in the subtree of v and a non-negative integer value k, such that au?=?p?+?m·k.InputThe first of the input contains two integers n and m (1?≤?n?≤?100?000,?1?≤?m?≤?1000) — the number of nodes in the tree and value mfrom the problem statement, respectively.
The second line consists of n integers ai (0?≤?ai?≤?109) — initial values of the nodes.
Then follow n?-?1 lines that describe the tree. Each of them contains two integers ui and vi (1?≤?ui,?vi?≤?n) — indices of nodes connected by the i-th edge.
Next line contains a single integer q (1?≤?q?≤?100?000) — the number of queries to proceed.
Each of the last q lines is either 1 v x or 2 v (1?≤?v?≤?n,?0?≤?x?≤?109), giving the query of the first or the second type, respectively. It's guaranteed that there will be at least one query of the second type.
OutputFor each of the queries of the second type print the number of suitable prime numbers.
Examplesinput8 203 7 9 8 4 11 7 31 21 33 44 54 64 75 842 11 1 12 52 4output311input5 108 7 5 1 01 22 31 52 431 1 01 1 22 2output2題意
給你一棵樹,n個結點,再給出一個數m(1?≤?m?≤?1000)
每個點有一個點權
有兩個操作
1 x v 使得x子樹里面的所有點的權值加v
2 x 查詢x的子樹里面所有點的權值中有多少個滿足p+k*m,其中p是小于m的素數,求出p的個數。
題解:
對于樹上對一個結點子樹的操作,可以采用dfs序。然后用線段樹存儲,對于線段樹的每個節點,維護區間出現過哪些數??梢杂胋itset實現。
更新操作,就直接讓這個bitset循環移動就好了,注意循環移動可以拆成兩個步驟,一個向右邊移動(x%m),一個向左邊移動(m-x),然后兩個并起來就好了
查詢操作,就最后得到那個區間的bitset和m以內的素數表&一下就好了。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>#include<bitset>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=100000+100;const int maxm=1000+5;typedef bitset<maxm> S;int head[maxn];int n,m;struct edge{ int from,to,next;}e[maxn*2]; //int tol=0;void add(int u,int v){ e[++tol].to=v,e[tol].next=head[u],head[u]=tol;}struct node{ int l,r; int lazy,tag; S sum;}seg[maxn*4];int c[maxn];void build(int i,int l,int r){ seg[i].l=l,seg[i].r=r,seg[i].sum.reset(),seg[i].lazy=seg[i].tag=0; if(l==r) { seg[i].sum[c[l]]=1; return; } int m=(l+r)/2; build(i*2,l,m),build(i*2+1,m+1,r); seg[i].sum=seg[i*2].sum|seg[i*2+1].sum;}void pushdown(int i){ if(seg[i].l!=seg[i].r) { int v=seg[i].tag; S t1=seg[i*2].sum,t2=seg[i*2+1].sum; seg[i*2].sum=(t1<<v)|(t1>>(m-v)),seg[i*2+1].sum=(t2<<v)|(t2>>(m-v)); seg[i*2].tag=(seg[i*2].tag+v)%m,seg[i*2+1].tag=(seg[i*2+1].tag+v)%m; seg[i*2].lazy=seg[i*2+1].lazy=1; seg[i].lazy=seg[i].tag=0; }}void update(int i,int l,int r,int v){ if(seg[i].l==l&&seg[i].r==r) { if(seg[i].lazy) seg[i].tag+=v,seg[i].tag%=m; else { seg[i].tag=v; seg[i].lazy=1; } S t=seg[i].sum; seg[i].sum=(seg[i].sum<<v)|(seg[i].sum>>(m-v)); return; } if(seg[i].lazy) pushdown(i); int m=(seg[i].l+seg[i].r)/2; if(r<=m) update(i*2,l,r,v); else if(l>m) update(i*2+1,l,r,v); else { update(i*2,l,m,v),update(i*2+1,m+1,r,v); } seg[i].sum=seg[i*2].sum|seg[i*2+1].sum;}S query(int i,int l,int r){ if(seg[i].l==l&&seg[i].r==r) { return seg[i].sum; } if(seg[i].lazy) pushdown(i); int m=(seg[i].l+seg[i].r)/2; if(r<=m) return query(i*2,l,r); else if(l>m) return query(i*2+1,l,r); else return query(i*2,l,m)|query(i*2+1,m+1,r);}int st[maxn],en[maxn];int a[maxn];int cnt=0;void dfs(int u,int f) //dfs序{ st[u]=++cnt; c[cnt]=a[u]; for(int i=head[u];i;i=e[i].next) { int v=e[i].to; if(v==f) continue; dfs(v,u); } en[u]=cnt;}int vis[maxn];S pri;void init(){ memset(vis,0,sizeof(vis)); pri.reset(); for(int i=2;i<m;i++) //預處理m以內的素數,儲存在pri中 { if(vis[i]) continue; pri[i]=1; for(int j=i*i;j<m;j+=i) vis[j]=1; }}int main(){ scanf("%d%d",&n,&m); init(); rep(i,1,n+1) scanf("%d",&a[i]),a[i]%=m; rep(i,1,n) { int u,v; scanf("%d%d",&u,&v); add(u,v),add(v,u); } dfs(1,-1); build(1,1,n); int q; scanf("%d",&q); while(q--) { int op,u,v; scanf("%d",&op); if(op==1) { scanf("%d%d",&u,&v); v%=m; update(1,st[u],en[u],v); } else { scanf("%d",&u); S res=query(1,st[u],en[u]); int ans=(res&pri).count(); // printf("%d/n",ans); } } return 0;}
新聞熱點
疑難解答