noip基本算法网络版.docx

上传人:b****2 文档编号:20141663 上传时间:2023-04-25 格式:DOCX 页数:24 大小:17.96KB
下载 相关 举报
noip基本算法网络版.docx_第1页
第1页 / 共24页
noip基本算法网络版.docx_第2页
第2页 / 共24页
noip基本算法网络版.docx_第3页
第3页 / 共24页
noip基本算法网络版.docx_第4页
第4页 / 共24页
noip基本算法网络版.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

noip基本算法网络版.docx

《noip基本算法网络版.docx》由会员分享,可在线阅读,更多相关《noip基本算法网络版.docx(24页珍藏版)》请在冰豆网上搜索。

noip基本算法网络版.docx

noip基本算法网络版

生成序列

01序列

Procedurepoducer1(n:

integer);

VarI,j:

integer;

B:

array[0..100]ofinteger;

Begin

B[0]:

=0;

Whileb[0]=0do

Begin

I:

=n;

Whileb[i]=1dodec(i);

B[i]:

=1;

Forj:

=I+1tondo

B[j]:

=0;

End;

End;

全排列

Procedureproducer2(n:

integer);

Varx,I,j,p,min:

integer;

B:

array[0..100]ofinteger;

Begin

Fori:

=1tondo

B[i]:

=I;

B[0]:

=0;

Whileb[0]=0do

Begin

I:

=n;

Whileb[i]

Ifi=0thenbreak;

Min:

=n+1;p:

=0;

Forj:

=itondo

If(min>b[j])and(b[j]>b[i-1])then

Begin

P:

=j;min:

=b[j];

End;

X:

=b[p];b[p]:

=b[i-1];b[i-1]:

=x;

Forj:

=Ito(n+i)div2do

Begin

X:

=b[j];b[j]:

=b[n+i-j];b[n+i-j]:

=x;

End;

End;

End;

组合

Procedureproducer3(n,m:

integer);

VarI,j:

integer;

B:

array[0..100]ofinteger;

Begin

Fori:

=1tomdo

B[i]:

=I;

B[0]:

=0;

Whileb[0]=0do

Begin

I:

=m;

Whileb[i]=n+i-mdodec(i);

Ifi=0thenbreak;

Inc(b[i]);

Forj:

=I+1tomdo

B[j]:

=b[j-1]+1;

End;

End;

数论

素数的判断

朴素算法

Functionisprime(n:

longint):

boolean;

Vari:

longint;

Begin

Isprime:

=true;

Fori:

=2totrunc(sqrt(n))do

Ifnmodi=0then

Begin

Isprime:

=false;

Exit;

End;

Ifi=1thenisprime:

=false;

End;

Miller_Rabbin

Functionmiller_rabbin(p:

longint):

boolean;;

VarI,x:

longint;

Begin

Miller_rabbin:

=true;

Randomize;

Fori:

=1to30do

Begin

X:

=trunc(random*100+1);

Ifmodular(x,p-1,p)<>1then//**a^(p-1)modp

Begin

Miller_Rabbin:

=false;

Exit;

End;

End;

Ifp=1thenmiller_rabbin:

=false;

End;

分治求a^bmodc

Functionmodular(a,b,c:

longint):

longint;

VarI,j,len:

longint;

Bin:

array[1..50]ofinteger;

Begin

Ifb<=1then

Begin

Ifb=1thenmodular:

=amodc;

Ifb=0thenmodular:

=1;

Exit;

End;

Len:

=0;

J:

=b;

Whilej<>0do

Begin

Inc(i);

Bin[i]:

=jand1;

Bin[i]:

=jshr1;

End;

J:

=a;

Fori:

=len-1downto1do

Begin

J:

=jmodc*jmodc;

Ifbin[i]=1thenj:

=amodc*jmodc;

End;

Modular:

=j;

End;

欧几里德辗转相除法

Functiongcd(va,vb:

longint):

longint;

Varr:

longint;

Begin

Whilevamodvb<>0do

Begin

R:

=vamodvb;

Va:

=vb;vb:

=r;

End;

Gcd:

=vb;

End;

进制转换

Functionchange(a:

string;dx,dy:

longint):

string;

Vartemp:

string;

Len,I,j,x,t:

longint;

Begin

T:

=0;temp:

=’’;

Len:

=length(a);j:

=1;

Fori:

=lendownto1do

Begin

If(a[i]>=’A’)and(a[i]<=’F’)thenx:

=ord(a[i])-55elsex:

=ord(a[i])-48;

T:

=t+x*j;

T:

=t*dx;

End;

Whilet<>0do

Begin

X:

=tmoddy;

If(x>=10)and(x<=15)thentemp:

=temp+chr(x+55)elsetemp:

=temp+chr(x+48);

T:

=tdivdy;

End;

Iftemp=’’thenchange:

=’0’elsechange:

=temp;

End;

高精度计算

Procedureplus_part;

Begin

Ifla>lbthenlc:

=laelselc:

=lb;

Fori:

=1tolcdo

Begin

C[i]:

=a[i]+b[i];

C[i+1]:

=c[i+1]+c[i]div1000;

C[i]:

=c[i]mod1000;

End;

End;

Procedureminus_part;

Begin

Ifla>lbthenlc:

=laelselc:

=lb;

Fori:

=1tolcdo

Begin

C[i]:

=a[i]-b[i];

Ifc[i]<0then

Begin

C[i+1]:

=c[i+1]-1;

C[i]:

=c[i]+1000;

End;

End;

End;

Proceduremult_part;

Begin

Fori:

=1tolado

Forj:

=1tolbdo

Begin

C[la+lb-1]:

=c[la+lb-1]+a[i]*b[j];

C[la+lb]:

=c[la+lb]+c[la+lb-1]div1000;

C[la+lb-1]:

=c[la+lb-1]mod1000;

End;

End;

二分法BinarySearch

Procedurebinary_search;

begin

left:

=1;right:

=n+1;c:

=0;

whileleft<=rightdo

begin

mid:

=(left+right)div2;

iff[mid]=1then

begin

right:

=mid-1;

c:

=mid;

end

else

begin

left:

=mid+1;

c:

=mid+1;

end;

end;

end;

排序

直接选择排序insertionsort

Procedureinsertion_sort;

Begin

Fori:

=1tondo

Begin

J:

=1;

While(ja[j])doinc(j);

X:

=a[i];

Fork:

=jtoI-1do

A[k+1]:

=a[k];

A[j]:

=x;

End;

End;

快速排序Quicksort

Procedureqsort(be,en:

longint);

VarI,j,x,mid,temp:

longint;

Begin

I:

=be;j:

=en;mid:

=(i+j)div2;

X:

=a[mid];

Repeat

Whilex>a[i]doinc(i);

Whilex

Ifi<=jthen

Begin

Temp:

=a[i];a[i]:

=a[j];a[j]:

=temp;

Inc(i);dec(j);

End;

Untili>j;

Ifi

Ifbe

End;

Procedurequicksort

Begin

Fori:

=1tondo

Read(a[i]);

Qsort(1,n);

End;

堆排序Heapsort

Procedureheapsort;

Begin

Heapn:

=0;

Fori:

=1tondo

Begin

read(x);

Insert(x);//**Heap

End;

Fori:

=1tondo

Delete

(1);//**Heap

End;

矩阵matrix

Procedurematrix;

Begin

Fori:

=1tondo

Forj:

=1tomdo

Fork:

=1totdo

C[I,j]:

=c[I,j]+a[I,k]*b[k,j];

End;

并查集Find_UnionSet

Functionfind(x:

longint):

longint;

Varvi,vj,sum:

longint;

Temp:

array[1..10000]oflongint;

Begin

Sum:

=0;

Inc(sum);

Temp[sum]:

=x;

Vi:

=x;

Whilex<>sset[x]do

Begin

Inc(sum);

Temp[sum]:

=sset[x];

X:

=sset[x];

End;

Forvj:

=1tosumdo

Sset[temp[vj]]:

=x;

Find:

=x;

End;

Procedurefind_unionset;

Begin

Iffind(x)=find(y)thenwriteln(‘YES’)

Else

Begin

X:

=find(x);y:

=find(y);

Sset[x]:

=sset[y];

Writeln(‘NO’);

End;

End;

树结构

排序二叉树BST

Procedureinsert(x:

longint);

Varp,q,s:

point;

Begin

New(p);p:

=root;new(q);q:

=nil;

New(s);s^.data:

=x;s^.left:

=nil;s^.right:

=nil;

Flag:

=true;

Whileflagand(p<>nil)do

Begin

Q:

=p;

Ifp^.data>s^.datathenp:

=p^.left

Else

Ifp^.data

=p^.right

Else

Begin

Flag:

=false;

Exit;

End;

End;

Ifs^.data>q^.datathenq^.right:

=selseq^.left:

=s;

Dispose(p);dispose(q);

End;

Procedurebst;

Begin

New(root);

Root^.left:

=nil;root^.right:

=nil;

Read(x);

Root^.data:

=x;

Fori:

=2ton-1do

Begin

Read(x);

Insert(x);

End;

End;

二叉堆Heap

Procedureinsert(x:

longint);

VarI,j:

longint;

Begin

Inc(heapn);

Heap[heapn]:

=x;

I:

=heapn;j:

=Idiv2;

While(j>0)and(heap[i]>heap[j])do

Begin

Temp:

=heap[i];heap[i]:

=heap[j];heap[j]:

=temp;

I:

=j;j:

=jdiv2;

End;

End;

Proceduredelete(k:

longint);

VarI,j:

longint;

Begin

Heap[k]:

=heap[heapn];

Dec(heapn);

I:

=k;j:

=k*2;

If(j+1<=heapn)and(heap[j]

While(j<=heapn)and(heap[i]

Begin

Temp:

=heap[i];heap[i]:

=heap[j];heap[j]:

=temp;

I:

=j;j:

=j*2;

If(j+1<=heapn)and(heap[j]

End;

End;

Procedureheap;

Begin

Heapn:

=0;

fori:

=1tondo

begin

readln(x);

insert(x);

end;

fori:

=ndownto1do

delete(i);

end;

图论

最短路径Floyed

Procedurefloyed;

Begin

Fork:

=1tondo

Fori:

=1tondo

Forj:

=1tondo

If(i<>j)and(i<>k)and(k<>j)and(g[i,k]+g[k,j]

G[I,j]:

=g[I,k]+g[k,j];

End;

最短路径Dijkstra

Proceduredijkstra;

Begin

Fork:

=1ton-1do

Begin

I:

=0;min:

=maxlongint;

Forj:

=1tondo

If(hash[j]=0)and(dis[j]

Begin

Min:

=dis[j];

I:

=j;

End;

Ifi=0thenexit;

Hash[i]:

=1;

Forj:

=1tondo

If(hash[j]=0)and(dis[i]+g[I,j]

Dis[j]:

=dis[i]+g[I,j];

End;

End;

最短路径Bellman_Ford

Procedurebellmanford;

Begin

Repeat

Flag:

=true;

Fori:

=1tomdo

Begin

X:

=line[i].x;y:

=line[i].y;w:

=line[i].w;

Ifdis[x]+w

Begin

Dis[y]:

=dis[x]+w;

Flag:

=false;

End;

End;

Untilflag;

End;

最短路径SPFA

Procedurespfa;

Begin

Front:

=0;rear:

=1;

Q[1]:

=s;hash[s]:

=1;

Whilefront<>reardo

Begin

X:

=q[(front+1)modmaxn];

Ifstartv[x]<>0then

Fori:

=startv[x]toendv[x]do

Begin

Y:

=line[i].y;w:

=line[i].w;

Ifdis[x]>dis[y]+wthen

Begin

Dis[x]:

=dis[y]+w;

Ifhash[y]=0then

Begin

Hash[y]:

=1;

Rear:

=(rear+1)modmaxn;

Q[rear]:

=y;

End;

End;

End;

Front:

=(front+1)modmaxn;

Hash[x]:

=0;

End;

End;

MST问题Prim

Procedureprim;

Begin

fori:

=1ton-1do

begin

line[i].x:

=1;line[i].y:

=i+1;line[i].w:

=g[1,i+1];

end;

fork:

=1ton-1do

begin

min:

=maxlongint;i:

=0;

forj:

=ktondo

ifline[j].w

begin

min:

=line[j].w;i:

=j;

end;

ifi=0thenexit;

temp:

=line[i];line[i]:

=line[k];line[k]:

=temp;

x:

=line[k].y;

forj:

=k+1ton-1do

begin

y:

=line[j].y;w:

=g[x,y];

ifw

begin

line[i].w:

=w;

line[i].x:

=x;

end;

end;

end;

End;

MST问题Kruscal

Procedurekruscal;

Begin

Qsort(1,m);//**Quicksort

Sum:

=0;count:

=0;

Forj:

=1tomdo

Begin

X:

=find(line[i].x);y:

=find(line[i].y);

Ifx<>ythen

Begin

Sset[x]:

=sset[y];//**Find_UnionSet

Inc(count,line[i].w);

Inc(sum);

Ifsum=n-1thenbreak;

End;

End;

End;

Euler图

Procedurefind(x:

longint);

VarI:

longint;

Begin

Ifoutdegree[x]=0then

Begin

Inc(sum);

Cnt[sum]:

=x;

End

Else

Begin

Fori:

=1tondo

Ifg[x,i]<>0then

Begin

G[x,i]:

=0;

Dec(outdegree[x]);

Find(i);

End;

Inc(sum);

Cnt[sum]:

=x;

End;

End;

Procedureeuler;

Begin

Sum:

=0;

Fori:

=1tondo

Ifodd(outdegree[i])thenbeginfind(i);exit;end;

Find

(1);

End;

AOV网(topo_sort)

Proceduretopo_sort;

Begin

Front:

=1;rear:

=0;

fori:

=1tondo

Ifindegree[i]=0then

Begin

Inc(rear);

Q[rear]:

=I;

End;

Whilefront<=reardo

Begin

X:

=q[front];

Fori:

=1tondo

Ifg[x,i]<>0then

Begin

Dec(indegree[i]);

Ifindegree[i]=0then

Begin

Inc(rear);

Q[rear]:

=I;

End;

End’

Inc(front);

End;

End;

AOE网

ProcedureAOE;

Begin

line1:

=line;

front:

=1;rear:

=0;

fori:

=1tondo

ifindegree[i]=0then

begin

inc(rear);

q[rear]:

=i;

early[i]:

=0;

end;

whilefront<=reardo

begin

x:

=q[front];

ifstartv[x]<>0then

fori:

=startv[x]toendv[x]do

begin

y:

=line[i].y;

w:

=line[i].w;

dec(indeg[y]);

ifindeg[y]=0then

begin

inc(rear);

q[rear]:

=y;

end;

ifearly[y]

early[y]:

=early[x]+w;

end;

inc(front);

end;

line:

=line1;

fori:

=1tondo

last[i]:

=maxint;

last[q[rear]]:

=early[q[rear]];

fori:

=rear-1downto1do

begin

x:

=q[i];

ifstartv[x]<>0then

forj:

=startv[x]toendv[x]do

iflast[x]>last[line[j].y]-line[j].wthen

last[x]:

=last[line[j].y]-line[j].w;

end;

end;

网络流Ford_Fulkson

Functionfindpath:

boolean;

vari,x:

longint;

begin

can:

=0;

hash[s]:

=maxint;

q[1]:

=s;front:

=1;rear:

=1;

whilefront<=reardo

begin

x:

=q[front];

fori:

=1tondo

if(hash[i]=0)and(c[x,i]-f[x,i]>0)and(min(hash[x],c[x,i]-f[x,i])>hash[i])then

begin

in

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 日语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1