1线性筛
1 var 2 n,i,j,m,tot:longint; 3 ans:int64; 4 p:array[0..10000005]of longint; 5 f:array[0..10000005]of boolean; 6 begin 7 readln(n); 8 f[1]:=true;f[0]:=true; 9 for i:=2 to n do 10 begin 11 if not f[i] then begin inc(tot); p[tot]:=i;end; 12 j:=1; 13 while (p[j]*i<=n)and(j<=tot) do 14 begin 15 f[p[j]*i]:=true; 16 if i mod p[j]=0 then break; 17 inc(j); 18 end; 19 end; 20 for i:=2 to n do 21 if not f[i] then inc(ans); 22 writeln(ans); 23 end.
2扩展欧几里得
1 function exgcd(a,b:int64; var x,y:int64):int64; 2 var t:int64; 3 begin 4 if b=0 then 5 begin 6 x:=1;y:=0; 7 exit(a); 8 end; 9 r:=exgcd(b,a mod b,x,y); 10 t:=x; 11 x:=y; 12 y:=t-a div b*y; 13 exit(r); 14 end;
3后序遍历
1 var 2 s1,s2:string; 3 procedure dfs(a,b:string); 4 var k:longint; 5 begin 6 if length(a)=1 then begin write(a);exit;end; 7 k:=pos(a[1],b); 8 if k>1 then dfs(copy(a,2,k-1),copy(b,1,k-1)); 9 if k<length(b) then dfs(copy(a,k+1,length(a)-k),copy(b,k+1,length(b)-k)); 10 write(b[k]); 11 end; 12 begin 13 readln(s1); 14 readln(s2); 15 dfs(s1,s2); 16 end.
4求n的欧拉函数
1 var 2 a,b,c,i,j,k,n,m:longint; 3 t:int64; 4 begin 5 readln(n); 6 a:=n; 7 t:=n; 8 for i:=2 to trunc(sqrt(n))do 9 if a mod i=0 then 10 begin 11 begin 12 while a mod i =0 do a:=a div i; 13 t:=t*(i-1)div i; 14 end; 15 if i>a then break; 16 end; 17 if a<>1 then t:=t*(a-1)div a; 18 if t=n then t:=t-1; 19 writeln(t); 20 end.
5 归并排序与逆序对
1 var 2 ans,n:int64; 3 i,j:longint; 4 a,t:array[0..1000005]of longint; 5 procedure merge(l,p,r:longint); 6 var i,j,k:longint; 7 begin 8 i:=l;j:=p+1;k:=l; 9 while(i<=p)and(j<=r) do 10 begin 11 if a[i]<=a[j] then 12 begin 13 t[k]:=a[i]; inc(i); 14 end 15 else 16 begin 17 t[k]:=a[j]; inc(j); 18 ans:=ans+p-i+1;//求逆序对 19 end; 20 inc(k); 21 22 end; 23 24 while i<=p do 25 begin 26 t[k]:=a[i]; 27 inc(i);inc(k); 28 end; 29 while j<=r do 30 begin 31 t[k]:=a[j]; inc(k); 32 inc(j); 33 end; 34 for i:=l to r do a[i]:=t[i]; 35 end; 36 procedure mergesort(l,r:longint); 37 var mid:int64; 38 begin 39 if l<r then 40 begin 41 mid:=(l+r)div 2; 42 mergesort(l,mid); 43 mergesort(mid+1,r); 44 merge(l,mid,r); 45 46 end; 47 end; 48 begin 49 readln(n); 50 for i:=1 to n do readln(a[i]); 51 mergesort(1,n); 52 writeln(ans); 53 end.
6 快速幂
1 var 2 ans,y,a,b,c,k:int64; 3 4 begin 5 readln(a,b,c); 6 ans:=1;y:=a; k:=b; 7 while b<>0 do 8 begin 9 if (b and 1)=1 then ans:=ans*y mod c; 10 y:=y*y mod c; 11 b:=b shr 1; 12 end; 13 writeln(a,'^',k,' ','mod',' ',c,'=',ans); 14 end.
7 矩阵快速幂
1 var 2 n,tot,b:int64; 3 i,j,k,l:longint; 4 a,ans,x:array[0..105,0..105]of int64; 5 c:array[0..10000]of int64; 6 procedure cheng; 7 var i,j,k:longint; 8 begin 9 x:=ans; 10 fillchar(ans,sizeof(ans),0); 11 for i:=1 to n do 12 for j:=1 to n do 13 for k:=1 to n do 14 begin 15 ans[i,j]:=(a[i,k]*x[k,j]+ans[i,j]) mod 1000000007; // 16 end; 17 end; 18 begin 19 readln(n,b); 20 for i:=1 to n do 21 begin 22 for j:=1 to n do 23 read(a[i,j]); 24 readln; 25 end; 26 for i:=1 to n do ans[i,i]:=1; 27 while b>0 do 28 begin 29 inc(tot); 30 c[tot]:=b mod 2; 31 b:=b shr 1; 32 end; 33 for l:=1 to tot do 34 begin 35 if c[l]=1 then cheng; 36 x:=a; 37 fillchar(a,sizeof(a),0); 38 for i:=1 to n do 39 for j:=1 to n do 40 for k:=1 to n do 41 a[i,j]:=(a[i,j]+x[i,k]*x[k,j]) mod 1000000007; 42 end; 43 for i:=1 to n do 44 begin 45 for j:=1 to n do write(ans[i,j],' '); 46 writeln; 47 end; 48 end.
8 tarjan缩点
1 var 2 j,ans,e,n,top,m,tot:longint; 3 i:longint; 4 instack,bj:array[0..200005]of boolean; 5 dis,q,rd,c,stack,dfn,low,a,next,head,belong,b_1,b_2,b:array[0..100005]of longint; 6 7 procedure add(x,y:longint); 8 begin 9 inc(e);a[e]:=y;next[e]:=head[x];head[x]:=e; 10 inc(rd[y]); 11 end; 12 procedure tarjan(u:longint); 13 var i,j,v:longint; 14 begin 15 inc(tot); 16 dfn[u]:=tot; low[u]:=tot; 17 instack[u]:=true;// enter stack. mark true 18 inc(top); 19 stack[top]:=u; 20 i:=head[u]; 21 while i>0 do 22 begin 23 v:=a[i]; 24 if dfn[v]=0 then 25 begin 26 tarjan(v); 27 if low[v]<low[u] then low[u]:=low[v]; // watch out 28 end 29 else 30 if (instack[v])and(low[v]<low[u])then low[u]:=low[v]; 31 i:=next[i]; 32 end; 33 if dfn[u]=low[u] then 34 begin 35 inc(ans); 36 while stack[top+1]<>u do //important last time you error here 37 begin 38 instack[stack[top]]:=false; 39 belong[stack[top]]:=ans; 40 inc(b[ans],c[stack[top]]); 41 dec(top); 42 end; 43 end; 44 end; 45 procedure spfa(s:longint); 46 var 47 h,t,i,v,u:longint; 48 begin 49 fillchar(bj,sizeof(bj),0); 50 h:=0;t:=1; 51 // for i:=1 to n do dis[i]:=100000000; last time you error here ,too 52 q[1]:=s; 53 dis[s]:=b[s]; 54 while h<t do 55 begin 56 inc(h); 57 u:=q[h]; 58 // bj[u]:=false; 59 i:=head[u]; 60 while i>0 do 61 begin 62 v:=a[i]; 63 if (dis[v]<dis[u]+b[v]) then 64 begin 65 dis[v]:=dis[u]+b[v]; 66 if not bj[v] then 67 begin 68 bj[v]:=true; 69 inc(t); 70 q[t]:=v; 71 end; 72 end; 73 i:=next[i]; //下一条边 74 bj[v]:=false;// 松弛y节点 75 end; 76 end; 77 for i:=1 to ans do if tot<dis[i] then tot:=dis[i]; 78 end; 79 begin 80 readln(n,m); 81 for i:=1 to n do 82 read(c[i]); 83 for i:=1 to m do 84 begin 85 readln(b_1[i],b_2[i]); 86 add(b_1[i],b_2[i]); 87 end; 88 for i:=1 to n do 89 if dfn[i]=0 then tarjan(i); 90 //for i:=1 to n do writeln(belong[i]); 91 fillchar(next,sizeof(next),0); 92 fillchar(head,sizeof(head),0); 93 fillchar(a,sizeof(a),0); 94 fillchar(rd,sizeof(rd),0); 95 e:=0; 96 tot:=0; 97 for i:=1 to m do 98 if belong[b_1[i]]<>belong[b_2[i]] then add(belong[b_1[i]],belong[b_2[i]]); 99 for i:=1 to ans do 100 begin 101 if rd[i]=0 then spfa(i); 102 end; 103 writeln(tot); 104 end.
9 乘法逆元
1 var 2 s1,c,m,x,y:int64; 3 i:longint ; 4 procedure exgcd(a,b:int64;var x,y:int64); 5 var r,t:int64; 6 begin 7 if b=0 then 8 begin 9 x:=1;y:=0; exit; 10 end; 11 exgcd(b,a mod b,x,y); 12 t:=x; 13 x:=y; 14 y:=t-a div b*y; 15 16 end; 17 {procedure cal(d,m:int64); 18 var x,y:int64; 19 begin 20 exgcd(d,m,x,y); 21 writeln(((x mod m+m)mod m)); 22 end; } 23 begin 24 readln(s1,c); 25 for i:=1 to s1 do 26 begin 27 exgcd(i,c,x,y); 28 writeln((x mod c+c)mod c); 29 end; 30 end.
10 treap
1 var 2 tr:array[0..500005]of record 3 l,r,v,w,num,s:longint; 4 end; 5 i,n,tot,ans,m,a,root:longint; 6 procedure update(x:longint); 7 begin 8 tr[x].s:=tr[x].num+tr[tr[x].l].s+tr[tr[x].r].s; 9 end; 10 procedure right(var x:longint); 11 var k:longint; 12 begin 13 k:=tr[x].l; 14 tr[x].l:=tr[k].r;tr[k].r:=x; 15 tr[k].s:=tr[x].s; 16 update(x); 17 x:=k; 18 end; 19 procedure left(var x:longint); 20 var k:longint; 21 begin 22 k:=tr[x].r; 23 tr[x].r:=tr[k].l;tr[k].l:=x; 24 tr[k].s:=tr[x].s; 25 update(x); 26 x:=k; 27 end; 28 procedure insert(var x:longint; v:longint); 29 begin 30 if x=0 then 31 begin 32 inc(tot); 33 x:=tot; 34 tr[x].s:=1; tr[x].num:=1; 35 tr[x].v:=v; tr[x].w:=random(n+5); exit; 36 end; 37 inc(tr[x].s); 38 if tr[x].v=v then inc(tr[x].num) 39 else if (v<tr[x].v) then 40 begin 41 insert(tr[x].l,v); 42 if tr[tr[x].l].w<tr[x].w then right(x); 43 end 44 else 45 begin 46 insert(tr[x].r,v); 47 if tr[tr[x].r].w<tr[x].w then left(x); 48 end; 49 end; 50 procedure erase(var x:longint; v:longint); 51 begin 52 if x=0 then exit; 53 if v=tr[x].v then 54 begin 55 if tr[x].num>1 then 56 begin dec(tr[x].num); dec(tr[x].s); exit; end; 57 if tr[x].l=0 then x:=tr[x].r 58 else 59 if tr[x].r=0 then x:=tr[x].l 60 else 61 begin 62 if tr[tr[x].l].w<tr[tr[x].r].w then 63 begin right(x);erase(x,v); end 64 else 65 begin left(x);erase(x,v); end; 66 end; 67 end 68 else 69 // begin 70 if v<tr[x].v then begin dec(tr[x].s);erase(tr[x].l,v); end 71 else 72 begin dec(tr[x].s); erase(tr[x].r,v);end; 73 // end; 74 end; 75 function rank(x,v:longint):longint; 76 begin 77 if x=0 then exit(0); 78 if tr[x].v=v then exit(tr[tr[x].l].s+1) 79 else 80 if(v<tr[x].v) then exit(rank(tr[x].l,v)) 81 else 82 exit(tr[tr[x].l].s+tr[x].num+rank(tr[x].r,v)); 83 end; 84 85 function num(x,v:longint):longint; 86 begin 87 if x=0 then exit(x); 88 if v<=tr[tr[x].l].s then exit(num(tr[x].l,v)) 89 else 90 if v>tr[x].num+tr[tr[x].l].s then 91 exit(num(tr[x].r,v-tr[tr[x].l].s-tr[x].num)) 92 else 93 exit(tr[x].v); 94 end; 95 procedure pre(x,v:longint); 96 begin 97 if x=0 then exit; 98 if v>tr[x].v then 99 begin 100 ans:=x; pre(tr[x].r,v); 101 end 102 else pre(tr[x].l,v); 103 end; 104 procedure sub(x,v:longint); 105 begin 106 if x=0 then exit; 107 if v<tr[x].v then 108 begin 109 ans:=x; sub(tr[x].l,v); 110 end 111 else sub(tr[x].r,v); 112 end; 113 begin 114 //assign(input,'jj.in');reset(input); 115 //assign(output,'jj.out');rewrite(output); 116 randomize; 117 readln(n); 118 for i:=1 to n do 119 begin 120 readln(a,m); 121 if a=1 then insert(root,m); 122 if a=2 then erase(root,m); 123 if a=3 then writeln(rank(root,m)); 124 if a=4 then writeln(num(root,m)); 125 if a=5 then begin pre(root,m);writeln(tr[ans].v); end; 126 if a=6 then begin sub(root,m);writeln(tr[ans].v); end; 127 end; 128 close(input); 129 close(output); 130 131 end.
11 树状数组
1 var 2 x,y,ch,i,j,n,m:longint; 3 a,c:array[0..500005]of longint; 4 function lowbit(x:longint):longint; 5 begin 6 exit(x and -x); 7 end; 8 procedure build(x,w:longint); 9 begin 10 while x<=n do 11 begin 12 c[x]:=c[x]+w; 13 x:=x+lowbit(x); 14 end; 15 end; 16 procedure add(x,w:longint); 17 begin 18 while x<=n do 19 begin 20 c[x]:=c[x]+w; 21 x:=lowbit(x)+x; 22 end; 23 end; 24 function sum(x:longint):int64; 25 var ans:int64; 26 begin 27 ans:=0; 28 while x>0 do 29 begin 30 ans:=ans+c[x]; 31 x:=x-lowbit(x); 32 end; 33 exit(ans); 34 end; 35 function slove(x,y:longint):int64; 36 var t1,t2:int64; 37 begin 38 t1:=sum(x-1); 39 t2:=sum(y); 40 exit(t2-t1); 41 end; 42 begin 43 readln(n,m); 44 for i:=1 to n do 45 begin 46 read(a[i]); 47 build(i,a[i]); 48 end; 49 for i:=1 to m do 50 begin 51 readln(ch,x,y); 52 if ch=1 then add(x,y); 53 if ch=2 then writeln(slove(x,y)); 54 end; 55 end.
12差分树状数组
1 var 2 x,y,ch,i,j,n,m,last,k:longint; 3 a,c:array[0..500005]of longint; 4 function lowbit(x:longint):longint; 5 begin 6 exit(x and -x); 7 end; 8 procedure build(x,w:longint); 9 begin 10 while x<=n do 11 begin 12 c[x]:=c[x]+w; 13 x:=x+lowbit(x); 14 end; 15 end; 16 procedure add(x,w:longint); 17 begin 18 while x<=n do 19 begin 20 c[x]:=c[x]+w; 21 x:=lowbit(x)+x; 22 end; 23 end; 24 procedure detele(x,w:int64); 25 begin 26 while x<=n do 27 begin 28 c[x]:=c[x]-w; 29 x:=x+lowbit(x); 30 end; 31 end; 32 function sum(x:longint):int64; 33 var ans:int64; 34 begin 35 ans:=0; 36 while x>0 do 37 begin 38 ans:=ans+c[x]; 39 x:=x-lowbit(x); 40 end; 41 exit(ans); 42 end; 43 begin 44 readln(n,m); 45 for i:=1 to n do 46 begin 47 read(a[i]); 48 build(i,a[i]-last); 49 last:=a[i]; 50 end; 51 for i:=1 to m do 52 begin 53 read(ch); 54 if ch=1 then 55 begin 56 read(x,y,k); 57 add(x,k); 58 detele(y+1,k); 59 end; 60 if ch=2 then begin read(x);writeln(sum(x));end; 61 readln; 62 end; 63 end.
13 LCA
1 var 2 dep,q,fa,a,next,head:array[0..1500005]of longint; 3 f:array[0..1000005,0..20]of longint; 4 bj:array[0..1500005]of boolean; 5 e,n,m,i,k,x,y,root,j,xx,yy:longint; 6 procedure add(x,y:longint); 7 begin 8 inc(e); a[e]:=y;next[e]:=head[x];head[x]:=e; 9 end; 10 procedure dfs(x:longint); 11 var i,v:longint; 12 begin 13 i:=head[x]; 14 while i>0 do 15 begin 16 v:=a[i]; 17 if dep[v]=0 then 18 begin 19 dep[v]:=dep[x]+1; 20 f[v,0]:=x; 21 dfs(v); 22 end; 23 i:=next[i]; 24 end; 25 end; 26 procedure swap(var xx,yy:longint); 27 var t:longint; 28 begin 29 t:=xx;xx:=yy;yy:=t; 30 end; 31 function lca(x,y:longint):longint; 32 var i:longint; 33 begin 34 if dep[x]<dep[y] then swap(x,y); 35 36 for i:=20 downto 0 do 37 begin 38 if dep[f[x,i]]>=dep[y] then x:=f[x,i]; 39 end; 40 // if y=5 then writeln(dep[x],' ',dep[y]); 41 if x=y then exit(x); 42 for i:=20 downto 0 do 43 if f[x,i]<>f[y,i] then 44 begin 45 x:=f[x,i];y:=f[y,i]; 46 end; 47 exit(f[x,0]); 48 end; 49 begin 50 readln(n,m,root); 51 for i:=1 to n-1 do 52 begin 53 readln(x,y); 54 add(x,y); 55 add(y,x); 56 end; 57 f[root,0]:=root;dep[root]:=1;// wrong here 58 dfs(root); 59 60 for i:=1 to 20 do 61 for j:=1 to n do 62 begin 63 f[j,i]:=f[f[j,i-1],i-1]; 64 end; 65 for i:=1 to m do 66 begin 67 readln(x,y); 68 writeln(LCA(x,y)); 69 end; 70 end.