1.筛法
1 var 2 n,m,i,x,tot,j :longint; 3 prime :array[0..10000050] of boolean; 4 p :array[0..10000050] of longint; 5 begin 6 read(n,m); 7 for i:=2 to n do 8 begin 9 if not prime[i] then 10 begin 11 inc(tot); 12 p[tot]:=i; 13 end; 14 for j:=1 to tot do 15 begin 16 if (i*p[j]>n) then break; 17 prime[i*p[j]]:=true; 18 if i mod p[j]=0 then break; 19 end; 20 end; 21 //for i:=1 to tot do prime[p[i]]:=true; 22 for i:=1 to m do 23 begin 24 read(x); 25 if x=1 then writeln(‘No‘) else 26 if (prime[x]) then writeln(‘No‘) else writeln(‘Yes‘); 27 end; 28 end.//prime
1 var 2 n,i,j,tot :longint; 3 nprime :array[0..1000050] of boolean; 4 p,phi :array[0..1000050] of longint; 5 begin 6 read(n); 7 phi[1]:=1; 8 for i:=2 to n do 9 begin 10 if not nprime[i] then 11 begin 12 phi[i]:=i-1; 13 inc(tot); 14 p[tot]:=i; 15 end; 16 for j:=1 to tot do 17 begin 18 if (i*p[j]>n) then break; 19 nprime[i*p[j]]:=true; 20 if (i mod p[j]=0) then 21 begin 22 phi[i*p[j]]:=phi[i]*p[j]; 23 break; 24 end else phi[i*p[j]]:=phi[i]*(p[j]-1); 25 end; 26 end; 27 for i:=1 to n do write(phi[i],‘ ‘); 28 end.//phi
1 var 2 n,i,j,tot :longint; 3 nprime :array[0..1000050] of boolean; 4 p,miu :array[0..1000050] of longint; 5 begin 6 read(n); 7 miu[1]:=1; 8 for i:=2 to n do 9 begin 10 if not nprime[i] then 11 begin 12 miu[i]:=-1; 13 inc(tot); 14 p[tot]:=i; 15 end; 16 for j:=1 to tot do 17 begin 18 if i*p[j]>n then break; 19 nprime[i*p[j]]:=true; 20 if (i mod p[j]=0) then 21 begin 22 miu[i*p[j]]:=0; 23 break; 24 end else miu[i*p[j]]:=-miu[i]; 25 end; 26 end; 27 for i:=1 to n do write(miu[i],‘ ‘); 28 end.//miu
2.平衡树——sbt
1 var 2 N,i,opt,x,t,tot :longint; 3 left,right,size,key :Array[0..100050] of longint; 4 5 procedure lrot(var t:longint); 6 var 7 k :longint; 8 begin 9 k:=right[t]; 10 right[t]:=left[k]; 11 left[k]:=t; 12 size[k]:=size[t]; 13 size[t]:=size[left[t]]+size[right[t]]+1; 14 t:=k; 15 end; 16 17 procedure rrot(var t:longint); 18 var 19 k :longint; 20 begin 21 k:=left[t]; 22 left[t]:=right[k]; 23 right[k]:=t; 24 size[k]:=size[t]; 25 size[t]:=size[left[t]]+size[right[t]]+1; 26 t:=k; 27 end; 28 29 procedure maintain(var t:longint; flag:boolean); 30 begin 31 if not flag then 32 begin 33 if size[left[left[t]]]>size[right[t]] then rrot(t) else 34 if size[right[left[t]]]>size[right[t]] then 35 begin 36 lrot(left[t]); 37 rrot(t); 38 end else exit; 39 end else 40 begin 41 if size[right[right[t]]]>size[left[t]] then lrot(t) else 42 if size[left[right[t]]]>size[left[t]] then 43 begin 44 rrot(right[t]); 45 lrot(t); 46 end else exit; 47 end; 48 maintain(left[t],false); 49 maintain(right[t],true); 50 maintain(t,false); 51 maintain(t,true); 52 end; 53 54 procedure insert(var t:longint; v:longint); 55 begin 56 if t=0 then 57 begin 58 inc(tot); 59 t:=tot; 60 left[t]:=0; 61 right[t]:=0; 62 size[t]:=1; 63 key[t]:=v; 64 exit; 65 end; 66 inc(size[t]); 67 if v<key[t] then insert(left[t],v) else insert(right[t],v); 68 maintain(t,v>=key[t]); 69 end; 70 71 function delete(var t:longint; v:longint):longint; 72 begin 73 dec(size[t]); 74 if (v=key[t]) or ((v<key[t]) and (left[t]=0)) or ((v>key[t]) and (right[t]=0)) then 75 begin 76 delete:=key[t]; 77 if (left[t]=0) or (right[t]=0) then t:=left[t]+right[t] else key[t]:=delete(left[t],v+1); 78 end else 79 if v<key[t] then exit(delete(left[t],v)) else exit(delete(right[t],v)); 80 end; 81 82 function rank(var t:longint; v:longint):longint; 83 begin 84 if t=0 then exit(1); 85 if v<=key[t] then exit(rank(left[t],v)); 86 exit(rank(right[t],v)+size[left[t]]+1); 87 end; 88 89 function select(var t:longint; v:longint):longint; 90 begin 91 if v=size[left[t]]+1 then exit(key[t]); 92 if v<size[left[t]]+1 then exit(select(left[t],v)); 93 exit(select(right[t],v-size[left[t]]-1)); 94 end; 95 96 function pred(var t:longint; v:longint):longint; 97 begin 98 if t=0 then exit(-1); 99 if v<=key[t] then exit(pred(left[t],v)); 100 pred:=pred(right[t],v); 101 if pred=-1 then exit(key[t]); 102 end; 103 104 function succ(var t:longint; v:longint):longint; 105 begin 106 if t=0 then exit(-1); 107 if v>=key[t] then exit(succ(right[t],v)); 108 succ:=succ(left[t],v); 109 if succ=-1 then exit(key[t]); 110 end; 111 112 begin 113 read(N); 114 for i:=1 to N do 115 begin 116 read(opt,x); 117 if (opt=1) then insert(t,x) else 118 if (opt=2) then x:=delete(t,x) else 119 if (opt=3) then writeln(rank(t,x)) else 120 if (opt=4) then writeln(select(t,x)) else 121 if (opt=5) then writeln(pred(t,x)) else 122 if (opt=6) then writeln(succ(t,x)); 123 end; 124 end.
3.线段树——加法和乘法标签
1 type 2 rec =record 3 l,r :longint; 4 sum,lz,tg :int64; 5 end; 6 7 var 8 t :array[0..300050] of rec; 9 a :array[0..100050] of int64; 10 N,M,mo,i,opt,x,y :longint; 11 z :int64; 12 13 procedure build(x,l,r:longint); 14 var 15 mid :longint; 16 begin 17 t[x].l:=l; t[x].r:=r; t[x].tg:=1; 18 if (t[x].l=t[x].r) then 19 begin 20 t[x].sum:=a[l] mod mo; 21 exit; 22 end; 23 mid:=(t[x].l+t[x].r)>>1; 24 build(x*2,l,mid); 25 build(x*2+1,mid+1,r); 26 t[x].sum:=(t[x*2].sum+t[x*2+1].sum) mod mo; 27 end; 28 29 procedure update(x:longint); 30 begin 31 t[x].sum:=(t[x].sum*t[x].tg+t[x].lz*(t[x].r-t[x].l+1)) mod mo; 32 if (t[x].l=t[x].r) then 33 begin 34 t[x].tg:=1; 35 t[x].lz:=0; 36 exit; 37 end; 38 t[x*2].tg:=(t[x*2].tg*t[x].tg) mod mo; 39 t[x*2+1].tg:=(t[x*2+1].tg*t[x].tg) mod mo; 40 t[x*2].lz:=(t[x*2].lz*t[x].tg+t[x].lz) mod mo; 41 t[x*2+1].lz:=(t[x*2+1].lz*t[x].tg+t[x].lz) mod mo; 42 t[x].tg:=1; 43 t[x].lz:=0; 44 end; 45 46 procedure c_tg(x,l,r:longint; y:int64); 47 var 48 mid :longint; 49 begin 50 if (t[x].lz<>0) or (t[x].tg<>1) then update(x); 51 if (t[x].l=l) and (t[x].r=r) then 52 begin 53 t[x].tg:=(t[x].tg*y) mod mo; 54 exit; 55 end; 56 mid:=(t[x].l+t[x].r)>>1; 57 if l>mid then c_tg(x*2+1,l,r,y) else 58 if r<=mid then c_tg(x*2,l,r,y) else 59 begin 60 c_tg(x*2,l,mid,y); 61 c_tg(x*2+1,mid+1,r,y); 62 end; 63 t[x].sum:=(t[x*2].sum*t[x*2].tg+t[x*2].lz*(t[x*2].r-t[x*2].l+1)+ 64 t[x*2+1].sum*t[x*2+1].tg+t[x*2+1].lz*(t[x*2+1].r-t[x*2+1].l+1)) mod mo; 65 end; 66 67 procedure c_lz(x,l,r:longint; y:int64); 68 var 69 mid :longint; 70 begin 71 if (t[x].lz<>0) or (t[x].tg<>1) then update(x); 72 if (t[x].l=l) and (t[x].r=r) then 73 begin 74 t[x].lz:=(t[x].lz+y) mod mo; 75 exit; 76 end; 77 mid:=(t[x].l+t[x].r)>>1; 78 if l>mid then c_lz(x*2+1,l,r,y) else 79 if r<=mid then c_lz(x*2,l,r,y) else 80 begin 81 c_lz(x*2,l,mid,y); 82 c_lz(x*2+1,mid+1,r,y); 83 end; 84 t[x].sum:=(t[x*2].sum*t[x*2].tg+t[x*2].lz*(t[x*2].r-t[x*2].l+1)+ 85 t[x*2+1].sum*t[x*2+1].tg+t[x*2+1].lz*(t[x*2+1].r-t[x*2+1].l+1)) mod mo; 86 end; 87 88 function find(x,l,r:longint):int64; 89 var 90 mid :longint; 91 begin 92 if (t[x].lz<>0) or (t[x].tg<>1) then update(x); 93 if (t[x].l=l) and (t[x].r=r) then exit(t[x].sum mod mo); 94 mid:=(t[x].l+t[x].r)>>1; 95 if l>mid then exit(find(x*2+1,l,r)) else 96 if r<=mid then exit(find(x*2,l,r)) else 97 exit((find(x*2,l,mid)+find(x*2+1,mid+1,r)) mod mo); 98 end; 99 100 begin 101 read(N,M,mo); 102 for i:=1 to N do read(a[i]); 103 build(1,1,N); 104 for i:=1 to M do 105 begin 106 read(opt); 107 if opt=1 then 108 begin 109 read(x,y,z); 110 c_tg(1,x,y,z); 111 end else 112 if opt=2 then 113 begin 114 read(x,y,z); 115 c_lz(1,x,y,z); 116 end else 117 begin 118 read(x,y); 119 writeln(find(1,x,y)); 120 end; 121 end; 122 end.
时间: 2024-10-09 23:47:49