1、 If(p%2=0|p%3=0)return false; For(int i=5;i=x;i+=2) If(p%i=0)return false; Return true;4.暴力分解质因数Int record10000;Void Baoli(long p) Long x=sqrt(p)+1,i,lc=0,ok=true; If(prime(p)=1) Lc=1; recordlc=p; Return; for(i=2;(i=x)&ok;i+) While(p%i=0) lc+; Recordlc=i;P/=i;If(p=1)ok=false;Break; 5.卡特兰数long f1001=
2、0;long CountCatalan(long n) f0=f1=1; for(long i=1;=n; fi=0; for(long j=1;j=p2)return;Int x=A(p1+p2)1,i=p1,j=p2;While(ij) while(Aix)j-;If(iaj)temp=aj-1,aj-1=aj,aj=temp;3.堆排序Void MinHeapify(int p,const int HeapSize)Int Small=p;If(p*2=HeapSize&ap*2aSmall)Small=p*2;If(p*2+1=Heapsize&ap*2+1=1;i-)MinHeapi
3、fy(i,HeapSize);ExtraMin(HeapSize);Part3.图论相关1.最小生成树prim算法Const int maxint=(116)-1;Int g,dis,n,visit;int Prim() int mst=0; Int dex=1,temp=-1; For(int i=1;i+)disi=maxint; Disdex=0;=n-1; Visitdex=1; For(int j=1;j+) If(visitj=0) If(gdexj!=0&gdexjdisj)disj=gdexj; If(temp=-1|disjdistemp)temp=j; dex=temp;
4、mst+=disdex; Return mst;/O(n2)2.最小生成树prim算法利用最小堆优化且图用邻接表存储const long maxint=(130-1);struct Hnode long dis,v; Hnode() v=0; dis=maxint;struct Gnode long v,w,pos,In; struct Gnode *next; Gnode() next=NULL; v=w=In=pos=0;G2001;long n,m,vis2001=0;long x,y,z;class HeapClasspublic: Hnode A2001; long Size; vo
5、id MinHeapify(long dep) long Small=dep; struct Hnode Temp; if(2*depA2*dep.dis) Small=2*dep; if(2*dep+11) i=dep/2; if(Ai.dis=x.dis)break; Adep=Ai,GAdep.v.pos=dep; dep=i; Adep=x; GAdep.v.pos=dep;Heap;long Prim() long MST=0; Heap.A1.v=1; Heap.A1.dis=0; G1.pos=1; for(int i=2; Heap.Ai.v=i; Gi.pos=i; for(
6、i=n/2; Heap.MinHeapify(i); for(int k=1;kv.pos!=-1)&(T-w)v.pos.dis) Heap.AGT-v.pos.dis=T-w; Heap.Decrease(GT-v.pos); T=T-next; Retrun MST;/O(nlogn)3.最小生成树kruskal算法利用并查集(的路径压缩)优化int f,m,n; /m表示边数,n表示节点数Struct Edge Int x,y,w;E100000,Temp,P;Int find(int x) If(fx!=x) fx=find(fx); Return fx;/并查集的性价比多高啊。就几
7、行代码。Void Qsort(int p1,int p2) If(p1 P=Erand()%(p2-p1)+p1; Int i=p1,j=p2; While(i while(Ei.wP.w)j-; If(i0&gkjgik+gkjgij) gij=gik+gkj;5.单源最短路的dijstra算法(写出来跟prim的样子差不多)Int visit=0,dis,gVoid Dijstra() disdex=0; for(int i=1; if(visitj=0) If(disdex+gdexjgdexi+disdexdisi) disi=gdexi+disdex; If(Inqueuei=0)
8、Inqueuei=1; Queue+tail=i; while(headtail);/理想状态下是O(E);7.BFS框架int g,Q,visit;int begin=0,end=0;void BFS() Qend+=1; visit1=true; while(beginkey=key;left=New-right=New-p=NULL; struct Tnode *F=NULL,*T=Root; F=T; if(T-key)key)T=T-left; else T=T-right;p=F; if(!F)Root=New; else if(F-key)F-left=New; else F-r
9、ight=New; struct Tnode *Search(int key) struct Tnode *T=Root;key)=key)return T; struct Tnode *Min(struct Tnode *T) struct Tnode *F=NULL; return F; void Delete(int key) struct Tnode *T=Search(key); /NO SON;T-left&right) if(T-p-left=T)T-left=NULL; else T-right=NULL; delete(T); return; /Only LeftSon; i
10、f(T-left=T-right=T- T-left-p=T-p; /Only RightSon;right- /Both LeftSon and RightSon; struct Tnode *M=Min(T-right); /Find M, TM if(M-left=M)M- /Remove Edge between M and M- else M- M-M-/Copy M to T;left=T)M-left=M;right=M;p=M; void Walk(struct Tnode *T)T)return; Walk(T-left); coutkeyl=1;Root-r=n;m=(n1
11、);left=Root-right=Root- Build(Root); /从根开始,建立链接结构 /建立链接结构的过程 void Build(Tnode *T) if(T-l)=(T-r) T-sum=AT-l; return; Tnode *L=new(Tnode),*R=new(Tnode);left=L;right=R; L-p=R-p=T;l=T-l;L-r=T-m;m=(L-l+L-r)1; R-m+1;R-r;m=(R-l+R- Build(L);Build(R);sum=L-sum+R-sum; /后序遍历 void Walk(Tnode *T) if(! Walk(T- co
12、utl)r)sum)r)&l)=p)break;m) else T=T- T-sum=x; /更新该节点上面所有节点sum值; T=T-sum=(T-sum+T-sum); /查询某区间关键字之和; int Ask(Tnode *T,int l,int r)l)=l&r)=r)return T-r)return T- if(rm) return Ask(T-left,l,r); else if(l return Ask(T-right,l,r); else return Ask(T-left,l,T-m)+Ask(T-right,T-m+1,r);3.Treap = BSTree + Heap
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1