Basic algorithm
1. Number of algorithms
Seeking two maximum number of conventions
Function GCD (A, B: Integer): Integer;
Begin
IF b = 0 THEN GCD: = a
Else GCD: = GCD (B, A MOD B);
END;
Seeking two minimum common multiple
Function LCM (A, B: Integer): Integer;
Begin
IF a
LCM: = a;
While LCM MOD B> 0 DO INC (LCM, A);
END;
Justice
A. Decree a number in a small range is the number:
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. Judging whether the number within the longint range is the number of prime numbers (including the number of prime tables within 500,000):
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 <5000 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 5000 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}
2.
3.
4. Ask the minimum spanning tree
A.Prim algorithm:
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
{Looking for the nearest point of the spangent tree K}
MIN: = Maxlongint;
For j: = 1 to n DO
IF (Lowcost [J]
MIN: = lowcost [j];
K: = J;
END;
Lowcost [K]: = 0; {Add the vertex k to the spanning tree}
{Generated tree Add a new side K to Closest [K]}
{Corrected Lowcost and CloseSt value}
For j: = 1 to n DO
IF cost [k, j] Lowcost [J]: = COST [K, J]; Closest [J]: = K; END; END; End; {prim} B.kruskal algorithm: (greed) Remove the edges in the figure in order, if the loop is not formed, add the minimum spanning tree. Function Find (v: integer): integer; {Return to the set of vertices v} 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]; {initialization definition N set, the i-th collections contain an element I} P: = N-1; Q: = 1; Tot: = 0; {p is the number of edges that is still added, Q is the edge set pointer} Sort; {Incurrently incrementing all edges, existing in E [i], e [i] .v1 and e [i] .v2 is the serial number of the two vertices connected to the side I, E [i] .le Length of the first side} While P> 0 do begin I: = find (e [q] .v1); j: = find (e [q] .v2); IF i <> j Then Begin INC (TOT, E [q] .le); VSET [I]: = vset [i] vset [j]; vset [j]: = []; DEC (P); END; INC; END; Writeln (Tot); END; 5. Shortest path A. The label method for solving the shortest path: VAR A: array [1..maxn, 1..maxn] of integer; B: array [1..maxn] of integer; {b [i] pointing the shortest path of the top I to the source point} 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 source point} Repeat BEST: = 0; For i: = 1 to n DO IF Mark [i] Then {points for each of the shortest paths have been calculated For j: = 1 to n DO IF (Not Mark [J]) and (a [i, j]> 0) THEN