基础代码汇总整理 for NOIP 修订版Word下载.docx
《基础代码汇总整理 for NOIP 修订版Word下载.docx》由会员分享,可在线阅读,更多相关《基础代码汇总整理 for NOIP 修订版Word下载.docx(33页珍藏版)》请在冰豆网上搜索。
K进制转换十进制
functionktodec(st:
k:
longint;
012456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
vari,j,ans:
ans:
=0;
j:
=1;
fori:
=length(st)downto1do
inc(ans,j*(pos(st[i],alph)-1));
=j*k;
exit(ans);
欧几里得算法
functiongcd(a,b:
ifb=0thenexit(a)
elseexit(gcd(b,amodb));
求最小公倍数
functionlcm(a,b:
exit(adivgcd(a,b)*b);
判断质数
functionjudgeprime(x:
longint);
vari:
ifx=1thenexit(false);
=2totrunc(sqrt(x))do
ifxmodi=0then
exit(false);
exit(true);
生成质数表
proceduremakeprime;
vari,j:
fillchar(f,sizeof(f),0);
f[1]:
=true;
=2tondo
if(notf[i])and(i<
10000)then
=i*i;
whilej<
=ndo
begin
f[j]:
inc(j,i);
end;
快速幂
functioncalc(x:
qword):
qword;
ifx=0thenexit
(1);
ifx=1thenexit
(2);
ifodd(x)thenexit(2*sqr(calc((x-1)div2))modp)
elseexit(sqr(calc(xdiv2))modp);
exit(calc(xdiv2)*calc(x-xdiv2)modp);
简单高精度运算系列
procedurechange(st:
varx:
arrayoflongint);
x[0]:
whilelength(st)>
4do
inc(x[0]);
val(copy(st,length(st)-3,4),x[x[0]]);
delete(st,length(st)-3,4);
val(st,x[x[0]]);
functioncompare(a,b:
arrayoflongint):
boolean;
ifa[0]>
b[0]thenexit(true);
ifa[0]<
b[0]thenexit(false);
=a[0]downto1do
ifa[i]>
b[i]thenexit(true)
elseifa[i]<
b[i]thenexit(false);
procedurehighplus(a,b:
arrayoflongint;
varc:
fillchar(c,sizeof(c),0);
b[0]thenc[0]:
=a[0]
elsec[0]:
=b[0];
=1toc[0]do
inc(c[i],a[i]+b[i]);
ifc[i]>
=10000then
dec(c[i],10000);
inc(c[i+1]);
whilec[c[0]+1]>
0doinc(c[0]);
procedurehighminus(a,b:
c[0]:
=a[0];
inc(c[i],a[i]-b[i]);
ifc[i]<
0then
inc(c[i],10000);
dec(c[i+1]);
while(c[0]<
1)and(c[c[0]]=0)dodec(c[0]);
procedurehighmulti(a,b:
=a[0]+b[0]-1;
=1toa[0]do
forj:
=1tob[0]do
inc(c[i+j-1],a[i]*b[j]);
inc(c[i+1],c[i]div10000);
c[i]:
=c[i]mod10000;
procedurehighout(x:
write(x[x[0]]);
=x[0]-1downto1do
ifx[i]<
1000thenwrite(0);
100thenwrite(0);
10thenwrite(0);
write(x[i]);
writeln;
表达式求值
constnum='
0123456789'
sym='
+-*/()@'
com:
array[1..7,1..7]oflongint=((1,1,-1,-1,-1,1,1),
(1,1,-1,-1,-1,1,1),
(1,1,1,1,-1,1,1),
(-1,-1,-1,-1,-1,0,2),
(1,1,1,1,2,1,1),
(-1,-1,-1,-1,-1,2,0));
functioncalc(suf:
string):
double;
varstack:
array[1..100]ofdouble;
i,top:
ch:
char;
i:
=suf[1];
top:
whilech<
@'
do
casechof
'
+'
:
=stack[top-1]+stack[top];
dec(top,2);
-'
=stack[top-1]-stack[top];
*'
=stack[top-1]*stack[top];
/'
=stack[top-1]/stack[top];
0'
..'
9'
=x*10+pos(ch,num)-1;
inc(i);
=suf[i];
end
inc(top);
stack[top]:
=x;
exit(stack[top]);
procedureturn(varmid,suf:
string);
array[1..100]oflongint;
i,top,w:
mid:
=mid+'
@'
suf:
stack[1]:
=7;
=mid[1];
ifpos(ch,num)<
whilepos(ch,num)<
0do
=suf+ch;
=mid[i];
=suf+'
ifpos(ch,sym)<
0then
w:
=stack[top];
whilecom[w,pos(ch,sym)]=1do
=suf+sym[w];
dec(top);
ifcom[w,pos(ch,sym)]=-1then
=pos(ch,sym);
else
whilew<
7do
格拉汉扫除法
functiondirection(a,b,c:
situ):
real;
exit((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.y-c.y));
functiondist(a,b:
exit(sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
procedurepolarangle(s,t:
varl,r:
key,tmp:
situ;
l:
=s;
r:
=t;
key:
=p[random(t-s+1)+s];
whilel<
=rdo
while(direction(p[l],key,p[1])>
0)or
((direction(p[l],key,p[1])=0)and(dist(p[l],p[1])<
dist(key,p[1])))doinc(l);
while(direction(p[r],key,p[1])<
((direction(p[r],key,p[1])=0)and(dist(p[r],p[1])>
dist(key,p[1])))dodec(r);
ifl<
=rthen
tmp:
=p[l];
p[l]:
=p[r];
p[r]:
=tmp;
inc(l);
dec(r);
ifs<
rthenqsort(s,r);
tthenqsort(l,t);
proceduregetvex;
if(p[i].y<
p[1].y)or((p[i].y=p[1].y)and(p[i].x<
p[1].x))then
=p[1];
p[1]:
=p[i];
p[i]:
proceduregraham;
getvex;
randomize;
polarangle(2,n);
stack[0]:
=2;
stack[2]:
=3tondo
while(stack[0]>
1)and(direction(p[i],p[stack[stack[0]]],p[stack[stack[0]-1]])>
=0)do
dec(stack[0]);
inc(stack[0]);
stack[stack[0]]:
=i;
=dist(p[stack[stack[0]]],p[stack[1]]);
=1tostack[0]-1doans:
=ans+dist(p[stack[i]],p[stack[i+1]]);
判断线段相交
functionsegment(a,b,c:
if(min(a.x,b.x)<
=c.x)and(max(a.x,b.x)>
=c.x)and
(min(a.y,b.y)<
=c.y)and(max(a.y,b.y)>
=c.y)then
functionintersect(a,b,c,d:
varda,db,dc,dd:
da:
=direction(c,d,a);
db:
=direction(c,d,b);
=direction(a,b,c);
dd:
=direction(a,b,d);
if(da*db<
-(1e-16))and(dc*dd<
-(1e-16))thenexit(true);
if(abs(da)<
1e-16)andsegment(c,d,a)thenexit(true);
if(abs(db)<
1e-16)andsegment(c,d,b)thenexit(true);
if(abs(dc)<
1e-16)andsegment(a,b,c)thenexit(true);
if(abs(dd)<
1e-16)andsegment(a,b,d)thenexit(true);
弗洛伊德算法
procedurefloyd;
vari,j,k:
=1tondo
ifg[i,j]<
0thendist[i,j]:
=g[i,j]
elsedist[i,j]:
=maxlongint;
fork:
if(dist[i,k]<
maxlongint)and(dist[k,j]<
maxlongint)and
(dist[i,k]+dist[k,j]<
dist[i,j])then
dist[i,j]:
=dist[i,k]+dist[k,j];
SPFA算法
procedurespfa(s:
varvis:
array[1..100]ofboolean;
que:
array[0..99]oflongint;
i,u,open,clo:
fillchar(vis,sizeof(vis),0);
=1tondodist[i]:
open:
clo:
dist[s]:
vis[s]:
que[1]:
whileopen<
clodo
=(open+1)modn;
u:
=que[open];
vis[u]:
=false;
if(g[u,i]<
0)and(dist[u]+g[u,i]<
dist[i])then
ifnotvis[i]then
begin
clo:
=(clo+1)modn;
que[clo]:
=u;
vis[clo]:
end;
dist[i]:
=dist[u]+g[u,i];
克鲁斯卡尔算法
procedurekruskal;
varfather:
i,get:
functionfind(i:
iffather[i]=ithenexit(i)
elsefather[i]:
=find(father[i]);
exit(father[i]);
procedureunion(i,j:
varu,v:
v:
=find(i);
=find(j);
father[v]:
qsort(1,e);
get:
=1tondofather[i]:
=1toedo
iffind(edge[i].u)<
find(edge[i].v)then
union(edge[i].u,edge[i].v);
inc(ans,edge[i].data);
inc(get);
ifget=n-1thenexit;
Kosaraju算法
procedurekosaraju;
order:
i,time:
procedureforthdfs(u:
ifg[u,i]and(notvis[i])then
forthdfs(i);
inc(time);
order[time]:
procedurebackdfs(u:
ifg[i,u]and(notvis[i])then
backdfs(i);
fill[u]:
=color;
time:
ifnotvis[i]then
color:
=timedownto1do
ifnotvis[order[i]]then
inc(color);
backdfs(order[i]);
最短增广路算法
proceduresap(s,t:
vardist,dsum,nowvex,pre,data:
array[0..100]oflongint;
i,j,delta,mintmp,minvex:
flag:
fillchar(dist,sizeof(dist),0);
=1tondonowvex[i]:
dsum[0]:
=n;
delta:
whiledist[s]<
ndo
data[i]:
=delta;
=nowvex[i]tondo
if(c[i,j]>
0)and(dist[j]+1=dist[i])then
flag:
nowvex[i]:
=j;
pre[j]:
ifdelta>
c[i,j]thendelta:
=c[i,j];
i:
ifi=tthen
inc(maxflow,de