# Begtostudy(白途思)'s Professional Technology Blog

posts - 564, comments - 64, trackbacks - 0, articles - 3
首页 :: 联系 :: 订阅  :: 管理

### 公告

1. 数论算法

1
．求两数的最大公约数
function gcd(a,b:integer):integer;
begin
if b=0 then gcd:=a
else gcd:=gcd (b,a mod b);
end ;

2
．求两数的最小公倍数
function lcm(a,b:integer):integer;
begin
if a<b then swap(a,b);
lcm:=a;
while lcm mod b>0 do inc(lcm,a);
end;

3

A.

function prime (n: integer): Boolean;
var I: integer;
begin
for I:=2 to trunc(sqrt(n)) do
if n mod I=0 then begin
prime:=false; exit;
end;
prime:=true;
end;

B.

procedure getprime;
var
i,j:longint;
p:array[1..50000] of boolean;
begin
fillchar(p,sizeof(p),true);
p[1]:=false;
i:=2;
while i<50000 do begin
if p[i] then begin
j:=i*2;
while j<50000 do begin
p[j]:=false;
inc(j,i);
end;
end;
inc(i);
end;
l:=0;
for i:=1 to 50000 do
if p[i] then begin
inc(l);pr[l]:=i;
end;
end;{getprime}

function prime(x:longint):integer;
var i:integer;
begin
prime:=false;
for i:=1 to l do
if pr[i]>=x then break
else if x mod pr[i]=0 then exit;
prime:=true;
end;{prime}

1

A.Prim

procedure prim(v0:integer);
var
lowcost,closest:array[1..maxn] of integer;
i,j,k,min:integer;
begin
for i:=1 to n do begin
lowcost[i]:=cost[v0,i];
closest[i]:=v0;
end;
for i:=1 to n-1 do begin
{

min:=maxlongint;
for j:=1 to n do
if (lowcost[j]<min) and (lowcost[j]<>0) then begin
min:=lowcost[j];
k:=j;
end;
lowcost[k]:=0; {

{

{

for j:=1 to n do
if cost[k,j]<lwocost[j] then begin
lowcost[j]:=cost[k,j];
closest[j]:=k;
end;
end;
end;{prim}

B.
Kruskal

function find(v:integer):integer; {

var i:integer;
begin
i:=1;
while (i<=n) and (not v in vset[i]) do inc(i);
if i<=n then find:=i else find:=0;
end;

procedure kruskal;
var
tot,i,j:integer;
begin
for i:=1 to n do vset[i]:=[i];{

p:=n-1; q:=1; tot:=0; {p

sort;
{

while p>0 do begin
i:=find(e[q].v1);j:=find(e[q].v2);
if i<>j then begin
inc(tot,e[q].len);
vset[i]:=vset[i]+vset[j];vset[j]:=[];
dec(p);
end;
inc(q);
end;
writeln(tot);
end;

2.

A.

var
a:array[1..maxn,1..maxn] of integer;
b:array[1..maxn] of integer; {b[i]

mark:array[1..maxn] of boolean;

procedure bhf;
var
best,best_j:integer;
begin
fillchar(mark,sizeof(mark),false);
mark[1]:=true; b[1]:=0;{1

repeat
best:=0;
for i:=1 to n do
If mark[i] then {

for j:=1 to n do
if (not mark[j]) and (a[i,j]>0) then
if (best=0) or (b[i]+a[i,j]<best) then begin
best:=b[i]+a[i,j]; best_j:=j;
end;
if best>0 then begin
b[best_j]:=best
mark[best_j]:=true;
end;
until best=0;
end;{bhf}

B.Floyed

procedure floyed;
begin
for I:=1 to n do
for j:=1 to n do
if a[I,j]>0 then p[I,j]:=I else p[I,j]:=0; {p[I,j]

for k:=1 to n do {

for i:=1 to n do
for j:=1 to n do
if a[i,k]+a[j,k]<a[i,j] then begin
a[i,j]:=a[i,k]+a[k,j];
p[I,j]:=p[k,j];
end;
end;

C. Dijkstra

var
a:array[1..maxn,1..maxn] of integer;
b,pre:array[1..maxn] of integer; {pre[i]

mark:array[1..maxn] of boolean;
procedure dijkstra(v0:integer);
begin
fillchar(mark,sizeof(mark),false);
for i:=1 to n do begin
d[i]:=a[v0,i];
if d[i]<>0 then pre[i]:=v0 else pre[i]:=0;
end;
mark[v0]:=true;
repeat {

min:=maxint; u:=0; {u

for i:=1 to n do
if (not mark[i]) and (d[i]<min) then begin
u:=i; min:=d[i];
end;
if u<>0 then begin
mark[u]:=true;
for i:=1 to n do
if (not mark[i]) and (a[u,i]+d[u]<d[i]) then begin
d[i]:=a[u,i]+d[u];
pre[i]:=u;
end;
end;
until u=0;
end;

3.

Var
T:array[1..maxn,1..maxn] of boolean;
Begin
Fillchar(t,sizeof(t),false);
For k:=1 to n do
For I:=1 to n do
For j:=1 to n do T[I,j]:=t[I,j] or (t[I,k] and t[k,j]);
End;

4

A.

procedure dfs ( now,color: integer);
begin
for i:=1 to n do
if a[now,i] and c[i]=0 then begin {

c[i]:=color;
dfs(I,color);
end;
end;

B

5

a.

b.

c.

d.

Ee[j] = El[j] ，则活动j为关键活动，由关键活动组成的路径为关键路径。

a.

b.

c.
Ee El;

6

7.

Euler

Hamilton

9
．判断图中是否有负权回路 Bellman-ford 算法

x[I],y[I],t[I]

procedure bellman-ford
begin
for I:=0 to n-1 do d[I]:=+infinitive;
d[0]:=0;
for I:=1 to n-1 do
for j:=1 to m do {

if d[x[j]]+t[j]<d[y[j]] then d[y[j]]:=d[x[j]]+t[j];
for I:=1 to m do
if d[x[j]]+t[j]<d[y[j]] then return false else return true;
end;

10
n最短路径问题

*

*

*

w[i]:
i个背包的重量；
p[i]:
i个背包的价值；

1
0-1背包 每个背包只能使用一次或有限次(可转化为一次)

A.

NOIP2001

l

procedure search(k,v:integer); {

var i,j:integer;
begin
if v<best then best:=v;
if v-(s[n]-s[k-1])>=best then exit; {s[n]

if k<=n then begin
if v>w[k] then search(k+1,v-w[k]);
search(k+1,v);
end;
end;

l DP
F[I,j]

f [I, j] = f [ i-1, j-w[i] ] (w[I]<=j<=v)

For I:=1 to n do
For j:=w[I] to v do F[I,j]:=f[I-1,j-w[I]];

F[0]:=true;
For I:=1 to n do begin
F1:=f;
For j:=w[I] to v do
If f[j-w[I]] then f1[j]:=true;
F:=f1;
End;

B.

F[I,j]

F [i,j] = max { f [ i – w [ j ], j-1] + p [ j ], f[ i,j-1] }

C.

DP:
Procedure update;
var j,k:integer;
begin
c:=a;
for j:=0 to n do
if a[j]>0 then
if j+now<=n then inc(c[j+now],a[j]);
a:=c;
end;

2

A

F[I,j]

f[I,j] = f [ I-1, j – w[I]*k ] (k=1.. j div w[I])

B.

USACO 1.2 Score Inflation

*

f[i,j] = max { f [i- k*w[j], j-1] + k*p[j] } (0<=k<= i div w[j])

*

Begin
FillChar(f,SizeOf(f),0);
For i:=1 To M Do
For j:=1 To N Do
If i-problem[j].time>=0 Then
Begin
t:=problem[j].point+f[i-problem[j].time];
If t>f[i] Then f[i]:=t;
End;
Writeln(f[M]);
End.

C.

Ahoi2001 Problem2

procedure try(dep:integer);
var i,j:integer;
begin
cal; {

if now>n then exit; {

if dep=l+1 then begin {

cal;
if now=n then inc(tot);
exit;
end;
for i:=0 to n div pr[dep] do begin
xs[dep]:=i;
try(dep+1);
xs[dep]:=0;
end;
end;

procedure try(dep,rest:integer);
var i,j,x:integer;
begin
if (rest<=0) or (dep=l+1) then begin
if rest=0 then inc(tot);
exit;
end;
for i:=0 to rest div pr[dep] do
try(dep+1,rest-pr[dep]*i);
end;
{main: try(1,n); }

USACO1.2 money system
V

Procedure update;
var j,k:integer;
begin
c:=a;
for j:=0 to n do
if a[j]>0 then
for k:=1 to n div now do
if j+now*k<=n then inc(c[j+now*k],a[j]);
a:=c;
end;
{main}
begin

i:=0; {a[i]

while i<=n do begin
a[i]:=1; inc(i,now); end; {

for i:=2 to v do
begin
update; {

end;
writeln(a[n]);

A.

procedure qsort(l,r:integer);
var i,j,mid:integer;
begin
i:=l;j:=r; mid:=a[(l+r) div 2]; {

repeat
while a[i]<mid do inc(i); {

while a[j]>mid do dec(j);{

if i<=j then begin {

swap(a[i],a[j]);
inc(i);dec(j); {

end;
until i>j;
if l<j then qsort(l,j); {

if i<r then qsort(i,r);
end;{sort}

B.

procedure insert_sort;
var i,j:integer;
begin
for i:=2 to n do begin
a[0]:=a[i];
j:=i-1;
while a[0]<a[j] do begin
a[j+1]:=a[j];
j:=j-1;
end;
a[j+1]:=a[0];
end;
end;{inset_sort}

C.

procedure sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then swap(a[i],a[j]);
end;

D.

procedure bubble_sort;
var i,j,k:integer;
begin
for i:=1 to n-1 do
for j:=n downto i+1 do
if a[j]<a[j-1] then swap( a[j],a[j-1]); {

end;

E.

procedure sift(i,m:integer);{

var k:integer;
begin
a[0]:=a[i]; k:=2*i;{

while k<=m do begin
if (k<m) and (a[k]<a[k+1]) then inc(k);{

if a[0]<a[k] then begin a[i]:=a[k];i:=k;k:=2*i; end
else k:=m+1;
end;
a[i]:=a[0]; {

end;

procedure heapsort;
var
j:integer;
begin
for j:=n div 2 downto 1 do sift(j,n);
for j:=n downto 2 do begin
swap(a[1],a[j]);
sift(1,j-1);
end;
end;

F.

{a

procedure merge(var a:listtype; p,q,r:integer);
{

var I,j,t:integer;
tmp:listtype;
begin
t:=p;i:=p;j:=q+1;{t
tmp指针，I,j分别为左右子序列的指针}
while (t<=r) do begin
if (i<=q){

then begin
tmp[t]:=a[i]; inc(i);
end
else begin
tmp[t]:=a[j];inc(j);
end;
inc(t);
end;
for i:=p to r do a[i]:=tmp[i];
end;{merge}

procedure merge_sort(var a:listtype; p,r: integer); {

var q:integer;
begin
if p<>r then begin
q:=(p+r-1) div 2;
merge_sort (a,p,q);
merge_sort (a,q+1,r);
merge (a,p,q,r);
end;
end;
{main}
begin
merge_sort(a,1,n);
end.

G.

type
hp=array[1..maxlen] of integer;

1

procedure plus ( a,b:hp; var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a[0]>b[0] then len:=a[0] else len:=b[0];
for i:=1 to len do begin
inc(c[i],a[i]+b[i]);
if c[i]>10 then begin dec(c[i],10); inc(c[i+1]); end; {

end;
if c[len+1]>0 then inc(len);
c[0]:=len;
end;{plus}

2

procedure substract(a,b:hp;var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
if a[0]>b[0] then len:=a[0] else len:=b[0];
for i:=1 to len do begin
inc(c[i],a[i]-b[i]);
if c[i]<0 then begin inc(c[i],10);dec(c[i+1]); end;
while (len>1) and (c[len]=0) do dec(len);
c[0]:=len;
end;

3

procedure multiply(a:hp;b:longint;var c:hp);
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a[0];
for i:=1 to len do begin
inc(c[i],a[i]*b);
inc(c[i+1],(a[i]*b) div 10);
c[i]:=c[i] mod 10;
end;
inc(len);
while (c[len]>=10) do begin {

c[len+1]:=c[len] div 10;
c[len]:=c[len] mod 10;
inc(len);
end;
while (len>1) and (c[len]=0) do dec(len); {

c[0]:=len;
end;{multiply}

4

procedure high_multiply(a,b:hp; var c:hp}
var i,j,len:integer;
begin
fillchar(c,sizeof(c),0);
for i:=1 to a[0] do
for j:=1 to b[0] do begin
inc(c[i+j-1],a[i]*b[j]);
inc(c[i+j],c[i+j-1] div 10);
c[i+j-1]:=c[i+j-1] mod 10;
end;
len:=a[0]+b[0]+1;
while (len>1) and (c[len]=0) do dec(len);
c[0]:=len;
end;

5

procedure devide(a:hp;b:longint; var c:hp; var d:longint);
{c:=a div b; d:= a mod b}
var i,len:integer;
begin
fillchar(c,sizeof(c),0);
len:=a[0]; d:=0;
for i:=len downto 1 do begin
d:=d*10+a[i];
c[i]:=d div b;
d:=d mod b;
end;
while (len>1) and (c[len]=0) then dec(len);
c[0]:=len;
end;

6

procedure high_devide(a,b:hp; var c,d:hp);
var
i,len:integer;
begin
fillchar(c,sizeof(c),0);
fillchar(d,sizeof(d),0);
len:=a[0];d[0]:=1;
for i:=len downto 1 do begin
multiply(d,10,d);
d[1]:=a[i];
while(compare(d,b)>=0) do {
d>=b}
begin
Subtract(d,b,d);
inc(c[i]);
end;
end;
while(len>1)and(c.s[len]=0) do dec(len);
c.len:=len;
end;

1

procedure Solve(pre,mid:string);
var i:integer;
begin
if (pre='''') or (mid='''') then exit;
i:=pos(pre[1],mid);
solve(copy(pre,2,i),copy(mid,1,i-1));
solve(copy(pre,i+1,length(pre)-i),copy(mid,i+1,length(mid)-i));
post:=post+pre[1]; {

end;

2

procedure Solve(mid,post:string);
var i:integer;
begin
if (mid='''') or (post='''') then exit;
i:=pos(post[length(post)],mid);
pre:=pre+post[length(post)]; {

solve(copy(mid,1,I-1),copy(post,1,I-1));
solve(copy(mid,I+1,length(mid)-I),copy(post,I,length(post)-i));
end;

3

function ok(s1,s2:string):boolean;
var i,l:integer; p:boolean;
begin
ok:=true;
l:=length(s1);
for i:=1 to l do begin
p:=false;
for j:=1 to l do
if s1[i]=s2[j] then p:=true;
if not p then begin ok:=false;exit;end;
end;
end;

procedure solve(pre,post:string);
var i:integer;
begin
if (pre='''') or (post='''') then exit;
i:=0;
repeat
inc(i);
until ok(copy(pre,2,i),copy(post,1,i));
solve(copy(pre,2,i),copy(post,1,i));
midstr:=midstr+pre[1];
solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1));
end;

1.

n取余

2.

n取整

3.

1.

procedure solve(dep:integer);
var
i:integer;
begin
if dep=n+1 then begin writeln(s);exit; end;
for i:=1 to n do
if not used[i] then begin
s:=s+chr(i+ord(''0''));used[i]:=true;
solve(dep+1);
s:=copy(s,1,length(s)-1); used[i]:=false;
end;
end;

2.

procedure solve(dep,pre:integer);
var
i:integer;
begin
if dep=k+1 then begin writeln(s);exit; end;
for i:=1 to n do
if (not used[i]) and (i>pre) then begin
s:=s+chr(i+ord(''0''));used[i]:=true;
solve(dep+1,i);
s:=copy(s,1,length(s)-1); used[i]:=false;
end;
end;

.查找算法

1.

function binsearch(k:keytype):integer;
var low,hig,mid:integer;
begin
low:=1;hig:=n;
mid:=(low+hig) div 2;
while (a[mid].key<>k) and (low<=hig) do begin
if a[mid].key>k then hig:=mid-1
else low:=mid+1;
mid:=(low+hig) div 2;
end;
if low>hig then mid:=0;
binsearch:=mid;
end;

2.

function treesrh(k:keytype):pointer;
var q:pointer;
begin
q:=root;
while (q<>nil) and (q^.key<>k) do
if k<q^.key then q:=q^.left
else q:=q^.right;
treesrh:=q;
end;

*

1 n个活动每个活动有一个开始时间和一个结束时间，任一时刻仅一项活动进行，求满足活动数最多的情况。

2）会议室空闲时间最少。

3）每个客户有一个愿付的租金，求最大利润。

4）共R间会议室，第i个客户需使用i间会议室，费用相同，求最大利润。

1. n

procedure try(i:byte);
var j:byte;
begin
if i=n+1 then begin print;exit;end;
for j:=1 to n do
if a[i] and b[j+i] and c[j-i] then begin
x[i]:=j;
a[j]:=false; b[j+i]:=false; c[j-i]:=false;
try(i+1);
a[j]:=true; b[i+j]:=true; c[j-i]:=true;
end;
end;

2.Hanoi Tower

h(n)=2*h(n-1)+1
h(1)=1

procedure hanoi(n,a,b,c:byte); {

begin
if n=0 then exit;
hanoi(n-1,a,c,b); {

write(n,'moved from',a,'to',c);
hanoi(n-1,b,a,c);{
b上的n-1块从b柱通过a柱移到c柱上
end;

h[1..3,0..n]

Procedure move(k,goal:integer); {

Begin
If k=0 then exit;
For I:=1 to 3 do
For j:=1 to han[I,0] do
If h[I,j]=k then begin now:=I;nowp:=j; end; {

If now<>goal then begin {

Move(k-1,6-now-goal); {

Writeln(k moved from now to goal);
H[goal,h[goal,0]+1]:=h[now,nowp]; h[now,nowp]:=0;
Inc(h[goal,0]); dec(h[now,0]);
Move(k-1,goal); {

End;

NOIP2001

procedure work(dep,pre,s:longint); {

{dep

var j:longint;
begin
if dep=n then begin
if s>=pre then inc(r); exit;
end;
for j:=pre to s div 2 do work(dep+1,j,s-j);
end;

procedure try(dep:integer);
var i:integer;
begin
if dep=k then begin
if tot>=a[dep-1] then inc(sum);
exit; end;
for i:=a[dep-1] to tot div 2 do begin
a[dep]:=i; dec(tot,i);
try(dep+1);
inc(tot,i);
end;
end;{try}

IOI94

inc(tail);
for k:=1 to n do
if k

end;
end;

1

loc(I:integer):pointer; {

var p:pointer;
j:integer;
begin
if (I>=1) and (I<=L.len) then
while j<I do begin p:=p^.next; inc(j); end;
loc:=p;
end;

2

var p,q:pointer;
begin
p:=loc(L,I);
new(q);
q^.data:=x;
q^.next:=p^.next;
p^.next:=q;
inc(L.len);
end;

3

var p,q:pointer;
begin
p:=loc(L,I-1);
q:=p^.next;
p^.next:=q^.next;
dispose(q);
dec(L.len);
end;

4

p:=loc(L,I);
new(q);
q^.data:=x;
q^.pre:=p;
q^.next:=p^.next;
p^.next:=q;
q^.next^.pre:=q;

5

p:=loc(L,I); {p

p^.pre^.next:=p^.next;
p^.next^.pre:=p^.pre;
dispose(p);