Окулов С.М._Программирование в Алгоритмах
DESCRIPTION
ОкуловTRANSCRIPT
-
519.85(023) 22.18
052
, 004.1-60.
. .
/ . . . .: . , 2002. 341 : .
ISBN 5-94774-010-9 ,
.
, , ,
, .
.
-
.
, , -
, -
.
519.85(023) 22.18
:. (095)955-03-98, e-mail: [email protected]
-: (812)247-93-01, e-mail: [email protected]
. ., 2002ISBN 5-94774-010-9 . , 2002
-
71. 9
1.1. 91.2. 21
2. 252.1. 252.2. 31
2.2.1. 312.2.2. 402.2.3. 462.2.4. 532.2.5. N
582.2.6. 612.2.7. 65
2.3. 683. 79
3.1. ( ) 793.2.
813.3. 963.4.
983.5. 1053.6. 1093.7. 113
4. 1414.1. 1414.2. 142
4.2.1. 1424.2.2. 143
4.3. 144
-
4.3.1. . 1444.3.2. 1454.3.3. . . . . 1484.3.4. . . 150
4.4. 1514.4.1. 1514.4.2. 1534.4.3. 154
4.5. 1574.5.1. 1574.5.2. 1584.5.3. 160
4.6. 1614.6.2. 1634.6.3. 1644.6.4. .
1664.7. 168
4.7.1. 1684.7.2.
1694.7.3. 1734.7.4. 1744.7.5. . . 175
4.8. 1804.8.1. 1804.8.2. . . . . 1814.8.3.
1854.9. , 186
4.9.1. 1864.9.2. . . . 1874.9.3. . . . 192
4.10. 195
4.10.1. 1954.10.2. 1974.10.3. 200
4.11. 2025. 222
5.1. 222
-
5.2. 2275.3. 2325.4. 2365.5. 2415.6. 2515.7. 259
6. 266
7. 3187.1. 3197.2. 3207.3.
( ) 328 340
-
.
-
. -
: -, .
. -
, .
, , -
- ,
, - -
. . -
. --
.
, -
. -
. , -
, -
.
? -, .
-, , -
-
. ,
,
,
ACM (As-sociation for Computing Machinery).
,
, -
. -, -
. -
- .
1993 - .
. ,
-
8
. -
. ,
,
. -
, ,
, , , . -
. . ,
, .
.
-
, --
. .
, -
-
. 1994 - .
, -
. -
-
5, -
.
-
, -
.
.
. , ,
.
. , ,
-
1.
, ,
, -
. ,
() , .
,
30!= 265252859812191058636308480000000? -
.
1.1. , -
-
, . -
.
.
:
0
9
1
0
2
8000
3
3084
4
8636
5
9105
6
8121
7
2859
8
6525
9
2
. 9 [0], ? , . - .
! . .
.
.
-
10 1.
Const MaxDig=1000;{* -.*}
Osn=10000;{*Ocoaue , - .*}
Type TLong=Array[0..MaxDig] Of Integer;{* - .*}
, .
23851674 (Osn) - 1000 ( ). - (- h) .
[0]30
11122233
[1]674
0
223
238385851516167674
[2]851
0
0002
23238385851
[3]230
0000002
23
ch-
2
3851674
1- 2- 3- 4- 5- 6- 7-
, [0] (-) . -
i - i+1, [1].
()
. -
. ,
A[i] A[i+1], . . :
For i:=A[0] DownTo 1 Do BeginA[i+1] :=A[i+l] + (LongInt (A[i] ) *10) Div Osn;A[i] : = (LongInt (A[i] ) *10) Mod Osn;
End;
-
1. 11
23851674 6 - . -
ch 7. 7 - [1]. - . -
.
i221
[1]516516160
[2]238380385
[3]022
ch7
[1] [0]. :
Procedure ReadLong(Var A:TLong);Var ch:Char;i:Integer;Begin
FillChar (A,SizeOf(A) ,0) ;RepeatRead (ch) ;Until ch In ['0'..'9'] {*
. *}While ch In ['0'..'9'] Do BeginFor i:=A[0] DownTo 1 Do Begin{*""
A[i] A[i+1]. *}
A[i+1]:=A[i+l]+(LongInt(A[i])*10) Div Osn;A[i] := (LongInt (A[i] ) *10) Mod Osn;
End;A[l] :=A[1]+Ord(ch)-Ord('0');{*
[1] . *}If [[0] +1]>0 Then Inc ([0]);{ * ,
. *}Read (ch);End;
End; .
. , .
-
,
, ,
-
12 1.
. -
. , , 128400583274. 58, 0058, - . , .
:
Procedure WriteLong(Const A:TLong) ;Var ls,s:String;
i: Integer;BeginStr(Osn Div 10,ls);Write(A[A[0]]);{* . *}For i:=A[0]-1 DownTo 1 Do BeginStr(A[i],s) ;While Length (s)
-
1. 13
i1234
A[i]945113028706
0
B[i]7461
5191213475
[1]6912691269126912
[2]1
135413541354
[3]00
78277827
[4]001
3476
=3476782713546912. , .
-
.
.
Procedure SumLongTwo(Const ,:TLong;Var C:TLong) ;Var i,k:Integer;Begin
FillChar (C,SizeOf (C) , 0) ;If A[0]>B[0] Then k:=A[0] Else k:=B[0];For i : = 1 To Do Begin [i+1]:=(C[i]+A[i]+B[i])
Div Osn;C[i]:=(C[i]+A[i]+B[i]) Mod Osn; {*
?*}End;If C[k+1]=0 Then C[0]:=k Else C[0]:=k+1;
End; .
(A=, , ==). = .
Function Eq(Const ,:TLong):Boolean;Var i:Integer;Begin
Eq:=False;If A[0]=B[0] Then Begin
i : =1 ;While (i .
Function More (,: TLong): Boolean;Var i:Integer;Begin
If A[0]
-
14 1.
Else If A[0]>B[0] Then More:=TrueElse Begin
i:=A[0];While (i>0) And (A[i]=B[i]) DoDec(i);If i=0 Then More:=False
Else If A[i]>B[i] Then More:=True ElseMore:=False;
End;End;
Eq More.Function Less(A,B:TLong):Boolean;{A
-
1. 1J5
While (i>sdvig) And (A [i] =B [i-sdvig] ) DoDec (i) ;
If i=sdvig Then Begin More:=0;{* . *}
For i:=1 To sdvig Do If A[i]>0 Then Exit;More:=2;{* , "" .*}
EndElse More:=Byte(A[i]0 Then C[0]:=A[0]+1Else [0]:=A[0];{*
.*}End;
End; -
, -
.
Procedure MulLong(Const ,: TLong; Var : TLong);{* "" "".*}Var i, j : Word;
dv: Longlnt;BeginFillChar(C
r SizeOf(C), 0) ;
For i:=1 To A[0] Do
-
16 1.
For j : = 1 B[0] Do Begindv:=LongInt (A[i])*B[j]+C[i+j-1];Inc (C[i+j], dv Div Osn);C[i+j-1]:=dv Mod Osn;
End;C[0] :=A[0]+B[0] ;While (C[0]>1) And (C[C[0]]=0) Do Dec(C[0]);
End;
. ,
. ,
,
.
.
: , ,
, . -
.
,
,
. ,
9 11 1 -, 10000 9 - .
Procedure Sub (Var A: TLong;Const : TLong; sp:Integer);
Var i , j : Integer;{ * sp, .*}
BeginFor i : = 1 To B[0] Do Begin Dec (A[i+sp], B[i]) ;
{* *} {*}j:=i; {*}While (A[j+sp]
-
1. 17
If A[i+sp]
-
18 1.
, ,
. B*10, ( ) . -, 564, 63 . , -
, .
Down - , Up , Ost -.
Down05788
Up101010109
C=B*((Down+Up) Div2)315441504567504
Ost=564COstC>OstC>OstC>OstCOstC>OstC>OstC>OstC>OstC>OstC
-
1. 19
Function FindBin (Var Ost: TLong; Const : TLong;sp: Integer) : LongInt;
Var Down, Up: Word;C: TLong;
BeginDown:=0; Up:=Osn; {* .*}While Up-1>Down Do Begin {*
.
Up>Down. - . *}
Mul (B,(Up+Down) Div 2, );Case More (Ost, C, sp) Of
0: Down:=(Down+Up) Div 2;1: Up:= (Up+Down) Div 2;2: Begin Up:=(Up+Down) Div 2; Down:=Up; End;
End;End;Mul (B, (Up+Down) Div 2, C) ;If More(Ost,C,0) =0 Then Sub (Ost, C, sp)
{* . *}Else begin Sub(,Ost,sp); Ost:=C; End;
FindBin:=(Up+Down) Div 2;{* .*}End;
sp . , , 635 15. ? 63 15 , , . -
. 4, . . 635, 35. . . . 2 5. , ( ) 42, 5. , 10, 10000? - , ,
.
Procedure MakeDel(Const ,: TLong;Var Res, Ost:TLong) ;
Var sp: Integer;Begin
Ost:=A;{* . *}sp:=A[0]-B[0];
-
20 1.
If More(,,sp)=1 Then Dec(sp);{* B*0sn>A, .*}
Res[0]:=sp+1;While sp>=0 Do Begin {*
.*}Res[sp+1]:=FindBin(Ost,B,sp);Dec(sp);End;End;
-
, () ( ). , .
F u n c t i o n Eq(Const , : TLong): B o o l e a n ;F u n c t i o n L e s s ( C o n s t A, B: TLong): B o o l e a n ;F u n c t i o n More(Const A, B: TLong): B o o l e a n ;F u n c t i o n L e s s _ E q ( C o n s t A, B: T L o n g ) : B o o l e a n ;F u n c t i o n More_Eq(Const A, B: TLong): B o o l e a n ;Function LongTurnShort (Const A: TLong; Var K:
LongInt): Boolean; {* LongInt.*}
Function LongModShort(Const A: TLong; Const K:Word): Word; {* Word.*}
Function HowDigits (Const A: TLong): Longlnt;{* .*}
Procedure Mul(Const A: TLong; : Word; Var :TLong); {* Word.*}
Procedure MulLong(Const A, B: TLong; Var C: TLong);Procedure SumLongTwo(Const A, B: TLong; Var C:
TLong);Procedure Sub(Var A: TLong; Const B: Tlong;Const
s p : I n t e g e r ) ;Procedure ShortTurnLong (K: LongInt; Var A: TLong);
{* LongInt .*}
Procedure LongDivShort(Const A: TLong; Const K:Word; Var B: TLong); {* Word.*}
-
1. 21
Procedure LongDivLong(Const , : TLong; Var ,ost: TLong);
Procedure SdvigLong(Var A: TLong; Const p: Word);{* .*}
1.2. 1.
. ( ) . -
.
. , 25974 - . .
, -
. :
Const Basis=10;{* . *}Typept=^elem;
elem=Record data:Integer; next,pred:pt; End;Var head, tail :pt;
-
.
2. 3. 4.
.
. .
[9]. 1.
() . -, , ,
. , 236488 , : 23 64 88, 15385 - 1.
-
22 1.
.
574564 : 57 45 64. , - ( ).
2. 57 - ,
2 57, -
. , 7, 7*7=4957. 7.
3. 57
2=49: 57-49=8. -
, 845. 4. .
, , (2**10+)* 845 845. 5, (14*10+5)*5=725845. , 5 y=75.
5. 3. : 845-725=120, 12064.
6. 4.(2*y*10+)*
-
1. 23
11. 2, 8, 16.
12. .
13. , () 2 (). b :
b ,
b ,
, b ,
, b ,
,
:
Function Sum (,: Integer) : Integer;Begin
If x=0 Then Sum:=yElse
If y=0 Then Sum:=xElse If (x Mod 2 =0) And (y Mod 2=0) Then
Sum:=Sum(x Div 2, Div 2)*2Else If (x Mod 2 =1) And (y Mod 2=1)
Then Sum:=Sum(x Div 2,y Div 2+1)*2Else Sum:=Sum(x Div 2, Div 2)*2+1;
End; (
) b. - 2 .
14. b , . ,
b=125, =37. :125*37250*18+125
-
24 1.
500*9+1251000*4+500+1252000*2+500+1254000*1+500+125. : 125*37=125+500+4000=4625 (
b ?). - :
,
,
:
Function RecMul (x,:Integer): Integer ;{* , Integer.*}
BeginIf =0 Then RecMul:=0Else If x Mod 2=0 Then RecMul:=RecMul (x Div
2,y)*2Else RecMul:=RecMul(x Div 2,y) *2+y;
End; ( -
) b. - 2 - .
15. - [2].
. t- numb 10*, 10*-|numb|. - [2, 23]. - i 9-i. - .
-
, -
1.1.
-
2.
, -
, , -
. -
Word, Integer, Char, Boolean . ,
Real , . , , , - -
- ( , ), -- , . -
-
.
2.1. -
-
, , -
. .
.
. -
, -
( ). - . -
.
N, . b, ( ) () N*M. .
( PN ).
N , N .
-
26 2.
1. 1,2,5, -
1, 2, 3? : 6.2. ,
, , , 1, 2, 3,4? : 24.
3. -? : 120.
4. 8 , ? -: 40320.
N , N , N! (-)=1*2*3*...*N (PN=N!).
,
1 N. - . -
N N 1 N. 1 N. , 1 N.
,
. -
F G FG, - : FG(i)=F(G(i)). , F - ,
-
F F-1, ,
FF-1=E. , -
-
1 N. -
-
-
2. 27
.
-
. , -
F : F=[1, 3], [2, 5, 4, 6]. , ifj.. (I) (-1)I. 6, - . , -
2, . ( ).
: N -?
1. 1, 2 1, 2, 5 ? : 6.
2. 1, 2 , , , ? : 12.
3. ? : 60.
4. 17 , .
? :4080.
5. 25 . - , , . -
, -
. : 303600.
() MN. - : N -?
1. 1,2,5 ? : 3.
2. - , , , ? : 6.
-
28 2.
3. - ? : 10.
4. 17 ,
. ,
-, -
.
? : 680.5. 25 . -
, -
. -
,
? : 300.6.
8 ( , ,)? : 4328284968.
7. N*M ( )? . +1 . ,
. :
-
,
1 N. -
:
( )( )
.
:
==1 -
.
( ). , N . -
-
2. 29
k. - .
, -
, .
1. .: 9*10*10*10*10*10=900000.
2. 4, 33 , ,
.
: 33*32*32*32=1081344.3. 16 (k).
, , 1 0 (N). 1 0 -? : 216=65536.
-
. -
. ,
, -
. .
. k - . N
x
, N2 , ..., Nk k-o
1. ? : 6 (, , , , ,).
2. ? : 180.
3. 2 - 3 ? : 10.
4. , 6 : 2 , 3 1 ? :60.
5. (, , ). -: bb, cabbac. - ? : 90.
-
30 2.
: N ==N1+N2+...+Nk .
. N - . k, ( - )?
. N=4. k=7. - . -
, ,
, , ,
. . (, , , ). - 7, - N-1 3, . . 10. -
3 10 , -
1. 30 . ? :
2. 3 - ? : 4 (N=2,
3. 13 52 , ? : 560 (N=4, k=13).
4. , - 2 ( , 36)? : 21 (N=6, k=2).
5. : 120 (N=4, k=7,
1101101101).. -
S, |S|=N ( ), k -,
-
2. 31
. S1 ,, ... . -
,
1. 1,2,5 - ? : 6.
2. , ,, ? : 6.
3. , ,
2? :4. 8
: , ?: 8!/(1!*3!*4!)=280.
. , -
2 6 ? :6!/(2!*2!*2!)=90.
S(N,k) - N- k . ,
. ,
,
2.2 2.2.1.
.
N. , -
:
123456
12624120720
-
32 2.
78910111213
...
30...
5040 ( Integer)40320362880362880039916800479001600 ( Longlnt)6227020800
265252859812191058636308480000000
.
Function Fac (k:LongInt): LongInt;Var r,i: LongLnt;Begin
r:=1;For i:=l To Do r:=r*i;Fa:=r;
End; N, 12.
N : ; ( 1).
.
N, -
. . ,
N. - -
.
1=1, P1[t]=
-
2. 33
()213231312321
()132312231321
(N=4). , , , -
.
123412431324134214231432
12342134132431242314
3214
213421432314234124132431
124321431423412324134213
312431423214324134123421
134231421432413234124312
412341324213423143124321
234132412431423134214321
. 11, 8, 5, 1, 7, 4, 10, 9, 6, 3, 2. ?
4 10. - , -
4, 6. 4 6 -, . . . -
-
:
-
34 2.
Procedure GetNext;Var i,j:Integer;Begin {* N, N-1,...,1
, . *}i : =N;While (i>l) And (P [i]
-
2. 35
:
i
tiyi
1110
281
351
411
570
643
7102
890
965
1035
1120
, 0=
-
36 2.
. ,
.
,
, -
.
N=4, - 0023. .
00232134
01231234
01221243
01211423
,
. y[i] - i , . ,
i ,
.
.
ConstNmax=12;Var P:Array[1. .Nmax] Of 0 . .Nmax; {*
.*}Y:Array[1..Nmax] Of 0..Nmax-1; {*
.*}D:Array[1..Nmax] Of-1..1; {*
"". *} -
Y D.Procedure First;
Var i:Integer;BeginFor i : = 1 To N Do BeginP[i]:=N-i+1; Y[i]:=0; D[i]:=1;
End;End;
-
2. 37
, .
D[i]=l Y[i]=i-1 D[i]=-l Y[i]=0 i - .
Function Ok:Integer; {* 1, , .*}
Var i:Integer;Begini:=N;While (i>1) And (((D[i]=1) And (Y[i]=i-1)) Or
((D[i]=-1) And (Y[i]=0))) DoDec(i);Ok: =i ;
End; :
BeginFirst;:=True;While pp Do BeginSolve (pp) ;If pp Then Print;End;End; .
Procedure Solve(Var q:Boolean);Var i, j, dj:Integer;Begini:=Ok; q:=(i>l); {*Ha ,
. *}If i>1 Then BeginY[i]:=Y[i]+D[i]; {* -
.*}For j:=i+1 To N Do D[j]:=-D[j]; {*
,
.*}j:=Who_i(i); {* ,
i.*}dj:=j+D[i]; {*
.*}Swap(P[j], P[dj]); {*.*}End;End;
-
38 2.
, -
, ,
i. .Function Who_i (i : Integer): Integer;Var j: Integer;Beginj:=N;While (j>0) And (P[j]i) Do Dec(j);Who_i :=j;
End; .
, , -
? -. . N 8 L, 37021. -. . -
7!, 5040 (1*******). 2 5040 (2*******). ,37021 Div 5040=7. , 8. L (37021 Mod 5040=1741) 1741. . , , .
,
4. , 1 2, : . , 4. 4, 5 7.
. N=
-
2. 39
.
Procedure GetPByNum(L:LongInt);Var Ws: Set Of Byte;
i,j,sc: Integer;Begin
Ws:=[]; {* , .*}
For i : = 1 N Do BeginSc:=L Div ResSm[N-i]; L:=L Mod ResSm[N-i]; j:=1;
While (sc0) Or (j In Ws) Do BeginIf Not (j In Ws) Then Dec(sc);Inc (j) ;
End;Ws:=Ws+[j]; {* . *}P[i]:=j;
End;End;
-
.
I
1
2
3
4
...
Sc
76543210210211021110
...
L370211741
301
61
13
...
J
12345678123123412
345
...
Ws
[]
[8]
[3,8]
[3,4,8]
[3,4,5,8]...
P********
8********
83******
834******
8345****...
-
40 2.
. , -
. .
N 8 53871462. :7!*6!*< 2- ,
3, 2>5!*< 3- ,
8, 5>4!*< 4- ,
7, 4>3!*< 5- ,
1, 0>2!*< 6- ,
4, 1>1!*< 7- ,
6, 1>,
7!*4+6!*2+5!*5+4!*4+3!*0+2!*1+1!*1=4*5040+2*720+5*120+4*24+0*6+1*2+1*1=22305.
Function GetNumByP: LongInt;Var ws:Set Of Byte;
i,j,sq:Integer; sc:LongInt;Beginws:=[]; {* .*} sc:=1;
{* .*}For i :=1 To N Do Begin
j:=1; sq:=0; {* .*}While j
-
2. 41
Function Plac(n,m: LongInt): LongInt;Var i,z: LongInt;Begin
z: =1 ;For i:=0 To m-1 Do z :=z* (n-i);Plac:=z;
End;
.
Function Plac (n,m:LongInt): LongInt;Begin
If m=0 Then Plac:=1Else Plac:=(n-m+1)*Plac (n,m-1);
End;
N=
-
42 2.
S:=S+[i]; {* .*}
A[t] :=i; {* .*}If t
-
2. 43
Procedure GetNext;Var i,j,k,q:Integer;
Free:Array[1..n] Of Boolean; {*
.*}Function FreeNext(t:Byte):Byte; {*
.*}BeginWhile (tn Then FreeNext:=0 {*
, .*}Else FreeNext:=t; {*
.*}End;BeginFor i:=1 To n Do Free [i]:=True; {*
.*}For i:=1 To m Do Free [A[i]]:=False; {*
.*}i:=m; {* .
,
.*}While (i>0) And (FreeNext(A[i])=0) Do Begin
{* , ,
.
.*}Free[A[i]]:=True;{* ,
i.*}Dec(i); {* .*}End;Free[A[i]]:=True; {*
.*}q:=FreeNext(A[i]+1); {*
, .*}Free[q]:=False; {* .*}A[i]:=q; {*3 .*}:=1; {* "" .*}For j:=i+1 To m Do Begin {*Co
.*}While (k
-
44 2.
If k>= Then k:=1 Else Inc(k);A[j]:=k; {*
.*}Free[k]:=False; {* .*}
End;End;
. N - L ( ).
5 3 , 60. - , , -
.
0.
012
345
123124125132134135
67
891011
142143145152153154
121314151617
213214215
231234235
1819202122
23
241243245251253254
242526272829
312314315321324325
3031323334
...
341
342345
351352
( ) 12.
12, . 32 1 2 3 4 5 ( - 0). 32 Div 12, 2, -, , 2- - , 3. 32 Mod 12 =8, 1 2 4 5. - 8 Div 3=2 , 4. 8 Mod 3=2, 1 2 5. - 2 Div 1 =2, 5.
Const n=5; m=3;Var A:Array[1. .] Of Integer;
L:LongInt; {* .*}Procedure GetPByNum(L:LongInt); {*B
Plac - .*}
Var i,j,q,t:LongInt;Free:Array[1..n] Of Byte; {*
.*}
-
2. 45
BeginFor i:=1 To n Do Free [i]:=i ; {*
.*}For i : = 1 To m Do Begin {*i -
.*}t:=Plac(n-i,m-i); {* ,
i.*}q:=L Div t; { *
.*}A[i]:=Free[q+1]; {*
.*}For j :=q+1 To n-i Do Free [j] :=Free [j+1];
{*, .*}
L:=L Mod t; {* ( ).*}
End;End;
. (- ).
. 345. , 3, 2(12*2=24). , 4, 2 24+3*2=30. , , 2. :12*2+3*2+1*2=32.
Function GetNumByP:LongInt; {* Plac .*}
Var L,i,j,num: LongInt;ws:Set Of Byte; {*
.*}Begin
ws: = [] ;L: =0 ;For i : = 1 To m Do Begin {*i -
.*}num:=0; {* .*}For j:=1To A[ i]-1 DoIf Not (j In ws) Then Inc (num); {*
j ws, ,
A[i].*}
-
46 2.
ws:=ws+[A[i]]; {* A[i] .*}
L:=L+num*Plac(n-i,m-i); {* ,
i.*}End;GetNumByP:=L;
End;
2.2.3. k- N- -
k - 1 N () - . , ,
[2, 6, 1, 3] - 1, 2, 3, 6. , - , k-- .
,
.
,
12341235
12361245
1246
1256
13451346
13561456
23452346
2356
2456
3456
. -
N k. . -
, -
-
, ,
, MaxInt. -
(. . 47). - . , -
-
N k, , -
.
-
CkN -. CkN .
Const MaxN=100;Var SmallSc:Array[0..MaxN,0..MaxN] Of LongInt;
{* ,
.*}Procedure FillSmallSc;Var i,j :Integer;BeginFillChar (SmallSc,SizeOf(SmallSc),0);For i:=0 To N Do SmallSc [i,0]:=1;For i:=1 To N DoFor j:=1 To k DoIf SmallSc[i-1,j]*1. 0+SmallSc[i-1,j-1]>
MaxLonglnt Then SmallSc[i,j]:=MaxLongIntElse SmallSc[i,j]:=SmallSc[i-1,j]+SmallSc
[i-1,j-1]; {* 1.0 ,
.
, ""
.*}End;
.
Type SmallSc=Array[0..MaxN] Of LongInt;Function Res(k:Integer): LongInt;Var A,B:SmallSc;
i,j:Integer;
-
48 2.
BeginFillChar (A,SizeOf (A),0) ;FillChar(B
rSizeOf(B),0) ;
A[0]:=1;A[1]=1;For i:=1 To N Do BeginB[0] :=1; B[1] :=1;For j : = 1 To k Do [j]:=A[j]+A[j-1]; {*
LongInt, ""
.
-
"" .*}:=;
End;Res:=A[k];
End; .
,
N=7 k=5. 21.
0
1
2
3
4
5
6
12345
12346
12347
12356
12357
12367
12456
7
8
9
10
11
12
13
12457
12467
12567
13456
13457
13467
13567
14
15
16
17
18
19
20
14567
23456
23457
23467
23567
24567
34567
1, 2, ..., k, N-k+1,N-k+2, ..., N. - . -
,
. , -
( ) - , .
Procedure GetNext;{*, ( ) .*}
Var i,j:Integer;Begini:=k;
-
2. 49
While (C[i]+k-i+1>N) DoDec(i); {*, .*}
Inc([i]); {* .*}For j : = i + 1 To k Do [j]:=[j-1]+1; {*
.*}End;
. L . (-) SmallSc, . . - ( ) N k. . , , 0 14 -, . -
( N k). L - L , , . L - .
Procedure GetWhByNum (L:LongInt);Var i,j,sc,ls:Integer;Begin
sc:=n;ls:=0; {* .*}For i:=1 To k Do Begin {*i -
; k-i - .*}
j:=1; {*sc-j - (), .*}
While L-SmallSc[sc-j,k-i]>=0 Do gi {*
SmallSc, L.*}
Dec (L,SmallSc[sc-j ,k-i]) ;Inc (j); {* While
,
SmallSc, .. ,
,
( ), L.*}
-
50 2.
End;[i]:=ls+j; {* .*}Inc(ls,j); {*
.*}Dec (sc,j); {* ,
.*}End;
End;
(N=7, k=5, L=17).
-
N=9, k=5, L=78. 23479. , .
-
2. 51
.
. ,
SmallSc. . SmallSc. - , . ,
3 (N=7, k=4). , -
1, 2 3. . , [0] 0 , - .
Function GetNumByWh:LongInt;Var sc:LongInt;
i, j; Integer;Begin
sc:=1;For i :=1 To DoFor j:=C[i-1]+1 To C[i]-1 Do Inc(sc, SmallSc[N-j,k-i]) ;
GetNumByWh:=sc ;End;
(N=9, k=5, C=023479).i1234
5
j1
2, j 4 3, j
568
SC
17171717578
SmallSc[N-j,k-i]70-
-
431
SC
71-
-
757879
(N=7, k=5, =023467).i12345
j1
3 2, j 4 3, j
5 7 6, j
SC
116161618
SmallSc[N-j,k-i]15-
-
2-
SC
16-
-
18-
, -
3 4 . 3 0, 4 1.
-
52 2.
. -
0 1. () N 0 1, [i]=1 , (N-i+1) - (). N=7, k=5. - , -
, .
0123456
0011111
0101111
011011101110110111101
0111110
1001111
12345
1234612356
1245613456
23456
12347
78910111213
1010111
10110111011101
1011110
1100111
11010111101101
12357
12457
13457
23457
1236712467
13467
14151617181920
1101110
1110011
11101011110110
1111001
11110101111100
2346712567
13567
23567
1456724567
34567
.
. C[i]=0 C[i+1]=1. - , .
, i i. ,
0, 1, 1 - .
.
Const MaxN=...;Type MyArray=Array[1..MaxN] Of 0. .1;Var Curr, Last: MyArray; {*
.*}i , j,Num, N, k: Integer;
Begin ...
For i : = 1 To N Do Begin Curr[i]:=0;Last[i];=0;End;For i:=1 To k Do Begin
Curr[N-i+1]:=1; Las t [ i ] :=1; End; {* .*}
While Not Eq(Curr,Last) Do Begini:=n-1;While (i>0) And Not ((Curr [i]=0) And
(Curr [i + 1] =1) ) Do Dec (i); {* 0 - 1 , ,
.*}Num:=0;
-
2. 53
For j : = i + 1 n Do Inc (Num,Curr [j] );{* .*}
Curr[i]:=1;For j : = i + 1 To n-Num+1 Do Curr[j]:=0; {*
.*}For j:=n-Num+2 To n Do Curr[j]:=1; {*3
.*}
End;End;
2.2.4. N. -
: N=al+a2++ak, k, a1 ..., ak - . ,
.
-
b1 ..., bk, . b1 ..., bk - N k .
, : - ; ,
; , - .
. N=8, -, .
1
2345678
111111112111111221111222112222311111321113221
910111213141516
3311332411114211422431445111
171819202122
5215361162718
N k P(N,k), P(N). , P(N) P(N,k) k. , (0)=(1)=1. .
-
54 2.
. N k N , .
. .
, , -
. P(i,j) P(i-j,k) k, k 0 j. -, j i i-j k . .
12345678
000000000
111111111
201122334
300112
.345
400011235
500001123
600000112
700000011
80000000
123
57111522
, (8,3)=(5,0)+(5,1)+(5,2)+(5,3). .
Const MaxN=100;Var N:LongInt;
SmallSc:Array[0..MaxN, 0..MaxN] Of LongInt;Procedure FillSmallSc;Var i, j , k:Integer;BeginFillChar(SmallSc,SizeOf(SmallSc),0);SmallSc[0
r0]:=1;
For i:=1 To N DoFor j : = 1 To i Do
-
2. 55
For k:=0 To j Do Inc (SmallSc [i, j ] ,SmallSc[i-j,k]);
SmallSc[0,0]:=0;End;
,
. -
.
Procedure Calc;Var i;Integer;
Res:LongInt;BeginRes:=SmallSc[N,1];For i:=2 To N Do Res:=Res+SmallSc [N , i ] ;WriteLn (Res:0:0);
End; . . -, -
.
.
Var Now:Array[0..MaxN] Of Integer; Now[0] , . . -
Now. - 8, 41111, 4211. Now[2]? , Now[1] Now[2], . . - Now[i-1]>Now[i], Now[i] , -
. -
? , 44, 5111. , , i 1. , , -
.
Procedure GetNext;Var i,j,sc:Integer;Begin
i:=Now[0]-1; {* .*}sc:=Now[i+1]-1;Now[i+1]:=0; {*B sc
,
.*}While (i>1) And (Now[i]=Now[i-1]) Do Beginsc:=sc+Now[i]; Now[i]:=0; Dec(i); {*
"".*}
-
56 2.
End;Inc (Now[i]); {*
.*}Now[0] :=i+sc; {* .*}For j :=1 To sc Do Now[j+i] :=1; {*3
,
.. .*}End;
-
( 8)? , . -.
, Now , Last . , , In.it. ....
While Not(Eq(Now,Last) ) Do Begin {* Eq "",
, ""
, -
While Now[0]1 Do ...*}GetNext;Print; {* .*}
End;
. (L) - (Now). - . L 0. .
sc:=N; i:=1; {*sc - , , i - .*}
While scOO Do Begin {* .*}j:=1; {*,
i .*}??????????? {*3 j
L, .*}Now[i]:=j; sc:=sc-j; Inc(i); {*
.*}End;
, ??? (), L, 0, -
-
2. 57
, . L 1. .
SmallSc[8,1]. L , L SmallSc[8,1], j , , 2111111., ? SmallSc sc , j, - L. sc, - j. .
Procedure GetWhByNum (L:LongInt);Var x,j,sc:Integer;Beginsc:=N; i:=1;While sc0 Do Begin j:=1;
While L>=SmallSc[sc,j] Do BeginDec(L,SmallSc[sc,j]); Inc(j);
End;Now[i]:=j; sc:=sc-j; Inc (i);
End;Now[0] :=i-1; {* .*}
End; . (Now) -
. . , -
, .
, 22211. , 2, . .
Function GetNumByWh:LongInt;Var i,jk,p:Integer;
sc:LongInt;Begin
sc:=1; {* .*}jk:=N; {* SmallSc.*}For i :=1 To Now[0] Do Begin {*
.*}For p:=0 To Now[i]-1 Do sc:=sc+SmallSc[jk,p];
{*3 SmallSc.*}
jk:=jk-Now[i]; {* .*}End;
-
58 2.
GetNumByWh:=sc;End;
(N=8, Now 22211).
SC1
2
3
44
jk8
6
4
21
i
1
2
3
45
01010100
SC
12
3
444
jk
6
4
210
, , 4.
2.2.5. N
. .
N=4. 8.
00000001
00100100
01011000
10011010
(- P(N))? N. .
P(N1). , - . -
0. - P(N-2). , : P(N)=P(N-1)+P(N-2), a , . -
, N, 1, N- . - LongInt (N=
-
2. 59
1597, 2584, 4181, 6765, 10946, 17711, 28657,46368, 75025, 121393, 196418, 317811, 514229,832040, 1346269, 2178309, 3524578, 5702887,9227465, 14930352, 24157817, 39088169,63245986, 102334155, 165580141, 267914296,433494437, 701408733, 1134903170, 1836311903);
.
Function GetSmall (N: Integer) : Longlnt;Begin
If N>NSmall Then GetSmall:=MaxLongIntElse GetSmall:=ResSm[N] ;
End; . . -
=6 ( , -).
0123456
000000000001000010
000100000101001000001001
78910111213
001010010000010001
010010010100010101
100000
14151617181920
100001
100010100100
100101
101000101001
101010
: - ,
..0010... ...0100... ..001010... 010000.... - . -
Now.Procedure GetNext;
Var i: Integer;Begin
i : =N;While (Now[i]=l) Or ( (i>l) And (Now[i-1] =1) )
Do BeginNow[i] :=0;Dec(i) ;
End;Now[i]:=2/
End;
-
60 2.
. -
. . L -, N1, () 0 1? : 1. !- - N1 - L. .
N=6, =15.
1522220
/1
_ _ _ 2 _ |3456
138532
1 '
Now (******)-j * * * * *
10****100***1000**10001*100010
Procedure GetWhByNum(L: Longlnt);Var i: Integer;BeginFor i:=1 To N DoIf L>=GetSmall (N-i) Then Begin Now[i]:=1;
L:=L-GetSmall(N-i)/EndElse Now[i]:=0;
End;
. -
-
. i 1,
N-i. , - 1 - .
Function GetNumByWh: Longlnt;Var i: Integer;
sc: Longlnt;Beginsc:=l;For i:=l To N Do If Now[i]=l Then Inc
(sc, GetSmall (N-i));GetNumByWh :=s ;
End;
-
2. 61
2.2.6. . iV--
1 N , -
. -
: ; ;
.
. N 2N. , N . N=15 32768, Integer . N 100. - N -, : (- 1- , Mul) ( 1- , SayTLong).
N . . N , -.
Procedure Calc;Var i: Integer;
Tmp: TLong;Begin
i :=N; { * .
10000, 13 8192.*}Res[1]:=1; Res[0]:=1;{* 0 !*}While i>0 Do BeginIf i>13 Then Begin Mul(Res, 1 shl 13, Tmp);
i:=i-13; End {*, shl - , 1 13 , .. 213. 214?*}
Else Begin Mul (Res, 1 shl i, Tmp); i:=0; End;Res:=Tmp;
End;End;
. iV- 1 N N Now[l], Now[2],..., Now[N]
-
62 2.
( Now). Now[i], , , Ni+1 .
0123456789101112131415
()0000000100100011010001010110011110001001101010111100110111101111
(Now)0000000100110010011001110101010011001101111111101010101110011000
[][1]
[1,2][2]
[2,3][1,2,3]
[1,3][3]
[3,4][1,3,4]
[1,2,3,4][2,3,4]
[2,4][1,2,4]
[1,4][4]
-
0 2 4 -1. - ( ), , , -
2, , Now. . .,Now[l]:~B[l] i 2 N. , -
Now. , -
. Now .
Procedure TransferB;Var i: Integer;BeginFillChar (Now, SizeOf (Now) , 0) ;Now[l]:=B[1];For i:=2 To N Do Now[i] :=B[i] Xor B[i-1] ;
End;
-
2. 63
: [1]:=Now[1], i 2 N.
Procedure TransferToB;Var i , j : Integer;BeginFillChar(B, SizeOf(B), 0);B[l] :=Now[l] ;For i:=2 To N Do B[i] :=Now[i] Xor B[i-1] ;
End; ,
,
.
-
.
Procedure GetNext;Var i: Integer;Begin
i:=N+l;Repeat
Dec (i) ;B[i] :=B[i] Xor 1;
Until B[i]=l;Now[i]:=Now[i] Xor 1;
End; {* 1111 (N=4) ?*}
-.
,
, , -
. ,
2*N. - ( ) . ,
. , , -
, . -
0000 N=4 .
-
64 2.
0100:
(- 1100):
(- 1000):)
.
L L . -
L - .
Procedure GetWhByNum (L: Longlnt);Var i: Integer;Begin
For i:=l To N Do BeginB[N-i+l] :=L And 1; {*
L. *} L:=L shr 1; {*' L (
2) . *}End;
End; .
-
-
.
Function GetNumByWh: Longlnt;Var sc: Longlnt;
i: Integer;Begin
sc:=0;For i:=l To N Do sc:=sc*2 + B[i] ;GetNumByWh:=sc+l;
End;
-
2. 65
2.2.7. 2*N ,
i (l
-
66 2.
Now[i-1]: = ' ('; Now[i]: = ') ';{". *}Now:=Copy(Now,I,i);
sc:=0;{" . *}
For j:=l To i Do If Now[j ] = ' (' Then Inc(sc) ElseDec (sc) ;
While sc0 Do Begin{" . *}Now:=Now+r)';Dec(sc) ;
End;While Length (Now)
-
2. 67
. ScSmall[3,l]=2: ((), ()(. ScSmall[4,2]=3: (((),(()(, ()((. ScSmall[4,0]=2: ()(), (()). ScSmall[5.1]=5: ()()(,(())(, ((). (()(). ()(() :ScSmall[5,l]=ScSmall[4,0]+ScSmall[4,2]. - ScSmall[4,0] -, ScSmall[4,2] . - .
ScSmall[2*N,0].
01
2
34
567
8
-1
00
0
00
0
00
0
01
01
02
0
5014
1
01
02
0
5014
0
2
001
03
090
28
3
000
104
014
0
4
000
01
0
50
20
5
00
0
001
060
6
00
0
0
001
07
7
00
0
0
00
01
0
800
0
0
00
001
Longlnt.Const SmallN=37;Var ScSmall: Array[-1. .SmallN + 1, -1. .SmallN + 1]
Of Longlnt; -
.
Procedure FillSmallSc;Var i , j : Integer;BeginFillChar (ScSmall, SizeOf(ScSmall), 0) ;
ScSmall[0, 0]:=l;For i:=l To SmallN-1 DoFor j:=0 To SmallN Do ScSmall[i,j]:=ScSmall
[i~lrj-l]+ScSmall[i-l,j+l];
End;
SCSmall. 'Function GetSmallSc (N, Up: Longlnt): Longlnt;Begin
If (N
-
68 2.
If (N>SmallN) Or (Up>SmallN) Then GetSmallSc:=MaxLongIntElse GetSmallSc:=ScSmall[N, Up];
End; -
.
. -
Up, , - .
Procedure GetWhByNumfL: Longlnt);Var i, Up: Integer;
P: Longlnt;BeginNow:=rr; Up:=0;For i:=l To N*2 Do BeginP:=GetSmallSc(N*2-i, Up-1);If (L>=P) Then Begin Now:=Now+'('; Inc (Up);
L:=L-P; EndElse Begin Dec (Up); Now:=Now+')'; End;
End;End;Function GetNumByWh: Longlnt;Var sc: Longlnt;
i, Up: Integer;Beginsc:=l; Up:=0;For i:=l To N*2 DoIf Now[i]='(' Then Begin sc:=sc+GetSmallSc
(N*2-i,Up-l) ;Inc (Up) ;EndElse Dec(Up);GetNumByWh:=sc;
End;
2.3. 1. -
k 1, 2, ... N. 1, 1, ...,1, N, N, ..., N.
2. - k, i- i. 1, 1, ..., - 1, 2, ..., k.
-
2. 69
3. N - (, - , ) (, N=4):
4, 3+1, 2+2, 2+1+1, 1+1+1+1; 4, 2+2, 1+3, 1+1+2, 1+1+1+1; 1+1+1+1, 1+1+2, 1+3, 2+2, 4.
4. - 2*N, N N , ,
. .
.
5. , . - R(N,1).
Procedure R(trk:Integer);
Var i:Integer;Begin
If t= l Then Begin A[k]:=1;;End
Else Begin[]:=t;;For i:=l To t-1 Do Begin A[k]:=t-i;R(i,k+l);End;End;
End;
6. - . 2-5 . 2.2.1.
7. 1-5 . 2.2.1 -.
8. 1-5 . 2.2.2 -.
9. 1-5 . 2.2.3 -.
10. [18] - .
Const -4 ;Var A:Array[l..n] Of Integer;
i:Integer;Function Place (i ,m: Integer) -.Integer;Begin
If (m Mod 2=0) And (m>2) ThenIf i
-
70 2.
Else Place:=m-2Else Place:=m-l;
End;Procedure Perm (m:Integer);Var i,t:Integer;Begin
If m=l Then Begin For i:=l To n Do Write (A[i] :3) ;WriteLn;End
ElseFor i :=J To m Do Begin
Perm (m-1) ;If Km Then Begin t: =A[Place (i ,m) ] ;
AfPlace(i,m)]:=A[m];A[m]:=t;End;End;
End;BeginFor i:=l To n Do A[i]:=i;Perm (n);
End.11. [18] -
.
Const n=4;Var A:Array[1..] Of Integer;
i,j,p,l:Integer;BeginFor i:=l To n Do A[i]:=0;i:=0;Repeat
For 1:-1 To n Do Write (A[l] : 3) ;WriteLn ;i : =i +1 ;p: =1 ;j : =i ;While j Mod 2=0 Do Beginj:=j Div 2;p:=p+l;
End;If pn;End.
12. [18] - .
Const n=5;^;
Var A:Array 11. .] Of Integer;i,j,p:Integer;
-
2. 71
BeginFor i:-l To Do A[i] :=i;p:=k;While p>=l Do BeginFor i:=l To Do Write (A [i] : 3) ; WriteLn;If A[k]=n Then p:=p-l Else p:=k;If p>=l ThenFor i:=k DownTo p Do A[i] :=A[p] +i-p+l ;
End;End.
13. . - , -
: ( ) (). -
,
. ,
.
, -
-
N (1
-
72 2.
P(N-2). , : P(N)=P(N-l)+P(N-2), a , .
, N, - 1, iV- . Longlnt N
-
2. 73
: 1, 2 . . : - , -
( !) ,, , -
. , .** -
,
. ,
-
. 3 2 16 45. , () - .
, . , -
-
,
, . -
. -
.
, .
, , ( ) , , .
Const NMax=13;Type MyArray=Array[l..NMax] Of Word;Var A:MyArray;Function Check (Const P-.MyArray) :Boolean;
{* - ?*}
Begin
End;
Procedure Solver-Type pnt=*Ilem;
Ilem=Record{* . " ". .
- ykw, - ykr. *}
. , -
, , - .
-
74 2.
Next:pnt;Data:MyArray;End;
Var R,Q:MyArray;ykr,ykw, p:pnt;i -.Word;head:pnt;
Function NotQuerBoolean;{* - ?*}
Begin
End;BeginNew (ykw);.Data:=;.Next:=Nil;head:=ykw;{*' . *}
ykr:=head;While ykrOnil Do Begin {* . *}:=
.Data;{* . *}For i:=l To N-l Do Begin {*
. *}Q:=R;Swap(Q[i] ,Q[i+l]) ;If Check (Qj And NotQue Then Begin {*
,
. *}New ();.Data:=Q;pA.Next:=Nil;ykw*.Next:=p;ykw:=p;Print (Q);{^
. *}End;End;ykr:=.next;{*
. *}End;End;Begin {* . .
) . *}Solve;
End.
-
2. 75
15. N .
, -
-
. -
,
2 3 2 8 1. , ,
(, , , ). . , -
:
, -
N, . . in-
put.txt N (l
-
76 2.
(1
-
2. 77
tm:=tm*A[p+l] ;Inc (p) ;
End;GetMul:=tm;
End;
(, , . . ). - . 2N , 0 9, , N JV . - , . ,
[,], , , -
20 . =3, =1, 5=999999. 55251. - . , -
.
Function Rec (,Sum:Longlnt) :;{* k ~ Sum - .*}
Var i:Integer;Ans:Longlnt;
BeginIf k=2*N+l Then If Sum=0 Then Rec:=l Else Rec:=0Else Begin
Ans:=0;For i :=0 To 9 Do
If k
-
78 2.
17. N -. , -
( . . ).
. -
5. . (, ) , -
, -
. , N -, C3N, O(7V4).
-
3.
, -
*. ,
.
,
, .
, : , - : ( ) - (); - , ,
. -
-
. -
, , .
3.1. ( ) N U
v U2, ..., UN (N -
), =(1, 2, ..., aN), -
.
, , ,
,
-.
-
80 3.
-
. ,
-1 , A={av 2, ..., _1, ?, ..., ?), -
Else Begin
;For Do Backtrack (,i+1) ;
{*\\ - . *}End;
End; . -
.
, 2V, - I U^ *| U2\ *...*! UN\ . Ut , CN .
-
3. 81
3.2.
1 . -
. N*N N , -.
.
^2 ( 4,4*109 N=8). - , NN - (1,7*107 iV=8). , , (at, a2, ..., aN) , (1,2,..., N), N\ (4,0*104 N==8) . - ,
( 2V=8 2056 ). , -
N N*N. -
,
. .
, , -
. ,
: - ,
. , -
, af^N/2], - , a^N/2]. -, , , , 12 UJ^N1, .
.
.
Up: Array [2. .16] Of Boolean ;{ * . *}
Down:Array[-7.. 7] Of Boolean;{* . *}
Vr:Array[I..8] Of Boolean;{* . *}
X: Array [1. .8] Of Integer;{ * , . *}
-
82 3.
, -
( ).Procedure Hod(i,j:Integer); {* .*}Begin
X[d] :=j;Vr[j] : =False;Up [i+j] :=False;Down[i-j]:=False;
End;Procedure O_hod (i,j:Integer);{* .*}Begin
Vr [j]:=True;Up[i+j]:=True;Down[i-j]:=True;End;
Function D_hod(i,j:Integer):Boolean;{* (i,j). *}
BeginD_hod:=Vr[j] And Up[i+j] And Down [i-j] ;
End;
:
Procedure Solve(i:Integer;Var q:Boolean);Var j:Integer;
-
3. 83
Beginj:=0;RepeatInc (j);q:=False;{* . *}If D_hod (i,j) Then BeginHod(i,j);If i
-
84 3.
). , - N :
, -
; 90
; -
. ,
.
N, 1 N. , -, i-. -. , ,
,
. Siml, Sim2,Sim3 - , -
. ( ),
, ,
-
.
, True -, -
. ( ) .
Type TArray=Array [I. .N] Of 'Integer;
Procedure Siml(Var X:TArray);Var i:Integer;Begin
For i:=l To N Do X[i] : =N-X[i]+1 ;End;
Procedure Sim2 (Var X:TArray) ;Var i , r:Integer;Begin
For i:=l To N Div 2 Do Beginr:=X[i]; X[i]:=X[N-i+1];X[N-i+1]:=r;
End;End;
Procedure Sim3(Var X:TArray);
-
3. 85
Var YiTArray;i:Integer;
BeginFor i:=l To N Do Y[X[i]J:=i;X:=Y;
End;Function Cmp (X,Y:TArray) -.Boolean;Var i:Integer;Begini : =1 ;While (iN Then Cmp:=FalseElse If Y[i]
-
86 3.
-. -
. , - ,
.
: -
.
Program Ferz;Uses Crt;Const N=8;Var B:Array[1. .N] Of Integers-Procedure Rf (i:Integer) ;Var j,k,p,t:Integer;BeginFor j:=l To N Do BeginB[i]:=j;k:=l;p:=O;While (k
-
3. 87
( ). -
. , -
. -
.
.
Const N= ; M= ;Dx:Array[1..8] Of Integer=(~2,-1,1,2,2,1,-1-2);Dy:Array[1. .8] Of Integer=(1,2,2,1,-1,-2,-2,-1);Var A:Array[-l..N+2,-1..M+2] Of Integer;t:Integer;
Solve.Procedure Solve(,,q:Integer);Var z,i,j:Integer;BeginA[x,y]:=q;If q=N*M Then Inc (t)Else For z:=l To 8 Do Begin
i:=x+Dx[z];j :=y+Dy[z];If A[i,j]=O Then Solve (i , j ,q+l)
End;A[x,y]:=0;
End; :
For i:=-l To N+2 DoFor j:=-l To M+2 Do A[i,j]:=-l;{*3"" ,
If, . *}For i:=2 N Do' For j:=l To M Do A[i,j] : =0 /t:=0;For i:=1 To N DoFor j : =1 To M Do Solve (i,j ,1) ;
-
88 3.
WriteLn{' ',N,'*' ,,' - ' ,t) ;
-
N , ,
( !). ,
. ,
( 150 ). , ,
, ,
. -
(, N - ).
Solve .Procedure Solve(,,q:Integer);Var W:Array[1. .8] Of Integer;
xn,yn,i,j ,m,min -.Integer ;BeginA[x,y]:=q;If (q
-
3. 8!)
W[m] : =Maxint;{* . *}
End;Inc(i) ;End;EndElse Begin ;halt;{* ,
,
. *}End;[,]:=0;End;
3 . . , -
. .
, . -
-
.
-
.
, -
1891 . : ; ,
( -) .
. 1891 . .
Program Labirint;Const Nmax=...;Dx:Array[1. .4] Of Integer=(1,0,-1,0);Dy:Array[1. .4] Of Integer^(0,1,0,-1);Type MyArray=Array[0..Nmax+1,0..Nmax+1] Of Inte-
ger;Var A:MyArray;
xn,yn,xk,yk,N:Integer;Procedure Init;
Begin{* , .
. *};
-
90 3.
End;Procedure Print;
Begin{*' - .*};
End;Procedure Solve(,,k:Integer);{*k - ,
, - . *}Var i:Integer;BeginA[x
ry]:=k;
If (x=xk) And (y=yk) Then PrintElse For i:=l To 4 Do IfA[x+Dx[],y+Dy[i]]=O
Then Solve (x+Dx[i] ,y+Dy[i] ,k+l) ;A[x
ry]:=0;
End;Begin
Init;Solve(xn,yn,1);
End.
-
. -
( Dx, Dy) . , -
, , . , -
.
( - ) . . , , -
1, . -
, , -
. ,
, : - ; -
.
-
( , , , -
). -
( )
-
3. 91
(). . .
, . , -
,
.
Program Labirint;Const NMax=...;
MMax=...;Dx:Array[1..4] Of Integer^(-1,0,1,0) ;Dy-.Array [1. .4] Of Integer^ (0,1, 0 , -1) ;
Type MyArray=Array[O..NMax+1,0..MMax+1] Of Integer;Var A:MyArray;{* . *}
N,M: Integer;xn,yn,xk,:Integer;
Function Solve:Boolean;Type Och=Array[l . .*,1. .2] Of Integer;
Var tfi,j,ykr,ykw:Integer;:Och;{*. *}Y-.Boolean;
BeginA[xn,yn]:=1/ ykr:=0{*
. *};ykw:=l; {* .*}Y:=False;{* . *}[ykw,1]:=;0[ykw,2]:=;{*3
. *}While (ykr
-
92 3.
Inc(ykw) ;O[ykw,l] :=i+Dx[t] ;[ykw,2]:=j+Dy[t];{*'
. *}End;
End;Solve:=Y;{*' Solve. *}
End; .
BeginInit;{* ,
.
. *}Assign (Output,'Output.txt' );Rewrite(Output);
{*' ( ) .*}If Solve Then Print{* Print
. *}Else WriteLn (' '};
Close (Output);End.
, -
, , , t , .
4 ( ). N ( ). W. I wt vt(=1,2, ..., N). , W. i kt,
v1*k1+v2*k2+...+vN*kN
w1*k1+w2*k2+...+wN*kN=W, ki (O^k^lW/wJ), - .
,
N (-, ?). , :
Const MaxN=????;Var N,N:Integer;{^ ,
. *}Weight,Price:Array[1. .MaxN] Of Integer;{*Bec,
.*}
-
3. 93
Best,Now:Array[I..MaxN] Of Integer;{*
. *}MaxPrice:Longlnt;{^ . *}
, :
Procedure Solve(k,w:Integer;st:Longlnt); {*k - , w - ,
, st - . *}Var i:Integer;Begin
If (k>N) And (st>MaxPrice) Then Begin {*. *}
Best:=Now;MaxPrice:-st; EndElse If k
-
94 3.
:Array[1..,1..Max] Of Byte;{^,
,
,
.*}. (
).
@ .Way,BestWay:Array[1..Max] Of Byte;{*
. *}Nnew:Array[I..Max] Of Boolean;{*3
False ,
. *}BestCost:Integer;{* . *}
. v. .
1. (), - ,
(BestCost), - .
2. ( ), - -
.
, BestCost BestWay - .
3. v , - Count Way.
4. v . ,
v, Count, Cost, .
-
3. 95
5. v .
,
. -
.
,
.
, .
Procedure Solve(v,Count:Byte;Cost:Integer);{*v - ; Count - ; Cost - . *}Var i:Integer;Begin
If Cost>BestCost Then Exit;{*
. *}If Count=N Then BeginCost:=Cost+A[v,1]/Way[N]:=v;{*
.
. *}If Cost
-
96 3.
Nnew[v]:=False/Way[Count]:=v;{* v,
. *}For i:=1 N DoIf Nnew[B[v,i]] Then Solve (B[v,i],
Count+1,Cost+A[vrB[v,i]]); {* ,
,
v.*}Nnew[v] : =True; {^ v
. *}End;
Solve(l,l,0). - .
, -
( ). - 1 8 9 2 5 6 10 7 4 3 1, 158.
@321933224118151631
32@5158274235181734
1951@23354926343541
335823@333723464632
22273533@191023239
4142493719@24424210
183526231024@252514
15183446234225@132
161735462342251@32
31344132910143232@
.
, [1..50,1..50] . -
, 2500 - .
3.3. . , -
: -, .
. -
.
.
. -
-
3. 97
,
. .
, ,
21 . , 6: . 20 (3
). - . ( 21). - , 100 - 19 . . N, 8, 12870 . - 15 , 193050 12869 , - 200000 . 1000000 0.2 , ? , N 30, - 60!*(30!*30!). , - 101 7. , -, 60*1017. 1000000 3.2*1013 (). , .
. -
. , -
. ? X, Y . Z X 15 , Y 11 . - , . . 11. . . , -
, . -
.
,
. . . N, 300, 3*301*301, 1000000, -
-
98 3.
. , N=30 1 iV=300.
. -
. -
, -
. ,
-
. .
,
17 . -
.
3.4.
1 -. -
.
, -
, -
, -
-
.
-
.
> 1
-
3. 99
For j:=l i Do R[i,j ]=max(D[i,j ]+R[i-l,j ] ,D[,j]+R[i-l,j-l]);
-
.
R, 30. 2 .
N k. , kN. - , k. - , q q1 . , N. - N.
, N=5 (k*k)2*k. , Op[i]
i (Op[l]=0). , N- k, , -
.
.
,
h, N. - N, , .
:
opnl: =Min { : l
-
100 3.
Op[i] :=Min(Op[i] , Op [j ]+ [i-j ]+1) ;If i Mod j=0 Then Op[i] :=Min (Op[i] ,
Op[i Div j]+j-l) /End;
End;End;
3 - ( - ). -. -
(, , , ). ( N), , , , .
, .
-
.
-
-
. -
,
. -
W -: W[i,j]
W[i-l,j], W[i,j-1], , W[i-l,j-l]+l. - -
: ,
. , W[3,l] , . -
-
, .
, , -
.
. -
: .
.
For i:=l To Length (SI) DoFor j:=l To Length (S2) Do BeginA[i,j] :=Max(A[i-l,j] ,A[i,j-l]) ;
-
3. 101
If SI [i]=S2 [j] Then A[i , j] :=Max (A[i , j] ,A[i-l,j-l]+l) ;
End;WriteLnf: ', A [Length (SI) , Length (S2) ]) ;
4 N-. - JV-, . N-2 -.
. -
.
.
S[k,l] -
A[k,l] - .
l=k+l k+2 S[k,l]=O,, l>k+2. - i, i - k+1 11, - -
A[k,l]. :S[k,l]~min{dMUHa + +S[k,i]+S[i,l]}. , i=k+l . i=ll.
5 . 1,2, .....pN W , , - W .
: N 5, Pj=5, 2=, 3=9, 4=11, 5=13, W=19. - (A:Array[l..N,0..W] Of Integer), ,
. , () , ( ).
. . , -
. 19 , 18.
-
12345
000000
100000
200000
300000
400000
555555
655555
757777
857777
957999
10 11 1257999
5791111
512121212
13 14 15 16 17 18 19512121212
512141414
512141414
512161616
512161616
512161818
512161818
( ), A[i,j] - Ni W=j. .
Procedure Solve;Var i,j:Integer;BeginFor i:=2 To N DoFor j:=1 To W Do BeginIf j-P[i]>=0 Then A[i,j]:= Max (A[i-l ,j] ,
A[i-l,j-P[i]]+P[i])Else A[i,j]:=A[i-l,j];
End;End;
, ,
-
. , -
, -
i /', P[i]Procedure Way(i,j:Integer);BeginIf (i=l) And (A[i,j]=O) Then ExitElse If i=l Then Begin Way (i ,j-P[i] ) ;
Write(P[i],' ');EndElse If A[i,j]OA[i-l,j] Then Begin
Way(i,j-P[i]) ;Write(P[i] ,' ') ;EndElse Way(i-l,j) ;
End; 6 (
). . N ( - ). W. i wi
102 3.
-
3. 103
vt (=1,2, ..., N). - , W. - , W. i kp - v1*k1+v2*k2++vN*kN
u>1*k1+w2*k2+...+wN*kN=W, kt (0
-
104 3.
) . ,
. -
.
.
Const MaxN=???;=???;
Type Thing=Record W,V:Nord; End;Var A:Array[1. .MaxN,0. .MaxK] Of Word;
P:Array[1..MaxN] of Thing;Old,NewA:Array[0..MaxK] Of Longlnt;N,W: Integers-
Procedure Solve;Var k,i,j:Integer;Begin
FillChar(Old,SizeOf(Old) ,0) ;For k:=l To N Do Begin { * . *}FillChar(NewA,SizeOf(NewA) ,0);For i:=0 To N Do {* . *}For j :=0 To i Div P[k] .W Do {*
-
.*}If j*P[k].V+01d[i-j*P[k].W]>=NewA[i]
Then BeginNewA[i] :=j*P[k] .V+Old [i-j *P [k] .N] ;A[k,i]:=j;{*3 j ?*}
End;Old:=NewA;
End;End;
.
Procedure OutWay(k,l:Integer);Begin
If k=0 Then ExitElse Begin
OutWay(k-l,l-A[k,l]*P[k].W) ;{*A . *}Write (A[k
r 1] ,' ');
End;End;
OutWay(N,W). - . .
-
3. 105
.
yL I , - i, i+1, ..., N j/ i=W j/.=0,i,...,W =2,3, ...,N. .
3.5.
. . .
.
(), ( ) - , ,
. ,
. -
( ). ,
-
, -
. -
., ., ., . . -
.// . 1965. 1. .13-22.
-
106 3.
, ,
.
. -
, -
, .
. :
24.
: - 2, 1. - 27. .
( ), 27. (1-3-2->4->5-6->1) , - . 27 . -
. ,
. -
. -
. .
-
3. 107
( 14) :
, .
. -
( ). -
, (1, 2)., , ,
. () ,
.
.
- ,
.
, 1+0=1. , , -
1. . -
. ,
. , . -
, (1,2) - (1,2). - , . . 15. .
() . , (2,1) , .
-
108 3.
. , , -
. , , -
(1,2), 14. ,
, .
-
(3,1) (3,1). - 14+5=19. -
, (2,3) - . -
.
-
. -
,
14+2=16. .
-
.
.
,
-
.
. , -
16 1246>531. -,
16, -
.
,
15. - 16 17. -. ,
16. -
( ). ? - , -
-
3. 109
-
. -
.
-
.
. 3.2.2 -
, -
. -
N? -,
iV
-
110 3.
Procedure Search (Var S:Number);Var i,j:2..N;Begin
S: = [2. .N] ;For i:=2 To N Div 2 DoIf i In S Then Beginj : =i +i ;Nhile j
-
3. 111
cnt:Integer;{* .*}ok:Boolean;{* - . *}
Procedure Init;Var i,j,k,1:Integer;Begin
For i:=2 To 6 DoFor j:=1 To 6 DoFor k:=l To 6 Do
For 1:=1 To 6 DoA[ (i-1) *216+(j-l) *36+(k-l) *6+l] ; =
Chr (i +Ord 0')) +Chr (j +Ord ('' 0')) ++Chr(k+Ord('0'))+Chr(1+Ord('0') ) ;
For i:=l To Do [i ] :=True;ent:=0;ok:=False;
End; . -
, .
32, 24. 1 0, - kr (1) bk(O).
11121314212223243132333441424344
TrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrueTrue
( )
FalseFalseFalseFalseFalse
False
False
( kr)False
False
False
False
False
FalseTrueFalseFalseFalseFalseFalseFalseFalseTrueFalseFalseTrueFalseTrueFalse
, 16 , - . ,
kr bk, :
-
112 3.
Function Pr (a,b:Post;kr,bk:Integer) -.Boolean;Var i,x:Integer;Begin{* "".*}
x:=0;For i:=l To 4 Do If a[i]=b[i] Then Inc(x) ;If xObk Then Begin Pr:=False;Exit;End;
{* "". *}x:=0;For i:=l To 4 Do If (a[i]ob[i]) And (Pos (b[i] ,a) 0)
Then Inc(x) ;If xOkr Then Begin Pr:=False;Exit;End;Pr:=True;
End; h.Procedure Hod(h:Post);Var i,kr
rbk:Integer;
BeginInc(cnt) ;Write(cnt:2,' . ' ,h,'-');ReadLn(kr,bk);If bk=4 Then Begin ok: =1'rue/;EndElse For i:=l To Pmax Do If B[i] And Not Pr(A[i]
r
h,kr,bk) Then B[i]:=False;End;
.
BeginClrScr;Init;Hod('1223');While Not ok Do Begin
\ "|Hod(h);
End;End.
:
cnt ? - .
? . , (-
-
3. 113
- True), - .
. -
.
3.7. 1. *. -
, .
, . ,
-
. ,
, , -
, . , -
, ,
.
, -
,
.
: 1 N (4
-
114 3.
, .
:
-
.
-
, ,
. , -
. .
,
, ,
, , -
.
( - ). -. . -
. : -, , , -
-
; -, ,
,
, -
; -, -
, ,
.
: . -
() .
, .
.
, .
-
3. 115
Const Nmax=150;Type Nint=0..Nmax+l;
Sset=Set of 0..Nmax;Var A:Array[Nint] Of ;
N:Integer;{* *.} .
Procedure Solve(k:Nint;Res,Rt:Sset); {*k- A; Res - , ; Rt - , ""
; min - ; mn - ; Rbest - ;Rwork - ; -Solved, [], [1..N]) .*}Var i:Nint;Begin
If Rt=[] Then Begin If nm
-
116 3.
i i , ,
i , .2. -
, ? . , -
9*9, 10*10 11*11.. ,
, ,
.
3. N*N(N
-
3. 117
, : ; ; ,
.
M*N (50). - 0 4 .
-
, ,
.
.
(0
-
118 3.
C[i] , i- (i+l)-u -, [] - . ,
, -
, -
, .
.
2* , 2** . - On, Ag: Array[1..M] Of Record wh, pr:Integer;End;. On[i], (wh) (jpr) i-ro , . i -: , . i - , +1, , - i=M. , On[i]=min{Z[i],C[i]+On[i+l].pr}. - On[i+lJ.pr? ; On[j]:=(j,Z[j]). On[j1],On[j-2], ..., On[j+1]. - Ag[i], - i On[i].pr Ag[i].pr .
10. (Array [1..N.1..M] Of Byte), 0 1, , 0.
. N=5, =6 (. ). (Array[l..N,l..M] Of Byte) . -
-
(B[i,j]=lA[i,j]) . , i=2..N j=2..M B[i,j]=O, A[i,j]=l, B[i,j]=Min(B[i-l,j],B[i,j-l],B[i-l,j-l])+1, A[i,j]=O. . -
3. , -
-
? ?
-
3. 119
Procedure Solve;Var i, j, , nx: Integer;Begin
sqa:=0;{*. *}For i:=1 To N DoFor j:=1 To M Do
If A[i, j]=0 Then B[i, j]:=B[i, j-l]+l ElseB[i, j]:=0;{*B , - . *}
For i:=1 N DoFor j :=1 To M Do Begin
nx:=B[i, j];For k:=i DownTo 1 Do Begin
nx:=Min(nx, B[k, j]);If nx* (i-k+1) >sqa Then sqa:=nx* (i-k+1) ;
End;End;
End;11. *. N*M -
2*1 (
-
120 3.
-
, .
.
,
, : 1, - 0. , -
00100011 (35). - 2' ( 0 2'-1), i .
,
. (Jk,j) - i, , - k. B[k,j] - .
B[O,j] - (i,j) .
, [0,0]=1 i, , -
, .
B[k,0]=0 k0, .
B[k,j] i. -, B[l,j1] I (0
-
3. 121
For j:=l 20 Do Begin {* . *}
For :- max Do {* . *}For l:=0 To max Do {* 1.*}If Can(k,l,i) Then [k,j] ;=S[k,j] +B[1 ,j-l] ;
{*' "" Can (k,l,i). *}
A[i,j]:=B[O,j];End;
End;End;
-
-
.
( ) - . -
, , -
I k, , .
-
.
. -, N ( ), - 0. -, =1 ^ 1. -, - . -,
2* - : A[2,t]=A[2,t-l]+A[2,t-2] ( ). -
-
Solve. . N - (t=8, y'=20), 3547073578562247994.
12. *. , .
. -
, ,
, , -
1993 .
-
122 3.
. -
, .
-
,
( ). -
. : - ; -, -
.
. -
,
.
West:
- -
, -
i, . . - 1..(1). ?
, ,
. Qt , - i i . , . . d[j] (jeQt) ,
.
:
, ? d , . d[i], /', . - : A:Array [I..max] Of Recordd, l:Byte; End;. , - . -
-
? . (-
-
3. 123
ray[ 1 ..max,I ..max] Of Record d,l:byte;End;). A[i,j].d ( i j) , : i 1( ) 1 / ( ). - N-ro N-&. , A[l,l].d=l uA[i,j]A[j,i]- Qt - , i. :
A[i,j] . d=max (A[k,j] .d+1) , keQi, koj , j1;A[i,i] .d=max (A[k,i] .d) , i>l keQi.13.
.
. , : New.Ar-[1..] Of Boolean; way, way_r:Array[l..2*max] Of Byte; -
(New[i]=True i -). - .
_.
. I.
.
Begin
FillChar(New,SizeOf(New) ,True);yk:=1;Way[yk]:=2;pp:=True;yk_max:=0;Solve (1) ;
End. .
Procedure Solve(i:Byte);Var j ,pn
rpv:Byte;
Begin
-
124 3.
If i=N Then pp:=False; {* . *}If (i=l) And Not pp Then Begin {*
. *}If yk>yk__max Then Begin {* .*} max:=yk ; way_r:=way;
End;pp:=True; {* . *}
End;(* , . *}
If pp Then Begin pn:=i+l; pv:=n; EndElse Begin pn:~l; pv:=i-l; End;
For j:=pn To pv DoIf West [i,j] And New[j] Then Begin { *B
j i, j . *}
Inc ();Way[yk]:=j;New[j]:=False; {* j . *}
Solve (j) ;New[j]:=True;Way[yk]:=0;Dec (yk); {*
j . *}If j=N Then pp: =True ; {*
(N) , . *}
End;End;14. . N . -
i . -
-
.
, . .
, , .
( 0). - i , , -
St.
, -
. .
, ,
, ,
-
.
-
3. 125
. N, , (l
-
126 3.
17. , , -
- -.
.
, -
(, 3 7, 5 6, 10 11), , - . ,
. , 7- - 11-, , 8-, . .
: N ,
I, m N=l*m (1
-
3. 127
18. N*M (2
-
128 3.
21. . - .
1*1*4 , .
. ( ) , .
22. 8*8 : -. -
, . -
.
( ): ; .
.
.
23. N*N (N
-
3. 129
, .
, -
, -
.
(), -.
, -
1 . - . -
8 , , 1, -
'_' 8 .
, . -
, .
. . -
,
. -
. -
.
, .
25. , . ,
, -
. , -
, 7, 36.
0
b
01000
1100
1200
1300
b1330
1360
b1390
b13120
131212
13
1224
13
1236
, .
. . -
, -
,
-
130 3.
. , '' '&' -. '' 'aaab' , ''. ,
'&' '' , - '' 'aaab' ( ), '' 'aaab'( ).
26. : 2, 3, 4, 6, 7, 8, 9, 10. 6x6, -, ,
, -
.
+
#
+11
@@
I
$
11$
#
+11#+
@
@I
11
#
;
, 11 4.
.
, .
, , -
-
( 40).27. MxN (1
-
3. 131
. L+1 - L+1 /) , 1 L . , - .
28. Heroes of Might and Magic III - , -
. -
. -
-
. -
.
NxM, 0 () 1 (). , 1, ,
,
.
. -
N ( , 1
-
132 3.
. , -
, -
, (-1) ( - ), ,
.
? - . (-), :
; () -
, ; , -
-
, (
). -
, -
. , -
,
, -
.
29. *. - ,
N . - =4 - .
,
+() * (). - 1 N.
.
: Vt V2, -
; Vt V2
, , -
, V1 V2.
1999 .
-
3. 133
, . -
, -
.
. ,
. 3. - 1, 4 , , 2. - 0.
, -
,
.
. pol.in N -. .
N. 1,..., N, ( , 1 2, 2 3 , - N 1). t, - +, , - *.
.
pol.out - ,
.
, -
.
.
. 3
-
134 3.
(-7+4)*(2*5), (-7+4*2)*5. :-ray[l..MaxN,l..MaxN] Of Longlnt, MaxN 50 ( ). Max[i,j] - j, i( ). , [1,3] 7+4*2. , Max[i,l] i. -. . -
.
. (- ).
.
30. *. - . ,
-
.
.
.
, , -
. -
. -
,
-
.
2000 .
-
3. 135
,
-
, -
.
. (post.in) - : V,1
-
136 3.
-
.
-
-
-
100
20
31
42
53
67
712
821
937
10 I43 |
. ,
3. i. . -
. -
. -
i=7 ( 7 3 ). , 1-
6-. ( 7, -, 7), - . -
1- 5-. 3, 6 - 2, 5. 3, 4 5 .
100
00
200
00
00
30
00
.41000
5210
6321
7532
8953
92195
1027159
. -
, .
-
3. 137
-
, . :
, -
9. - . -
? , , -
.
31. * , . . - , . -
, -
,
.
, Ab3bd (dAb3bAd Adb3bdA), -
.
. palin.in . - N , 3
-
138 3.
0-
-
-
-
10-
-
-
210-
-
1010-
21210
(S='abcdba3').
0-
-
-
-
-
-
10-
-
-
-
-
210-
-
-
-
3210-
-
-
21210-
-
123210-
2343210
,
. .
32. : - ( - ), .
.
( ), , - ( ). , . - , -
, ,
.
. input.txt N (l
-
3. 139
[1,], Array[l..MaxN+l, O.MaxK] Of Longlnt. - -
.
Procedure Solve;Var i , j : Integer;
sum: Longlnt;Begin
sum:=0;For i : = N DownTo 1 Do Begin
sum:=sum + Pr[i] ;For j:=l To Do Begin
A[i, j]:=A[i, j-1];If i+j3. N2*(N2+l)/2. , N*(N2+1 )/2. : - , -
1, 2, 3, ..., N*N - .
. 9! N=3 8 , 161=20922789888000 - 7040 -
-
140 3.
. JV=4.
( - ). ( , -) , , -
/=4. iV .
-
4.
, -
.
( ), , , . -
-
, ,
.
4.1. V
G(V,E). V - N . - , . , -
, ; , , , .
, , . ,
, . -
. -
, (, v) . , -
, , -
.
, () -.
. -
.
: ; ; . : .
N*N.
-
142 4.
i j I j
R *2. .
4.2.
4.2.1. . -
v. , v. . . -
q , - q (), q , . , - , . ,
, ,
: Nnew : Array[1..N] Of Boole-an.
.
-
. -
-
.
,
-
,
.
.
Procedure Pg(v:Integer);{* Nnew . *}
Var j:Integer;BeginNnew[v]:=False; Write(v:3);For j:=l To N Do If (A[v,j ]0) And Nnew[j]
Then Pg (j) ;End;
.
-
4. 143
FillChar(Nnew,SizeOf(Nnew),True);For i:=l To N Do If Nnew[i] Then Pg(i) ;
-
. -
: ; Nnew . - St, yk.
Procedure (v:Integer);Var St:Array[l..N] Of Integer;
yk, t, j:Integer;pp:Boolean;
BeginFillChar(St,SizeOf (St),0); yk:=0;Inc(yk);St[yk]:=v;Nnew[v]:=False;While yk0 Do Begin {* . *}t:=St[yk];{* " "
. *}j:=2;:=False;Repeat
If (A[t,j] 0) And Nnew[j] Then pp:=TrueElse Inc (j ) ;
Until pp Or (j>=N); {* ,
, . *}If pp Then Begin
Inc (yk);Nnew[j]:=False;{*
. *}End
Else Dec (yk); {*"" . *}
End;End;
4.2.2. . ( )
, , .
, -
. -
.
-
144 4.
. -
.
-
.
-
.
.
Procedure Pw(v:Integer);Var :Array[1. .N] Of 0..N;{*. *}
ykl,yk2:Integer;{^ ,ykl - ; yk2 - . *}j -.Integer;
BeginFillChar(Og,SizeOf(Og) ,0) ;ykl:=0;yk2:=0;{* . *}Inc(ykl);Og[ykl]:=v;Nnew[v]:=False;{*B
- v. *}While yk2
-
4. 145
, ). , - .
.
.
-
JV NN'2.. .
(). - -
. -
,
, , Tree (Ar-ray[1..2,1..N] Of Integer).
. ,
.
.
4.3.2. . G=. .
.
. .
-
, .
G : - . G G_. G
-
146 4.
G , G. , -
,
.
G - :
Turn (Array[1..N] Of Integer) (Down) (Up) ;
Nnew; , , Tree; numb. :
FillChar(Nnew,SizeOf(Nnew),True);FillChar(Tree,SizeOf(Tree),0);Nnew[l]:=False;Turn[1]:=1; Down:=1;Up:=2; {*B
. *}numb:=0;
Procedure Solve(v,q:Integer) ;{*v - , , q - ,
. *}Var j:Integer;Begin
If Down>=Up Then Exit;j = q vWhile (j
-
4. 147
If numb=N-l Then Begin ; Exit End;{* , v,.
,
. *}If j=N+l Then Begin Inc(Down);Solve(Turn[Down],1);Dec(Down);
End;End;
-
. -
-
- . , .
.
. - , , . - 8 .
-
148 4.
4.3.3. . .
. G=. .
. (Array [ 1..3,1.-N* (N-1) Div 2] Of Integer). -. Q=,
. -
.
1. Q, N - .
2. G .
3. -
, -
Q, - -
: -
Q.
8 .
-
4. 149
4. 3 , Q N-1. .
3?
(Mark:Array[l..N] Of Integer). -
(Mark[i]=i i 1 N). - , , ,
. -
. , ,
Mark .
1234
-
[1,2,3,4,5][1,2,3,1,5][1,2,3,1,1][1,2,2,1,1][1,1,1,1,1]
.
Procedure Chang_Mark(l,m:Integer);{* Mark. *}
Var i,t:Integer;Begin
If m
-
150 4.
While (i
-
4. 151
.
Procedure Tree; { * - , . *}Var SM,SP:Set Of 1..N;
min,i,j,l,k,t:Integer;Beginmin:=maxint; SM:=[1..N];SP:=[];{*
. . *}For i:=l To N-l DoFor j:=i+l To N DoIf (A[i,j]
-
152 4.
, -
. -
v , , v. R :
R(v) - G, -
v. F(v) - G, -
1. 2() (()), . . - 2 . :
, , -
. ( ).
, -
R.Procedure Reach; {* R,
. -
,
. *}Var S,T:Set Of 1. .N;
i,j,l:Integer;BeginFillChar (R,SizeOf (R) ,0) ;For i:=l To N Do Begin {^
i . *}T: = [i] ;Repeat
S:=T;For 1:=1 To N DoIf 1 In S Then{ * ,
S. *}For j:=1 To N DoIf A[l
rj]=l Then T:=T+[j];
Until S=T; { * , ,
i .*}
-
4. 153
F o r j :=1 N DoIf j In T Then R[i,j]:=l;
End;End;
Q -:
.
1. , (v,u) (u,t) (v,t). Ch=(VJ) ' ,
, Gz . -
G.2. R(v) , v, a Q(u)
, . -
, -
.
4.4.2. . G , -
G i j. G , , G , . ,
i j i /
Q(v) G -, .
, v Q v R, . . Q=R', R* , - R.
A, R Q :
-
154 4.
j i. G - G. - .
-
. .
-
(1)={1,2,5} G, -, 1. : R(3)={1,2,3,4,5,6,7},
,
G. G*=(V*,E*) : -
G, (*, j*) G* , G (i,j), , I - , i*, a. j -, j * . G* G. -. G* . i . , -
-
, , -
.
. ,
G. - G .
. -
. * G* G - G*, . , G : - G, * G*, G.
4.4.3. , . -
, . -
, ,
. ,
. , ,
-
4. 155
. -
, . -
,
.
. .
4, 5 7.
. t - , , -
t, , (, ) t.
-
. .
Gv G2, G3, G4 G5 -
1, 2, 3. t, - G
v
Gx G2,
1. - G2 , -
1. G2 - ,
1. . G
x -
G3, G4 G5. - -
. G4 G5 3 G5 . , . . , -
G4, 2 . ,
, ,
, ,
-
156 4.
.
,
-
,
.
, -
. .
-
, -
(), .
.
?
Num. Num (1,2,3,4,5,6,7,9,8). (v,u), , - , Num[v] Num[u], - v , . Num , Lowpg - ,
, -
.
Num. - (v,u) :Lowpg[v]:=Min(Lowpg[v],Num[u]) v, . -
v (v,u) , ,
v (Lowpg[v]:= Min(Lowpg[v],Lowpg[u])). Lowpg :(1,1,1,2,4,4,4,9,8). - . (,), -, Lowpgfu] Num[v]. , - v . v - ,
, .
, .
Procedure Dvy(v,p:Integer);{* p - v. A, Num, Lowpg - .*}
-
4. 157
Var :Integer;Begin
Inc(nm);Num[v]:=nm;Lowpg[v]:=Num[v];For u:=l To N DoIf A[v,u]0 ThenIf Num[u]=0 Then Begin
;Dvy (u,v) ;Lowpglv] :=!Min (Lowpg[v] ,Lowpg[u] ) ; { *,
. *}If Lowpg[u]>=Num[v] Then ;
EndElse
If (up) And (Num[v]>Num[u]) Then Begin {*u v. *}
;Lowpg[v]:=Min(Lowpg[v],Num[u]);
End;End;
:
FillChar(Num,SizeOf(Num),0);FillChar (Lowpg,SizeOf (Lowpg) ,0) ;nm:=0;For v:=l To N Do If Num[v]=0 Then Dvy(v,0) ;
G , - .
. -
,
G. (-) ) ?
4.5. 4.5.1.
,
.
. G , -
.
-
158 4.
,
, , , ,
, -
. G, . - .
, . -
( ) . , , , -
, -
.
, . ,
( ) . , . ,
, -
.
Procedure Search(v:Integer);{*: - , CV - ;yk - . *}
Var j:Integer;BeginFor j:=1 To N DoIf A[v,j]0 Then Begin
A[v,j] :=0;A[j,v] :=0;Search (j)
End;Inc (yk);Cv[yk]:=v;
End;
-
Cv Se-arch.
4.5.2. ,
,
. -
.
.
, .
G. , . -
(backtracking). -, , . ,
k .
-
4. 159
, . ,
, -
.
(k+1) . , -
, . -
. () , .
,
, .
Procedure Gm(:Integer); {* - . : - ; St -
; Nnew - : . *}Var j,v:Integer;Beginv:=St [k-1]; {* . *}For j:=1 To N DoIf (A[v,j]0) Then {*
v j . *}If (k=N+l) And (j=l) Then ElseIf Nnew[j] Then Begin {^
. *}St[k]:=j;Nnew[j]:=False;Gm(k+1) ;Nnew[j]:=True;End;
End;
-
160 4.
.
St[l] :=l;Nnew[l] :=False; Gm(2) ;
4.5.3. (V,T) G=
N-1 , N G. , , . . -, -
.
-
G . N1 , G - -N+l , G.
,
.
, -
.
,
. -
( ) - St, ( Gnum) , -. , / , - v, .
.
:=0;:=0;For j : = 2 N Do Gnum[j ] :=0;
-
4. 161
.
Procedure Circl(v:integer);{*: - ; St -
,
; - St; Gnum -
(num.) , . *}
Var j:Integer;Begin
Inc(yk);St[yk]:=v;Inc (num);Gnum[v]:=num;For j :=1 To N DoIf A[v,j]0 ThenIf Gnum[j]=0 Then Circl[j] {* j
.*}Else
If (jSt[yk-l]) And (Gnum[j]
-
162 4.
. 1 4 3- 2- 6.
-
.
.
s1 =5, ,
,
1 5 . -
.
-
.
, . . -
, .
: - . D[s,t]. , D[s,t], s . D Array[1..N] Of Integer. , - D . - . S H ( - v, D[t]=D[v]+A[v,t]. v (, ). , , D[v]=D[u]+A[u,v], , - s. t, v, , .,., s - .
Procedure Way(s,t:Integer);{*D, A - . St - . *}
Var v,u:Integer;Procedure Print; (* St.*}Begin
End;Begin
;;v:=t;While vOs Do Begin:=;
;
-
4. 163
v:=u;End;
End;, D .
, . . -
D. - . -
.
, . ,
D[vJ, , , D[u]+A[u,v]
-
164 4.
Procedure Dist; (*, D, s, N - . *}Var i,u: Integer;
:Set Of 1. .N;Begin
For i:=l To N Do D[i] :=A[s, i] ;D[s]:=0;T: = [l..N]-[s];While TO[ ] Do Begin:=;T:=T-[u];For i:=1 To N DoIf i In T Then D[i] :=min (D[i]
rD[u] +A[u, i] ) ;
End;End;
4.6.3. G= ,
.
() D s .
, i /.
.
-
:
Numln, Numln[i] ,
-
i; Num, Num[i]
i;
-
4. 165
St, , - . -
; , .
. i, Nu-mln ( ), St, - ( Num), - Numln , I. , St . - -
.
123456
Numln[2,2,2,1,0,1][2,2,1,0,0,1][1,2,0,0,0,1][0,2,0,0,0,0][0,1,0,0,0,0][0,0,0,0,0,0][0,0,0,0,0,0]
Num[0,0,0,0,0,0][0,0,0,0,1,0][0,0,0,2,1,0][0,0,3,2,1,0][0,0,3,2,1,4][5,0,3,2,1,4][5,6,3,2,1,4]
St[5][4][3]
[6,1][1][2][ ]
_ |
Nm0123456
Procedure Change_Num; {*A, Num . *}
Var Numln,St:Array[1..N] Of Integer;i,j,,nm,yk:Integer;
BeginFillChar(Numln,SizeOf(Numln),0);For i:=1 To N DoFor j:=1 To N Do
If A[i,j]0 Then Inc(NumIn[j]) ;nm:=0;yk:=0;For i:=2 To N Do
If Numln[i]=0 Then Begin Inc (yk)/Stack[yk]:=i;End;
While yk0 Do Beginu:=Stack[yk];Dec[yk];Inc(nm);Num[u]:=nm;
For i:=1 To N DoIf A[u,i]0 Then BeginDec(Numln [i]) ;If Numln[i]=0 Then Begin
Inc(yk); Stack[yk]:=i;
-
166 4.
End;End;
End;End;
, G ( ) - ( ) . L - ,
i. , . .
Procedure Dist;{*D, - . *}Var i,j:Integer;Begin
D[l]:=0;For i:=2 To N Do D[i] :=!-;{*, Maxlnt .*}
For i:=2 N DoFor j:=1 To i-1 DoIf A[j ,i]Ooo Then D[i] : =Min (D[i] ,D[j] +A[j ,i] ) ;
End; , -
A[i,j] . - . -
Num. . -
?
4.6.4. .
G= (-[1 ..N,1 ..N] Of Integer). - D .
.
[l..m]. :
. -
[l..(m+l)].
-
4. 167
,
: i (+1) (+1) .
Procedure Dist/ {*Ar D -
. *}Var m,i,j :Integer;Begin
For i:=1 To N DoForj:=l To N Do D[i,j] :=A[i,j] ;
For i : = 2 To N Do D[i,i] :=0;For m:=l To N DoFor i:=2 To N Do
For j:=l To N Do D[i,j] :=Min{D[i,j] ,D[i,m]+D[m,j]};
End; -
D -.
D ( m Dist).
D. - ,
D. M[i,j] - i .
Dist . -, D[i,j] D[i,m]+D[m,j], D[i,jJ, M[i,j]. M[i,j] M[m,j]. .
, 3- - 2-. [3,2] 1, [3,1]. . [3,4] 3-. , :
-
168 4.
Procedure All_Way (i,j:Integer);{* i j . *}
BeginIf M[i,j]=i Then If i=j Write(i) Else Write (i, '~',j)Else Begin All_Way (i ,M[i,j ] ) ;All_Way (M[i ,j ] ,j) ;
End;End;
4.7. V G,
, , -
.
4.7.1. G=(V,E). -
G, , , . .
. , S, - V, , S - S , .
.
(1, 2), (3, 4, 5),(4, 7), (5, 6) . - -
, -
,
. Q - -
G,
G, S*, ,
. a[G]=3, a S* (3, 4, 5).
,
, (). ,
. -
G G', G' G.
-
4. 169
G' - ( ), ( ) G. - G' 4, - (2, 5, 7, 8), G ( ).
.2.
. -
-
. -
(k )., ,
.
.
Ss (Array[1..N] Of Integer), k .
( Print). -
, .
,
.
. -
. : Type Sset=Set Of 1..N Var A:Array[l..N] Of Sset.
, , -
i, .
.
. Gg
-
170 4.
-
.
k. - -
Ggl , , () . -
(Qp) (Qm). Qp Qm ft- - . k ,, i, Qp Qm - . ,
(k+1) Qp Qm - , i, - ( ),, , I. , .
Procedure Find(:Integer; Qp,Qm:Sset);Var Gg:Sset;
i:Byte;BeginIf (Qp=[ ]) And (Qm=[ ]) Then Begin Print () ;
Exit;End;;
1:=1;While i
-
4. 171
.
Sset.Function Number(A:Sset):Byte;Var i, ant:Byte;Begin
cnt:=0;For i;=1 To N Do
If i In A Then Inc(cnt) ;Number:=cnt;
End; , -
.
.
12
Qp[1-5][4,5]
[ ][ ]
Gg[1..5][4,5]
Ss(1)
(1,4)
2, 3 ,
4,5
.
If Qm [] Then
Else Gg:~Qp; ,
, -
Qp, Qp. Find.
3
2
[ ]
[5]
[ ]
[4]
[ ]
[5]
(1,4)
(1,5)
Find 4 Qp Qm
While Find - k, 2. - 5. Find -.
3
2
[ ]
[ ]
[ ]
[4,5]
[ ]
[ ]
(1,5)
While , Find (/c=1)
-
172 4.
. : - i Qp Qm. : - Gg. , i, - , -
Qp A[i]. , .
Qp: =Qp- [i ] ;Qm: =Qm+ [i ] ;Gg:=Qp*A[i] ; Find,
k=l.
1
1234
32
12
[2..5]
[2..5][3,5]
[5][ ]
[ ][5]
[3..5][5]
[1]
[1][ ][ ][ ]
[5][3]
[1.2][2]
[1-5]
[2..3][3,5]
[5][ ]
[ ][ ]
[2,3]
(2)(2,3)
(2,3,5)
(3)
Gg
,
Find, Gm
Gm. 1. /, - Qm, , A[j] Qp , -
A[j] ( ). 2. Qm, , - Qp ? :
/77 QP . - . ,
.
Begindelt:=W+1;For j:=2 To N Do
-
4. 173
If j In Qm ThenIf Number (A[j] *Qp)
-
174 4.
-
-
. -
(3, 5, 12, 14).
.
4.7.4. . -
*. -
G - *, -
.
, , -
.
, ,
() , . -
, ,
, .
. 1. - * , . .
, ,
.
2. *, . : /1=(1, 6,
-
4. 175
7, 8), /2=(1, 2, 5, 8), 73=(2, 3, 5), /4=(3, 4), U^(2, 3, 4, 5),/6=(5, 6), /7=(6, 7), !78=(7,8). , -, 5- , , 4- , - 5-. .
4.7.5. , ,
, . ,
.
*. ,
,
.
*,
. 1, : Type Pr = Array [L.MaxN, l..MaxN+l] Of Integer; VarBl:Pr; MaxN . ( ), ( Press).
1 : [12 3 . . N 0], .
,
* . -
. (i) 1. Bl[i,j]0, Bl[i,j], - *,
*. 1 -, 1, 1 , (Press). , N-1 1. - 1 :
-
176 4.
*, -
. Price(Array[L.MaxN] Of Integer) [15 13 43 8 9 10]. - 1
*. 1 :
(Blocs).Procedure Blocs;{^ , 1 -
. *}Procedure Sort;Begin
End;Procedure Press(i,j:Integer);{*
i, () j, .*}
Var k:Integer;Begink:=j;While Bl [i,k]0 Do Begin { *
.
0. *}Bl[i,k]:=Bl[i,k+l];Inc (k) ;End;End;
Var i,j,cnt:Integer;Begin
-
4. 177
FillChar (Bl,SizeOf (B1) ,0) ;For i:=l N Do Bl[1,i]:=i;{*,
. *}For i:=l To N-l Do Begin
j:=1;cnt:=0;While Bl[i,j]0 Do BeginIf A*[i,Bl[i,j]]=0 Then Begin{*
. *}Inc (cnt) ;Bl[i+1,cnt]:=B1[i,j];{*
. *}Press (i,j);Dec(j) ;
End;Inc(j);
End;End;Sort;
End; - * (*[2,7]=0, *[4,7]=1). -
-
-
. Bl , ( ) . - ,
, .
. -
, -
, , -
( . , . -,
, . . ,
( ), - . ,
,
,
() . -, . ,
- . , .
-
178 4.
Type Model=Array [I..MaxN] Of Boolean;Var Sbetter:Model;Pbetter:Integer;{*
. *}S:Model;P:Integer;{* . *}R:Model;{*R[i]=True - ,
i "" . *} () k -
( ) :Procedure Include (:Integer); {*
. *}Var j .-Integer;BeginP:=P+Price[];{* . *}S[]:=True;{* . *}For j:=1 N DoIf A*[j,]=1 Then R[j]:=True; {*,
"" .*}Ends-Procedure Exclude(:Integer);{*
. *}Var j:Integer;Beginp:=p-Pri[];S[k]:=False;For j:=1 To N DoIf (A*[j,k]=l) And R[j] Then R[j