Вы находитесь на странице: 1из 341

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: lbz@aha.ru
-:
(812)247-93-01, e-mail: dialect@sndlct.ioffe.rssi.ru

ISBN 5-94774-010-9

. ., 2002
. , 2002

1.
1.1.
1.2.

7
9
9
21

2.

25

2.1.
2.2.
2.2.1.
2.2.2.
2.2.3.
2.2.4.
2.2.5. N

2.2.6.
2.2.7.
2.3.
3.

25
31
31
40
46
53

3.1. ( )
3.2.

3.3.
3.4.

3.5.
3.6.
3.7.
4.
4.1.
4.2.
4.2.1.
4.2.2.
4.3.

58
61
65
68
79
79
81
96
98
105
109
113
141
141
142
142
143
144

4.3.1. .
4.3.2.
4.3.3. . . . .
4.3.4. . .
4.4.
4.4.1.
4.4.2.
4.4.3.
4.5.
4.5.1.
4.5.2.
4.5.3.
4.6.
4.6.2.
4.6.3.
4.6.4. .

4.7.
4.7.1.
4.7.2.

4.7.3.
4.7.4.
4.7.5. . .
4.8.
4.8.1.
4.8.2. . . . .
4.8.3.

4.9. ,
4.9.1.
4.9.2. . . .
4.9.3. . . .
4.10.

4.10.1.
4.10.2.
4.10.3.
4.11.
5.
5.1.

144
145
148
150
151
151
153
154
157
157
158
160
161
163
164
166
168
168
169
173
174
175
180
180
181
185
186
186
187
192
195
195
197
200
202
222
222

5.2.
5.3.
5.4.
5.5.
5.6.
5.7.

227
232
236
241
251
259

6.

7.
7.1.
7.2.
7.3.
( )

266
318
319
320
328
340

.
. : , .

. , .
, , - ,
, - . . . - .
, . . ,
,
.
? -,
.
-, , . ,
,
,
ACM (Association for Computing Machinery).
,
, . -, . - .
1993 .
. ,

. . ,
,
.
, ,
, , , .
. . ,
, .
.

, - . .
, . 1994 .
, . 5,
.
, .
.
. , ,
.
. , ,
okulov@vspu.kirov.ru.

1.

, ,
, . ,
() , .

,
30!= 265252859812191058636308480000000?

.

1.1.
, , .
.
.
:

8000 3084 8636 9105 8121

2859 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]

[1]

[2]

[3]

ch

674

851

23

1-

23

2-

238

3-

385

4-

851

23

5-

516

238

6-

167

385

7-

674

851

23

, [0] () .
i i+1,
[1].
()

. . ,
A[i] A[i+1], . .
:
For i:=A[0] DownTo 1 Do Begin
A[i+1] :=A[i+l] + (LongInt (A[i] ) *10) Div Osn;
A[i] : = (LongInt (A[i] ) *10) Mod Osn;
End;

11

1.

23851674 6 . ch
7. 7 [1]. . .
i

[1]

2
2
1

516
516
160

[2]
238
380
385

[3]
0

ch
7

2
2


[1] [0].
:
Procedure
ReadLong(Var
A:TLong);
Var
ch:Char;i:Integer;
Begin
FillChar (A,SizeOf(A) ,0) ;
Repeat
Read (ch) ;
Until ch In ['0'..'9'] {*
. *}
While ch In ['0'..'9'] Do Begin
For 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 l s , s : S t r i n g ;
i: Integer;
Begin
Str(Osn Div 10,ls);
Write(A[A[0]]);{* . *}
For i:=A[0]-1 DownTo 1 Do Begin
Str(A[i],s)
;
While Length (s)<Length (ls) Do s:='0'+s;
{*' . *}
Write (s) ;
End;
WriteLn;
End;

. , . , ,
.
. SumLongTwo.
:
Var ,,: TLong;
Begin
Assign(Input,'Input.
txt');
ReadLong (A) ;ReadLong (B) ;
Close (Input);
SumLongTwo (A,B,C);
Assign
(Output,'Output.txt');
WriteLong(C);
Close (Output) ;
End.

Reset

(Input);

Rewrite

(Output);

? . =870613029451, B=3475912100517461.

1.
i
1
2
3
4

A[i]
9451
1302
8706
0

B[i]
7461
51
9121
3475

[1]
6912
6912
6912
6912

13

[2]
1
1354
1354
1354

[3]
0
0
7827
7827

[4]
0
0
1
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<=A[0]) And (A[i]=B[i]) Do Inc(i);
Eq:=(i=A[0]+l) ;
End;
End;

> .
Function More (,: TLong): Boolean;
Var
i:Integer;
Begin
If A[0]<B[0] Then More:=False

14

1.

Else If A[0]>B[0] Then More:=True


Else 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 Else
More:=False;
End;
End;

Eq More.
Function
Less(A,B:TLong):Boolean;{A<B}
Begin
Less:=Not(More(A,B)
Or Eq(A,B));
End;
Function More_Eq (A,B: TLong): Boolean;
Begin
More_Eq:=More(A,B)
Or Eq(A,B);
End;
, =<.
Function
Less_Eq(,:TLong):Boolean;
Begin
Less_Eq:=Not(More(,));
End;
. , 0, , 1,
2 .
. ? .
56784, B 634. 2
, ,
. 567000, 567 3,
, .
.
Function More(Const , : TLong; sdvig: Integer):
Byte;
Var i: Integer;
Begin
If A[0]>(B[0]+sdvig) Then More:=0
Else If A[0]< (B[0]+sdvig) Then More:=1
Else Begin
i:=A[0];

1.

1J5

While (i>sdvig) And (A [i] =B [i-sdvig] ) Do


Dec (i) ;
If i=sdvig Then Begin
More:=0;{*
. *}
For i : = 1 To sdvig Do If A[i]>0 Then Exit;
More:=2;{* , ""
.*}
End
Else
More:=Byte(A[i]<B[i-sdvig]);
End;
End;

. . , .
.
Procedure Mul(Const A: TLong;Const : LongInt;Var
: TLong);
Var i:
Integer;{*
-
.*}
Begin
FillChar(C,
SizeOf(C),
0);
If K=0 Then Inc(C[0]){* .*}
Else Begin
For i:=1 To A[0] Do Begin
C[i+1]:=(LongInt
(A[i] )*K+C[i] ) DivOsn;
[i]:=(LongInt(A[i])*K+C[i])
Mod Osn;
End;
If C[A[0]+1]>0 Then C[0]:=A[0]+1
Else
[0]:=A[0];{*

.*}
End;
End;
, .
Procedure MulLong(Const ,: TLong; Var :
{* "" "".*}
Var
i, j : Word;
dv:
Longlnt;
Begin
FillChar(Cr SizeOf(C),
0) ;
For i:=1 To A[0] Do

TLong);

16

1.
For j : = 1 B[0] Do Begin
dv:=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, .*}
Begin
For i : = 1 To B[0] Do Begin Dec (A[i+sp], B[i]) ;
{* *}
{*}
j:=i;
{*}
While (A[j+sp]<0) And (j<=A[0]) Do Begin
{*}
Inc(A[j+sp],
Osn); Dec(A[j+sp+1] ); Inc(j) ; {*}
End;
{*}
End;
i:A[0];
While (i>1) And (A[i]=0) Do Dec (i) ;
A[0] :=i;
(*

*)
End;
, {*},
:
{* *}

1.
If A[i+sp]<0 Then Begin Inc (A[i+sp],
Dec (A[i+sp+1]);End;

17
Osn);

, ,
. .
, , .
100000001000000000000, 2000073859998.
. , . .
. ( ) . :
Procedure LongDivLong(Const ,:
TLong;
Var Res,
Ost:
TLong);
Begin
FillChar(Res,
SizeOf(Res),
0);
Res[0]:=1;
FillChar (Ost,
SizeOf(Ost),
0) ;
Ost[0]:=1;
Case More (A, B,0) Of
0: MakeDel (A,B,Res,Ost);
{*A ,
, , "" . *}
1:Ost:=A; { *A . *}
2:Res[1]:=1;
{* . *}
End;
End;
? . . :

? (1,
3, 5 . .), ,
, .... ?
, .
? . -

18

1.

, ,
. B*10,
( ) . , 564, 63
. , , .
Down , Up , Ost .
Down
0
5
7
8
8

C=B*((Down+Up) Div2)
315
441
504
567
504

Up
10
10
10
10
9

Ost=564
C<Ost
C<Ost
C<Ost
C>Ost
C<Ost

, (Up+Down)
Div 2, Ost
. (Down) , () , (Up), . .
27856, 354. 10, 10000.
Down
0
0
0
0
0
0
0
78
78
78
78
78
78
78

Up
10000
5000
2500
1250
625
312
156
156
117
97
87
82
80
79

1770000
885000
442500
221250
110448
55224
27612
41418
34338
30798
29028
28320
27966
27612

Ost=27856
C>Ost
C>Ost
C>Ost
C>Ost
C>Ost
C>Ost
C<Ost
C>Ost
C>Ost
C>Ost
C>Ost
C>Ost
C>Ost
C<Ost

78, 27856
27612, . . 244.
. :
(More)
(Mul) .

1.

19

Function FindBin (Var Ost: TLong; Const : TLong;


sp:
Integer) :
LongInt;
Var Down, Up: Word;
C: TLong;
Begin
Down:=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 E q ( C o n s t , : T L o n g ) : 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: T L o n g ) : B o o l e a n ;
F u n c t i o n M o r e ( 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 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: T L o n g ) : 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
sp:Integer);
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=49<57,
8*8=64>57. 7.
3. 57
2 =49: 57-49=8. , 845.
4. .
, , (2**10+)* 845
845. 5,
(14*10+5)*5=725<845, (14*10+6)*6=876>845. ,
5 y=75.
5. 3. : 845-725=120,
12064.
6. 4.
(2*y*10+)*<=12064. 8,
(150*10+8)*8=12064. , =758.
4, , , .
10*+. 100*2+10**+10**+2=z.
2, 100*2 z. z-100* 2 .

3, 4,
, 00.

.
5. , , 9?
6. .
7. .
8. , .
9. .
10. ,
2 1 9 9 3 6 *(2 1 9 9 3 7 -1) , . .
, .

1.

23

11.
2, 8, 16.
12.
.
13. , () 2
().
b
:
b ,
b ,
, b ,
, b ,
,


:
Function
Sum (,: Integer) : Integer;
Begin
If x=0 Then Sum:=y
Else
If y=0 Then Sum:=x
Else If (x Mod 2 =0) And (y Mod 2=0) Then
Sum:=Sum(x Div 2, Div 2)*2
Else If (x Mod 2 =1) And (y Mod 2=1)
Then Sum:=Sum(x Div 2,y Div 2+1)*2
Else Sum:=Sum(x Div 2, Div 2)*2+1;
End;
(
) b. 2
.
14. b ,
. ,
b=125, =37.
:
125*37
250*18+125

24

1.

500*9+125
1000*4+500+125
2000*2+500+125
4000*1+500+125.
: 125*37=125+500+4000=4625 (
b ?). :
,
,

:
Function
RecMul (x,:Integer): Integer ;
{* ,
Integer.*}
Begin
If =0 Then RecMul:=0
Else If x Mod 2=0 Then RecMul:=RecMul (x Div
2,y)*2
Else 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 ,

-1

F F , ,
-1

FF =E. ,

1 N.

2.

27

.

. , F
: F=[1, 3], [2, 5, 4, 6].
<fi,f>, i<j F,
fi>fj.. (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 ? : 2 1 6 =65536.

. . ,
, . .
. k . Nx
, 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. ,

:
1
2
3
4
5
6

1
2
6
24
120
720

32

2.

7
8
9
10
11
12
13

5040 ( Integer)
40320
362880
3628800
39916800
479001600 ( Longlnt)
6227020800

30

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<2 , t>=1, P1[t]=<P2[t] P1[i]=P2[i] i<t.
.
. 1<2 ,
1
2
l
2
t=<N, [t]> [t] P [i]=P [i]
i>t.
(N=3).
()
123
132

()
123
213

33

2.

()
213
231
312
321

()
132
312
231
321

(N=4). , ,
, .

1234

1234

2134

1243

3124

1342

4123

2341

1243

2134

2143

2143

3142

3142

4132

3241

1324

1324

2314

1423

3214

1432

4213

2431

1342

3124

2341

4123

3241

4132

4231

4231

1423

2314

2413

2413

3412

3412

4312

3421

1432

3214

2431

4213

3421

4312

4321

4321

. 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] <P [i-1] ) DoDec(i);
{* "".*}
j:=N;
While P[j]<P[i-1] Do Dec(j);
{*
,

P[i-1].*}

Swap (P[i-1], P[j]) ;


For j:=0 To (N-i+1) Div 2 - 1 Do Swap (P[i+j] ,
P[N-j]) ; {* ""
.*}
End;
Procedure
Swap (Var a,b:Integer);
Var
t:Integer;
Begin
t:=a; a:=b; b:=t;
End;

:
Init;{*:
-
, Last - .*}
Print;{*
.*}
While Not(Eq(P,Last)) Do Begin {* Eq

; ,
*}
GetNext;
Print;
End;

.
1 N , .
:
p1 p2, ..., pN 1 , 2 ,
..., yN. i 1 N s, i , . . s, ps=i,
, i 1, 2, ..., ps-1,
yi.

35

2.

:
i

ti
yi

11
0

2
8
1

3
5
1

4
1
1

5
7
0

6
4
3

7
10
2

8
9
0

9
6
5

10
3
5

11
2
0

, 0=<yi<i. N!,
. . .
, , . . (pi,pj), , i<j pi>pj,
.
, ,
, (1, 2, 3, ..., N). . ,
.
. 0111.
,
; *4**. *43*. , . . 1432.
. 0023.
(3+1),
(2+1) **34, (0+1) 2134. 0112.
(2+1), (1+1),
(1+1), 1342. ?
N-4.
0000

4321

0011

2431

0123

1234

0112

0001

3421

0010

0002

3241

0020

0003

3214

0013

2314

0012

2341

1342

4231

0122

1243

0113

1324

4213

0121

1423

0103

3124

0021

2413

0120

4123

0102

3142

0022

2143

0110

4132

0101

3412

0023

2134

0111

1432

0100

4312

.
. i i.
. ().

2.

36


. ,
.
,
, .
N=4, 0023.
.

0023
2134

0123
1234

0122
1243

0121
1423

,

. 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;
Begin
For i : = 1 To N Do Begin
P[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;
Begin
i:=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;
:
Begin
First;
:=True;
While pp Do Begin
Solve (pp) ;
If pp Then Print;
End;
End;
.
Procedure Solve(Var q:Boolean);
Var i, j, dj:Integer;
Begin
i:=Ok; q:=(i>l); {*Ha ,
. *}
If i>1 Then Begin
Y[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;
Begin
j:=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=<12, ,
L LongInt. .
Const Nsmall=12;
ResSm: Array[0..Nsmall] Of LongInt
=(1,1,2,6,24,120, 720,5040,40320,362880,3628800,3991
6800,479001600) ; {*3 N! N 0 12.*}

2.

39

.
Procedure
GetPByNum(L:LongInt);
Var Ws: Set Of Byte;
i,j,sc:
Integer;
Begin
Ws:=[]; {* ,
.*}
For i : = 1 N Do Begin
Sc:=L Div ResSm[N-i]; L:=L Mod ResSm[N-i]; j:=1;
While (sc<>0) Or (j In Ws) Do Begin
If Not (j In Ws) Then Dec(sc);
Inc (j) ;
End;
Ws:=Ws+[j]; {* . *}
P[i]:=j;
End;
End;
.
I
1

...

...

7
6
5
4
3
2
1
0
2
1
0
2
1
1
0
2
1
1
1
0

Sc

37021
1741

********
1
2
3
4
5
6
7
8
1
2
3
1
2
3
4

301

61

13

3
...

[]

[8]

8********

[3,8]

83******

[3,4,8]

834******

2
4
5

...

Ws

[3,4,5,8]
8345****
...
...

40

2.

. , . .
N 8 53871462. :
7!*< 1- ,
5, 4>
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;
Begin
ws:=[]; {* .*} sc:=1;
{*
.*}
For i : = 1 To N Do Begin
j:=1; sq:=0; {* .*}
While j<P[i] Do Begin
If Not (j In ws) Then Inc (sq);
Inc (j);
End;
ws:=ws+[P[i]]; {* P[i] .*}
sc:=sc+ResSm[N-i]*sq; {*
.*}
End;
GetNumByP:=s;
End;

2.2.2.
. N .

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:=1
Else Plac:=(n-m+1)*Plac (n,m-1);
End;
N=<12. N .
.
N .
. N=4, = 3 .
.
123
124
132
134

142
143
213
214

231
234
241
243

312
314
321
324

341
342
412
413

421
423
431
432

:
Program GPlac;
Const n=4; =3; {*
.*}
Var
A:Array[1..m]
Of Integer; {*
.*}
S:Set Of Byte; {*
.*}
Procedure Solve (t:Integer); {*
t
.*}
Var i:Byte;
Begin
For i:=1 To n Do {*
1-
.*}
If Not (i In S) Then Begin

42

2.

S:=S+[i]; {*
.*}
A[t] :=i; {* .*}
If t<m Then Solve (t+1) Else Print; {*
,
,
.*}
S: =S- [i]; {*
.*}
End;
End;
.
Begin

S: = [];

Solve (1) ;

End.

.
.
. .

1 N,
( ). . N=5, =3
1 3 4. 2 5.
,
(1 3 4 5
2), (1 3 5 2 4)
(1 3 5). .
, .
:

() , , .
, ,
.
, ,

.
, .
Const n=4; =3;
Var A:Array[1 . .] Of Byte;

2.

43

Procedure GetNext;
Var i,j,k,q:Integer;
Free:Array[1..n] Of Boolean; {*

.*}
Function FreeNext(t:Byte):Byte; {*
.*}
Begin
While (t<=n) And Not (Free [t]) Do Inc(t);
{* .*}
If t>n Then FreeNext:=0 {*
, .*}
Else FreeNext:=t; {*
.*}
End;
Begin
For 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<=n) And Not(Free[k]) D {*
.*}

44

2.

If k>= Then k:=1 Else Inc(k);


A[j]:=k; {*
.*}
Free[k]:=False;
{* .*}
End;
End;
. N L
( ).
5 3 , 60. , , .
0.
0
1
2

18

241

24

312

13

213
214

19

243

25

314

30 341
31 342

145

14

215

20

245

26

315

32

123

142

12

124

143

125

345

132

152

15

251

27

321

33

351

134

10

153

16

231
234

21

4
5

22

253

28

324

34

352

135

11

154

17

235

23

254

29

325 ...

(
) 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

Begin
For 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 Do
If 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- .
,
.
,
1234

1246

1356

2356

1235

1256

1456

2456

1236

1345

2345

3456

1245

1346

2346

. 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;
Begin
FillChar (SmallSc,SizeOf(SmallSc),0);
For i:=0 To N Do SmallSc [i,0]:=1;
For i:=1 To N Do
For j:=1 To k Do
If SmallSc[i-1,j]*1. 0+SmallSc[i-1,j-1]>
MaxLonglnt Then SmallSc[i,j]:=MaxLongInt
Else 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.

Begin
FillChar (A,SizeOf (A),0) ;
FillChar(BrSizeOf(B),0)
;
A[0]:=1;A[1]=1;
For i:=1 To N Do Begin
B[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

12345

12457

14

14567

12346

12467

15

23456

12347

12567

16

23457

12356

10

13456

17

23467

12357

11

13457

18

23567

12367

12

13467

19

24567

12456

13

13567

20

34567

1, 2, ..., k, N-k+1,
N-k+2, ..., N. . ,
. , ( ) , .
Procedure GetNext;{*,
( )
.*}
Var
i,j:Integer;
Begin
i:=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.*}

2.

50

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. ,


.

51

2.

.
. ,
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 Do
For 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).


i
1

j
1

2
3
4

2, j
4 3, j
5
6
8

SC

SmallSc[N-j,k-i]

SC

1
71

70
-

71
-

71
71

75

75

3
1

78
79

SmallSc[N-j,k-i]

SC

15
-

16
-

18

78

(N=7, k=5, =023467).


i
1
2
3
4
5

j
1
3 2, j
4 3, j
5
7 6, j

SC

1
16
16
16
18

, 3 4 . 3 0,
4 1.

52

2.

. 0 1.
() N 0 1,
[i]=1 , (N-i+1) (). N=7, k=5. , , .
0
1
2
3
4
5
6

0011111

12345

0101111

12346

0110111

12356

0111011

12456

0111101

13456

0111110

23456

1001111

12347

7
8
9
10
11
12
13

1010111

12357

1011011

12457

1011101

13457

1011110

23457

1100111

12367

1101011

12467

1101101

13467

14 1101110
15 1110011
16 1110101
17 1110110
18 1111001
19 1111010
20 1111100

23467
12567
13567
23567
14567
24567
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; L a s t [ i ] : = 1 ; End; {*
.*}
While Not Eq(Curr,Last) Do Begin
i:=n-1;
While (i>0) And Not ((Curr [i]=0) And
(Curr [i + 1] =1) ) Do Dec (i); {* 0 - 1 ,
,

.*}
Num:=0;

53

2.

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

11111111
2111111

3311
332
41111

17
18

521

19

611

3
4
5

221111

9
10
11

22211
2222

12
13

4211
422

20
21

62
71

311111

14

431

22

7
8

32111
3221

15
16

44
5111

53

N k
P(N,k), P(N). ,
P(N) P(N,k) k. ,
(0)=(1)=1. .

2.

54

. N k
N , .

. .

, , . P(i,j)
P(i-j,k) k, k 0 j. , j i
i-j k .
.
0

0
1

1
1

2
3

0
0

1
1

.3

0
1

0
1
1

5
6

1
1

7
11
15

22

, (8,3)=(5,0)+(5,1)+(5,2)+(5,3).
.
Const MaxN=100;
Var N:LongInt;
SmallSc:Array[0..MaxN,
Procedure
FillSmallSc;
Var i, j , k:Integer;
Begin

0..MaxN] Of LongInt;

FillChar(SmallSc,SizeOf(SmallSc),0);
SmallSc[0r0]:=1;
For i:=1 To N Do
For 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;
Begin
Res:=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 Begin
sc:=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;
Begin
sc:=N; i:=1;
While sc<>0 Do Begin j:=1;
While L>=SmallSc[sc,j] Do Begin
Dec(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;

2.

58

GetNumByWh:=sc;
End;
(N=8, Now 22211).
SC

jk

SC

0
1

jk

1
2

, , 4.
2.2.5. N

. .
N=4. 8.
0000

0010

0101

1001

0001

0100

1000

1010

( P(N))? N.
.
P(N1). , . 0. P(N-2). , : P(N)=P(N-1)+P(N-2), a
, . , N,
1, N- . LongInt (N=<44). N
,
.
.
ConstNSmall=44;
ResSm: Array[0..NSmall] Of Longlnt=(1,2,3,5,8,
13,21,34,55,89,144,233,377,610,987,

59

2.

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:=MaxLongInt
Else GetSmall:=ResSm[N] ;
End;
. . =6 ( , ).
0
1
2
3
4
5
6

000000
000001
000010
000100
000101
001000
001001

7
8
9
10
11
12
13

001010
010000
010001
010010
010100
010101
100000

14
15
16
17
18
19
20

100001
100010
100100
100101
101000
101001
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 Begin
Now[i] :=0;Dec(i) ;
End;
Now[i]:=2/
End;

2.

60

. . . L , N1,
() 0
1? : 1. !- N1 L. .
N=6, =15.
/

Now (******)

15

13

-j * * * * *

___2_|

10****

100***

1000**

10001*

'

100010

Procedure GetWhByNum(L: Longlnt);


Var
i: Integer;
Begin
For i:=1 To N Do
If L>=GetSmall (N-i) Then Begin Now[i]:=1;
L:=L-GetSmall(N-i)/End
Else Now[i]:=0;
End;

. . i 1,

N-i. , 1 .
Function GetNumByWh: Longlnt;
Var
i: Integer;
sc:
Longlnt;
Begin
sc:=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 Begin
If 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 .


()

(Now)

0000

0000

[]

0001

0001

0010

0011

[1]
[1,2]

0011

0010

0100

0110

[2]
[2,3]

0101

0111

[1,2,3]

0110

0101

[1,3]

0111

0100

1000

1100

[3]
[3,4]

1001

1101

[1,3,4]

10

1010

1111

[1,2,3,4]

11

1011

1110

[2,3,4]

12

1100

1010

[2,4]

13

1101

1011

[1,2,4]

14

1110

1001

[1,4]

15

1111

1000

[4]

4
0 2 - 1 . ( ),
, , 2, , Now. . .,
Now[l]:~B[l]
i 2 N.
, Now.
, . Now
.
Procedure
TransferB;
Var i:
Integer;
Begin
FillChar (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.
TransferToB;
Integer;

Procedure
Var i , j :
Begin
FillChar(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
.

2.

64

0100:

( 1100):

( 1000):)

.
L L
. L .
Procedure GetWhByNum (L: Longlnt);
Var i: Integer;
Begin
For i:=l To N Do Begin
B[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<i<2*N), , (, )
.
. (())()() (JV=4),
(()))(() .
- . . .
N 4.
,
.
0
1
2
3
4
5
6

0000
00(0)
0(0)0
0(00)
()((()))
(0)00
(())(())

7
8
9
10
11
12
13

(00)0
(000)
(()(()))
((()))()
((())())
((()()))
((()))

?
? N? ,
.

Now.
)(. , , , .
(). (),
, ,
2*N, ().
Procedure
GetNext;
Var i, j, sc: Integer;
Begin
i:=N*2;
While (i>l) And (Now[i-1] + Now[i]<>') (')
Do Dec(i);{* ") (".*}
3 3500

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) Else
Dec (sc) ;
While sc<>0 Do Begin{"
. *}
Now:=Now+r)';Dec(sc)
;
End;
While Length (Now) <2*N Do Now:=Now+' () ' ;
{" , ""
, ..

.*}
End;
2 , ()()(())
()(()()) ? ,
. i, P1[i]oP2[i].
, P1[i]=')', a P2[i]='(', , 2. , , . .
. , , ,
. , ((((( , (()))(( , 5 .
. ,
, ScSmall, ScSmall[i,j]
i ,
/.
:
. 1, , ,
. ScSmall[i,j] 0, I j
.

67

2.

. 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].
-1

14

14

5
0

14

28

20

Longlnt.
Const SmallN=37;
Var ScSmall: Array[-1. .SmallN + 1, -1. .SmallN + 1]
Of Longlnt;
.
Procedure
FillSmallSc;
Var i , j : Integer;
Begin
FillChar (ScSmall, SizeOf(ScSmall), 0) ;
ScSmall[0,
0]:=l;
For i:=l To SmallN-1 Do
For 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<0) Or (Up<0) Then GetSmallSc:=0
Else

68

2.

If (N>SmallN) Or (Up>SmallN) Then GetSmallSc


:=MaxLongInt
Else GetSmallSc:=ScSmall[N, Up];
End;
.
. Up, , .
Procedure GetWhByNumfL: Longlnt);
Var i, Up: Integer;
P:
Longlnt;
Begin
Now:=rr;
Up:=0;
For i:=l To N*2 Do Begin
P:=GetSmallSc(N*2-i,
Up-1);
If (L>=P) Then Begin Now:=Now+'('; Inc (Up);
L:=L-P; End
Else Begin Dec (Up); Now:=Now+')'; End;
End;
End;
Function GetNumByWh: Longlnt;
Var
sc: Longlnt;
i, Up: Integer;
Begin
sc:=l; Up:=0;
For i:=l To N*2 Do
If Now[i]='(' Then Begin sc:=sc+GetSmallSc
(N*2-i,Up-l) ;Inc (Up) ;End
Else 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;<
A>;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) Then
If i<m-l Then Place:=i

70

2.

Else Place:=m-2
Else
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
Else
For 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;
Begin
For 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;
Begin
For 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 Begin
j:=j Div 2;p:=p+l;
End;
If p<=n Then A[p] :=1-A[p] ;
Until p>n;
End.
12. [18] .
Const n=5;

^;
Var A:Array 11. .]
i,j,p:Integer;

Of

Integer;

71

2.

Begin
For i:-l To Do A[i] :=i;
p:=k;
While p>=l Do Begin
For i:=l To Do Write (A [i] : 3) ; WriteLn;
If A[k]=n Then p:=p-l Else p:=k;
If p>=l Then
For i:=k DownTo p Do A[i] :=A[p] +i-p+l ;
End;
End.
13. . , :
( ) ().
,
. ,
.
,
N (1<7V<1OO).
. (put.txt) N.
. (output.txt) .
. 1,
0. 0 1.
0 1, 1 . N 1.
N=4. 8.
0000
0001

0010
0100

0101
1000

1001
1010

( P(N'))? N.
0.
P(N1). ,
1.
0.

72

2.

P(N-2). , : P(N)=P(N-l)+P(N-2), a
, .
, N, 1, iV- .
Longlnt
N<44.
N
, . - , . 2N
.
14. 1 N (1<N<13) . , 3, 4, 6,
2, 5, 1. : k. k
, . () t, , t>k, k
, t
, ( ) . .,
(k ) . .*
,
. ,
, 16 .
. .
,
. . (N=6) 1, 2,
3, 4, 5, 6. : 1 2 3 4 5 6.
2001 . ( ).

2.

73

: 1,
2 . . : , ( !) ,
, , . , .** ,
. ,
. 3 2 1 6 4
5. ,
()
- .

, . , ,
, . . .
, .
, , (
) ,
, .
Const NMax=13;
Type MyArray=Array[l..NMax] Of Word;
Var A:MyArray;
Function Check (Const P-.MyArray) :Boolean;
{* -

?*}
Begin
End;
Procedure SolverType 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;
Begin
New (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 {*

,
. *}

A
New (); .Data:=Q;p .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, .
. input.txt N (l<iV<200),
(0<-<(-1)/2)
, .
. output.txt N,
.
. ( ), s. N-s
. , .
16. a1,...,aN +, , * ,
.
. input.txt (-210 9 <<210 9 ), N -

76

2.


(1<<12), av...,aN
(0<.<20)
. output.txt 0,
. ,
.
. N1. ,
+, , *, 3N~*.
,
, .
. . ( ) +, , *,
.
Function GetRes: Comp;
Var tm: Comp;
p:Integer;
Begin
P-.-1;
tm:=GetMul(p);
While (p<N) And (B[p] In [' + ', '-']) Do Begin
Inc (p) ;
Case B[p-1] Of
' + ' : tm:=tm+GetMul (p);
'-':
tm:=tm-GetMul(p);
End;
End;
GetRes:=tm;
End;
, .
Function GetMul(Var p: Integer): Comp;
Var tm: Comp;
Begin
tm:=A[p] ;
While (p<N) And (B[p] = '*r) Do Begin

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;
Begin
If k=2*N+l Then If Sum=0 Then Rec:=l Else Rec:=0
Else Begin
Ans:=0;
For i:=0 To 9 Do
If k<=N Then Ans:=Ans+Rec(k+1,Sum+i)
Else If i<=Sum Then Ans:=Ans+Rec(k+1rSum-i);
Rec:=Ans;
End;
End;
. -
, Rec(3,8)
iV=2. M:Array[1..2O,O..9O] Of .
1.
, - ,
. .

78

2.

17. N . , (
. . ).
. 5.
. (, )
, , . , N , C3N, O(7V4).

3.

, *. ,
.
,
, .
, : , : (
) (); , ,
. . , , .

3.1. ( )
N Uv U2, ..., UN (N ), =(1, 2, ..., aN),
.

, , ,
,
.

80

3.

. ,
-1 , A={av 2, ..., _1, ?, ..., ?),

<Sfe<>[ ] ( ),
ak Sk
+1 . , Sk ,
-1 , ak_t _1 Sk_v
. , _
Sk,
. a,k_v
_2
.
.
( ) .
, , , fc-ro
, at, a2, ....
ak_1 , .
, , ,
- . , .
.
Procedure
Backtrack (<,i>);
Begin
If < > Then >
Else Begin
< Si>;
For <aeSi>Do Backtrack (<\\ >,i+1) ;
{*\\ - . *}
End;
End;
. .
, 2V, I U^ *| U2\ *...*! UN\ .
Ut ,
N
C .

3.

81

3.2.

1 .
. N*N
N , .
.
^2 ( 4,4*10 9 N=8). , NN (1,7*10 7 iV=8).
, , (at, a2, ..., aN)
, (1,2,
..., N), N\ (4,0*10 4 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). *}
Begin
D_hod:=Vr[j] And Up[i+j] And Down [i-j] ;
End;

:
Procedure
Solve(i:Integer;Var
q:Boolean);
Var
j:Integer;

3.

83

Begin
j:=0;
Repeat
Inc (j);q:=False;{* . *}
If D_hod (i,j) Then Begin
Hod(i,j);
If i<8 Then Begin
Solve (i+1 ,q) ;
If Not q Then O_hod(i,j) ;
End
Else q:=True;{* .*}
End;
Until q Or (j=8) ;
End;

.
N*N. 8*8 92.
.
Procedure
Solve(i:Integer);
Var
j:Integer;
Begin
If i<=N Then Begin
For j : =1 To N Do
If D_hod(i,j) Then Begin
Hod(i,j) ;
Solve (i+1) ;
O_hod(i,j) ;
End;
End
Else Begin
Inc (S);{* ,
. *}
Print;{* . *}
End;
End;

. . 8*8 12. .
90, 180 270 , (

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 Begin
r:=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;
Begin
For i:=l To N Do Y[X[i]J:=i;
X:=Y;
End;
Function Cmp (X,Y:TArray) -.Boolean;
Var
i:Integer;
Begin
i : =1 ;
While (i<=N) And (Y[i]=X[i]) Do Inc(i);
If i>N Then Cmp:=False
Else If Y[i]<X[i] Then Cmp:=True Else Cmp:=False;
End;
Procedure
Solve(i:Integer);
Var j :Integer;f-.Boolean;
Y: TArray;
Begin
If i<=N Then Begin
For j:=1 To N Do
If D_hod(i,j) Then Begin
Hod(irj) ;
Solve (i+1) ;
O_hod(i,j) ;
End;
End
Else Begin
f:=True;
For j:=0 To 7 Do Begin
Y:=X;

If j And 1 =0 Then Siml (Y) ;


If j And 2 =0 Then Sim2 (Y) ;
If j And 4 =0 Then Sim3 (Y) ;
If Cmp(YrX) Then f:=False;
End;
If f Then Begin
Inc(S);{* ,
. *}
Print-; {* . *}
End;
End;
End;

86

3.


-. . , - ,
.
: .
Program Ferz;
Uses Crt;
Const N=8;
Var B:Array[1. .N] Of IntegersProcedure Rf (i:Integer) ;
Var
j,k,p,t:Integer;
Begin
For j:=l To N Do Begin
B[i]:=j;k:=l;p:=O;
While (k<l) And (p=0) Do Begin
If (B[k]=B[i]) Or (Abs (k-i)=Abs(B[k]-B[i] ) )
Then p:=l;
Inc(k) ;
End;
If p=0 Then
If i<N Then Rf(i+1)
Else Begin
For t:=l To N Do Write (B [t] : 3) ;
WriteLn;
End;
End;
End;
Begin
ClrScr;
Rfd);
End.

2 . ,
.
.
64! 8*8. , .

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;
Begin
A[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 Do
For 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 Do
For 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 ;
Begin
A[x,y]:=q;
If (q<N*M) Then Begin
For i:=l To 8 Do Begin {*' W. *}
W[i] : =0 ;xn : =x+Dx [i ] ;yn : =y+Dy [i ] ;
If (A[xn,yn]=0) Then Begin
For j :=1 To 8 Do
If (A[xn+Dx[j] ,yn+Dy[j] ]=0)
Then Inc(W[i]);
End
Else
W[i]:=-l;
End;
i:=l;
While (i<=8) Do Begin
min:=Maxint;
m:=l; {* ,
. *}
For j:=2 To 8 Do If W[j]<min Then Begin
m:=j;min:=W[j];End;
If (W[m]>=0) And (W[m]<Maxint) Then Begin
Solve(x+Dx[m],y+Dy[m],1+1);

3.

8!)

W[m] : =Maxint;{*
. *}
End;
Inc(i) ;
End;
End
Else 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]
ger;
Var
A:MyArray;
xn,yn,xk,yk,N:Integer;
Procedure
Init;

Of

Inte-

Begin
{* ,
.
. *};

90

3.
End;
Procedure

Print;

Begin
{*' -
.*};
End;
Procedure Solve(,,k:Integer);{*k - ,
, - . *}
Var i:Integer;
Begin
A[xry]:=k;
If (x=xk) And (y=yk) Then Print
Else For i:=l To 4 Do IfA[x+Dx[],y+Dy[i]]=O
Then Solve (x+Dx[i] ,y+Dy[i] ,k+l) ;
A[xry]:=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;
Begin
A[xn,yn]:=1/ ykr:=0{*

. *};
ykw:=l; {* .*}
Y:=False;{*
. *}
[ykw,1]:=;0[ykw,2]:=;{*3

. *}
While (ykr<ykw) And Not(Y) Do Begin(*
. *}
Inc
(ykr);i:=O[ykr,1];j:=O[ykr,2];{*

. *}
If (i=xk) And (j=yk) Then Y: =True {*
,
. *}
Else
For t:=l To 4 Do
If A[i+Dx[t],j+Dy[t]]=0 Then Begin{*B
. *}
A[i+Dx[t],j+Dy[t]]:=A[i,j]+l;{^
. *}

92

3.

Inc(ykw) ;O[ykw,l] :=i+Dx[t] ;


[ykw,2]:=j+Dy[t];{*'

. *}
End;
End;
Solve:=Y;{*' Solve. *}
End;
.
Begin
Init;{* ,
.
. *}
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;
End
Else If k<=N Then
For i:=0 To W Div Weight [k] Do Begin
Now[k]:=i;
Solve(k+1,W-i*Weight[k],st+i*Price[k]);
End;
End;
, (Solve(l,W,0)) .
5 . 200 : N , ; - , N 1
.

().
iVP- . .
.
N (N1)\ ,
. ,
N .
,
. .
.
Const Max...;
Var
A:Array[1..Max,1..Max] Of Integer;
{ * . *}

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 Begin
Cost:=Cost+A[v,1]/Way[N]:=v;{*

.

. *}
If Cost<BestCost
Then Begin BestCost:=Cost;
BestWay:=Way;End;
Exit;{*
~ "

. " . *}
End;

3.

96

Nnew[v]:=False/Way[Count]:=v;{* v
,
. *}
For i:=1 N Do
If 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.
@
32
19
33
22
41
18
15
16
31

32
@
51
58
27
42
35
18
17
34

19
51
@
23
35
49
26
34
35
41

33
58
23
@
33
37
23
46
46
32

22
27
35
33
@
19
10
23
23
9

41
42
49
37
19
@
24
42
42
10

18
35
26
23
10
24
@
25
25
14

15
18
34
46
23
42
25
@
1
32

16
17
35
46
23
42
25
1
@
32

31
34
41
32
9
10
14
32
32
@

.
, [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!). , 10 1 7 . , , 60*10 1 7 . 1000000
13
3.2*10
(). ,
.
. . ,
. ? 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
<100.

07
0 99.
0 10
0 18
, .
0 20
JD.
0 24
, , :
R:array[l..MaxN,O..MaxN] , :
R[1,1]=D[1,1]
For i:=2 To N Do

7
3
8
2
4

0
8
1
7
5

0
0
0
4
2

0
0
0
4
6

0
0
0
0
5

15
16 15
25 20 19
30 27 26 24

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<p<N, Op [] + [-] +1} .
,
2 :=Min{ (1) - N,
Op[N div p]+p-l} .
Op[N]=Min(opnl,2). :
Const MaxN=1000;
Var
N, : Integer;
Op: Array[1..MaxN] Of Integer;
Procedure Solve;
Var i, j: Integer;
Begin
For i:=2 To N Do Begin
Op[i] :=Op[i-l]il;
For j:=2 To i-1 Do Begin

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],
<ij> ,
W[i-l,j-l]+l. :
,
. , W[3,l]
, . , .
, , .
. : .
.
For i:=l To Length (SI) Do
For j:=l To Length (S2) Do Begin
A[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

<,1>+

<i,l>+S[k,i]+S[i,l]}. ,
i=k+l <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.

102

1
2
3
4
5

3.

0
0
0
0
0
0

1
0
0
0
0
0

2
0
0
0
0
0

3
0
0
0
0
0

4
0
0
0
0
0

5
5
5
5
5
5

6
5
5
5
5
5

7
5
7
7
7
7

8
5
7
7
7
7

9
5
7
9
9
9

10
5
7
9
9
9

11
5
7
9
11
11

12
5
12
12
12
12

13 14 15 16 17 18 19
5 5 5 5 5 5 5
12 12 12 12 12 12 12
12 14 14 16 16 16 16
12 14 14 16 16 18 18
12 14 14 16 16 18 18


(
), A[i,j] Ni W=j.
.
Procedure Solve;
Var
i,j:Integer;
Begin
For i:=2 To N Do
For j:=1 To W Do Begin
If 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);
Begin
If (i=l) And (A[i,j]=O) Then Exit
Else If i=l Then Begin Way (i ,j-P[i] ) ;
Write(P[i],'
');End
Else If A[i,j]OA[i-l,j] Then Begin
Way(i,j-P[i]) ;Write(P[i] ,' ') ;End
Else Way(i-l,j) ;
End;
6 (
). .
N ( ).
W. i wi

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<fe;<[W/wJ), .
. i
1=1,2,...,^. yi i
, 0,1,...,i. yn=W, ; =0,1,...,
i=l,2,...,Nl. . kt I
i, Q<k<\W/wj].

. W=6,
:
/

W,-

1
2
3
4

2
3
1
4

50
90
30
140

.
(

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: IntegersProcedure
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 Begin
NewA[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 Exit
Else 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<100.

3.6.
. , , .
(backtrack), .
.
. ,
275-194 . .
[2..N] .
.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31 32
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32
, , .
2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
, , .
Const N= ...; {^ . *}
Type Number=Set Of 2. .N; {, N<256}
Var S:Number;

110

3.

Procedure Search (Var S:Number);


Var
i,j:2..N;
Begin
S: = [2. .N] ;
For i:=2 To N Div 2 Do
If i In S Then Begin
j : =i +i ;
Nhile j<=N Do Begin S:-S- [j] ;j :=j+i; End;
End;
End;
N
256. .
*. .
( ) , . 1 6.
, , .
, ( ) :
;
.
. ,


1
1234
0
4655.
2
1
5156
6165
2
1
:
1
2
5625
,
1
2
5653
4655
4
0
, .

. , .
Const =6*6*6*6;
Type
Post=String[4];
Var A:Array[1. .] Of Post;
:Array[1..]
Of Boolean;

111

3.

cnt:Integer;{*

.*}
ok:Boolean;{* - . *}
Procedure
Init;
Var
i,j,k,1:Integer;
Begin
For i:=2 To 6 Do
For j:=1 To 6 Do
For k:=l To 6 Do
For 1:=1 To 6 Do
A[ (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).

11
12
13
14
21
22
23
24
31
32
33
34
41
42
43
44

True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True

( )

( kr)
False
False

False
False
False
False
False
False
False
False
False
False

False
True
False
False
False
False
False
False
False
True
False
False
True
False
True
False

, 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,krrbk:Integer;
Begin
Inc(cnt)
;Write(cnt:2,'
.
' ,h,'-');
ReadLn(kr,bk);
If bk=4 Then Begin ok: =1'rue/< >;End
Else For i:=l To Pmax Do If B[i] And Not Pr(A[i] r
h,kr,bk)
Then B[i]:=False;
End;
.
Begin
ClrScr;
Init;
Hod('1223');
While Not ok Do Begin
\ "|
Hod(h);
End;
End.


:
cnt ? .

? . , (-

3.

113

- True), .

. .

3.7.
1. *. , .

, . ,
. ,
, , , . , , ,
.
, ,
.
:
1 N (4<iV<150).
N .



1
2,3,4
.
2
3
, :
3
1,4,2
2
4
2
2.
. iVP- .
. 1, ( N
Div 2) ,
. ,

.

.
1992 ,
. .

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<min Then Begin min:=mn;
Rbest:=Rwork End;
End
Else Begin
i:=k;
While i<=N Do Begin
1 i
Include(i);{* ,

i
, . *}
Solve
(i+1,Res+A[i].part,Rt-A[i].part);
Exclude(i);{* . *}
Inc (i) ;
End;
End;
End;

. i
(l<i<t) , , i no t ( C:Array[l..N]
Of Sset). , Res , a Rt
, , Res+C[i]<Rt
.

116

3.

i
i ,
,
i , .
2. , ?
. , 9*9, 10*10 11*11.
. ,
, ,
.
3. N*N(N<12) N , .
4. ,
.
5. ( ,
!).
. .
, .
, , .
6. ,
. 10*10 10
( ) .
7. ()
5*5 . i (1<<25)
(,),
:
(z,w)=(x3,y);
(z,w)=(x,y3);
(z,w)=(x2,y2).
:
,
5*5 , 1 (
);

,
, .*
, 1991 .

3.

117

, :
;
;
,
.
M*N (<50, iV>50). 0 4 .
, ,
.


.

(0<<15). : 1 (
), 2 (), 4 ()-, 8 (). . ,
(1,1) (2,1).
.
. , ( ).
, :
, , ,
(N , W , S
, ).
9. .
, .
Z[i] i , 1994 .

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 Do
For j:=1 To M Do
If A[i, j]=0 Then B[i, j]:=B[i, j-l]+l Else
B[i, j]:=0;{*B ,
- . *}
For i:=1 N Do
For 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
(<20, iV<8, M, N ). .
, -2, iV=3
.


<20, N<8. , 20 8 .
,
N . <<*.
. i (1<<8), j
(1<'<20). . . , ,
, . .
VI
1994 .

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 k<>0,
.
B[k,j] i. , B[l,j1]
I (0<Z<21 1). I k,
.
I,
. . :
Var
B:Array[0..255,0..20] ofComp;
A:Array[1. .8,1..20]
of Comp;
{* .*}
Procedure
Solve;
Var
i,j,k,l,max:Integer;
Begin
For i:=l To 8 Do Begin (*
. *}
max:=2Shli-l ; { * 2 i 1. *}
FillChar (, SizeOf () ,0) ;
[0,0] :=1;

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 Record
d, 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 , j<>1;
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 ,pnrpv: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; End
Else Begin pn:~l; pv:=i-l; End;
For j:=pn To pv Do
If 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<iV<100, 1<<100, 0<:r<30000).
Tt,
2, ...,TN, (0<<, i=l,2,...,iV).
Pv P2, ... PN, (0<FV<300,
i=l,2,...,JV).
Slt S2, ... SN,
(1<S<K, i=l,2,...,iV).
.
. , . , , 0.
.

, .
q t.
,

, t
, Sr ?
, , . ,
. t. , t-1.
15. N*M.
,
(, , , ). - .
?
16. A[1..N] B[1..N].
: C[i,j]=A[i]+B[j]
C[i,j]=Min(A[i],B[j]) ioj.
.

126

3.

17. ,
, - -.
.

, (, 3 7, 5 6, 10
11), , . ,
. , 7- 11-, ,
8-, .
.
:
N ,
I, m N=l*m (1<1, 7<11);
( )
.
. :
. - . :
A[i ,j J/+ Min (A[i ,j+l]/, A[i , j +1 ]/
+A[i,j+l]/)
A[i,j]/+Min(A[i+l,j]/,A[i+l,j]/+[i+1,
j]/).

i Z-1 1, / 1 1.
(max ).
, 40.
. :
Const Nmax=ll;
Type Cross=Record Right, Down, Turn :Integer; End;
Var A:Array[1. .Nmax,l. .Nmax] Of Cross,
.

3.

127

18. N*M (2<=N,M<8)


k. k
.
. .
.
:

;
;
.
.
. . . () 1 L, L
, i+1
L. ( ). . .
.
19. , N

,
. ,
,
. , , , .
20. N At*Bt N
Cl*Dl i=l, ..., N.
,
.

128

3.

21. . .
1*1*4 ,
.
. (
) , .

22. 8*8 : . , .
.
( ):
;
.
.
.
23. N*N (N<=10), .
.
.
.
.
. .
2*N , N N . - 2*N ,
k 1 2*N, ,
.
24. 8 *. - , , , , , , ,
( ).
.

3.

129



, .
, , .

(), .
, 1 . . 8 ,
, 1,
'_' 8
.

, . , .
. . ,
. . .
, .
25. ,
. ,
, . , , 7,
36.
0
1

b
1

b
1

0
0

3
0

3
3

3
6

3
9

2
0

12

3
12

3
12

3
12

12

24

36

, .
. . , ,

3.

130

. , '' '&' . '' 'aaab'


, ''. ,
'&' '' , '' 'aaab' (
), '' 'aaab'
( ).
26.
: 2, 3, 4, 6, 7, 8, 9, 10.
6x6, , ,
, .
+
#
+
11

$
@
@
I

11

11

11
#

@
#

@
I

, 11 4
.

.
, .

, , ( 40).
27. MxN (1<=,<=4).
.
(0<<20) ,
, . , .
, - ,
. : , N, . : 2, 2, 2.
: 10.
. , .
M*N . , L ,
1

3.

131

. L+1 L+1 /) , 1
L . , .
28. Heroes of Might and Magic III , .
. . .
NxM,
0 ()
1 ().
, 1, ,

,
.
. N ( , 1<N, M<1000).
N ( ), 0, 1.
.
5 .
, 0.
. . 100.' , ( ). :

( ),
.
, l<iV,M<1000,
.
. ,
(
4
6
4*10 , ). ? N , (-1)- . ,
iV ,

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<N<50.
[-32768, 32767].
. , ,
. , . -7+4*2*5. : (-7)+(4*2*5),

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<<300, ,
1<<30, <. V , .
X 1<<10000.
.
(post.out)
S
. .
.
,
.
.
(1952 ). V Div
2+1 (V )
(V ).

. . .
. , .
i. , .
1

5~ 6

12

16

38

26

8
115

10

291

345

? .
. 1
i.
i. , . (/. 5). -

136

3.

10 I

00

12

21

37

43

. ,
3. i.
.
. . i=7 ( 7 3
). ,
1-

6-. ( 7, , 7), . 1- 5-.


3, 6 2, 5.
3, 4 5 .
1

.4

10

00

00

21

27

00

00

15

00

00

00

. , .

3.

137

, . :

, 9. . ?
, , .
31. * , . . , . , ,
.
, Ab3bd
(dAb3bAd Adb3bdA),
.
. palin.in . N
, 3<N<5000. N,
() '' 'Z',
'' '' '0' '9'. .
. palin.out .
, .
. .
R[i,j] (i,j=l..N), R[i,j]
, S(i,j) i-ro /-
S ,
. R[1,N].
JR? -,
, , .
-, S[i]=S[j], R[i,j]=R[i+l,j-l]. -, S[i]<>S[j], R[i,j]=Min(R[i+l,j],R[i,j-l])+l. . S='Ab3bd'. R :

2000 .

3.

138

0
-

1
0
-

1
0
1
0
-

2
1
0
-

2
1
2
1
0

(S='abcdba3').
0
-

1
0
-

2
1
0
-

3
2
1
0
-

2
1
2
1
0
-

1
2
3
2
1
0
-

2
3
4
3
2
1
0

,
. .
32. : ( ),
.
.
( ), , (
). , . , , ,
.
. input.txt
N (l<iV<180). N , ( 1 20000),
, ,
(1<-<80).
. output.txt , ,
.
.

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+j<N+2 Then A[i, j] :=Max(A[i, j ] ,


sum
- A[i+j,
j]);{*

. *}

End;
End;
End;

33. .
N*N 1, 2, 3, ..., N*N ,
, .
.
2
9
4

6
1
8.

7
5
3
1
12
13
8

1
5
9

8
3
4
2
14
7
11

15
3
10
6

16
5
4

. ^>3.
2
2
N *(N +l)/2. ,
2
N*(N +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 Boolean.
.

. .

,


,
.
.
Procedure
Pg(v:Integer);{*
Nnew
. *}
Var
j:Integer;
Begin
Nnew[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;
Begin
FillChar(St,SizeOf
(St),0);
yk:=0;
Inc(yk);St[yk]:=v;Nnew[v]:=False;
While yk<>0 Do Begin {* . *}
t:=St[yk];{*
" "
. *}
j:=2;:=False;
Repeat
If (A[t,j] <>0) And Nnew[j] Then pp:=True
Else 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.
. ( )
, , .
, . .

4.

144

.

.

.
.
.
Procedure
Pw(v:Integer);
Var :Array[1. .N] Of 0..N;{*. *}
ykl,yk2:Integer;{^
,
ykl - ; yk2 - . *}
j
-.Integer;
Begin
FillChar(Og,SizeOf(Og) ,0) ;
ykl:=0;yk2:=0;{*
.
*}
Inc(ykl);Og[ykl]:=v;Nnew[v]:=False;{*B

- v. *}
While yk2<ykl Do Begin {* . *}
Inc(yk2);v:=0g[yk2]/Write(v:3);{*""
. *)
For j : =1 To N Do {^ ,
v. *}
If (A[v,j]<>0) AndNnew[j] Then Begin {*
,
. *}
Inc
(ykl);Og[ykl]:=j;Nnew[j]:=False;
End;
End;
End;

4.3.
4.3.1. .
. -:
, TV N 1 . G=<V,E>
<V,T>,
(-

4.

145

, ). , .

.
.
JV NN'2.
. .

(). . ,

, , Tree (Array[1..2,1..N] Of Integer).
. ,
.
.

4.3.2.
. G=<V,E>. .
.
. .
, .
G : <v,u> .
G<uu> G_<vu>. G<vu>

146

4.

G<vu> , 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 v

While (j<=N) And (numb<N-l) Do Begin {*


, v.*}
If (A[v,j]<>0) And Nnew[j] Then Begin {*
, j
. .*}
Nnew[j]:=False;
Inc
(numb)/Tree[1,numb]:=v;Tree[2,numb]:=j;
Turn[Up]:=j;Inc(Up);{*

j . *)
Solve(v,j+l); {* . *}
Dec(Up);Nnew[j]:=True;Dec (numb);
{*
. *}
End;
Inc(j) ;
End;

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;

. - . , .
.
<4,3> . , <1,4> <1,5>,
. <1,5> 8 .

148

4.

<1,4> 8 .

4.3.3. .
.
. G=<V,E>.
.
. (Array [ 1..3,1.-N* (N-1) Div 2] Of Integer). . Q=<V,T>,

. .
1. Q, N .
2. G
.
3.

,

Q, :

Q.

4.

149

4. 3 , Q
N-1.
.
3?

(Mark:Array[l..N] Of Integer).
(Mark[i]=i i 1 N). , , ,
. . , ,
Mark .

<1,4>
<4,5>
<2,3>
<2,5>

3
4


[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<l Then Begin t: =1;1:=m;m:=t End;
For i:=l To N Do If Mark[i]=m Then Mark [i] : = J ;
End;
.

Program Tree;
Const N = . . ;
Var
P:Array[1..3,l..N*(N-l)
Div 2] Of Integer;
Mark:Array[1..N]
Of
Integer;
k,i,t:Integer;
M:Integer;{* . *}
Begin
< - >;
< >;
For i : =2 N Do Mark [i ] : =i ;
k:=0;t:=M;
While k<N-l Do Begin
i : =1 /

150

4.
While
(i<=t)
And
(Mark[P[l,i]]=Mark[P[2,i]])
And
(P[l,i]<>0)
Do Inc(i) ;
Inc(k)
;
<

>;
Change_Mark
(Mark[P[l,i]],Mark[P[2
,i]]);
End;
End;

4.3.4. .
.
. G=<V,E>.
. A (Array [ 1 ..N,1 ..N] Of Integer). , ,
. . Q-=<V,T>, TczE.
, , , . . ,
, . .
SM SP (Set Of 1..N). SM , a SP .

<i,j>
, i j SM SP
( ,
SP).
. .

4.

151

.
Procedure Tree; { * - , . *}
Var
SM,SP:Set Of 1..N;
min,i,j,l,k,t:Integer;
Begin
min:=maxint;
SM:=[1..N];SP:=[];{*
. . *}
For i:=l To N-l Do
For j:=i+l To N Do
If (A[i,j]<min) And (A[i,j]<>0) Then Begin
min:=A[i,j];1:-i;t:=j;
End;
SP:=[l,t];SM:=SM-[1,
t];
<
<1, t ;
{*' . *}
While SM<>[] Do Begin
min:=maxint;l:=0;t:~0;
For i:=1 To N Do
If Not (i In SP) Then
For j:=1 To N Do
If (j In SP) And (A[i,j]<min) And (A[i,j]<>0)
Then
Begin min:=A[j,k]; 1:=i;t:=j;End;
SP:=SP+[1];SM: =SM- [1];
< <l,t;
End;
End;

4.4.
4.4.1.
( ) , , , . ,
. -

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;
Begin
FillChar (R,SizeOf (R) ,0) ;
For i:=l To N Do Begin {^
i . *}
T: = [i] ;
Repeat
S:=T;
For 1:=1 To N Do
If 1 In S Then{ * ,
S. *}
For j:=1 To N Do
If A[lrj]=l Then T:=T+[j];
Until S=T; { * ,
,
i . * }

4.

F o r j :=1 N Do
If j
In
T Then
End;
End;

153

R[i,j]:=l;

Q :

Q(v) G , .
, v Q
v R, . . Q=R', R* , R.
A, R 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 /

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, Gv
Gx G2,
1. G2
, 1. G2 ,
1.
. Gx G3, G4 G 5 . . G 4 G5
3 G 5 .
, . . , 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 Do
If A[v,u]<>0 Then
If Num[u]=0 Then Begin
< (v,u) >;
Dvy (u,v) ;
Lowpglv] :=!Min (Lowpg[v] ,Lowpg[u] ) ; { *,
. *}
If Lowpg[u]>=Num[v] Then < >;
End
Else
If (u<>p) And (Num[v]>Num[u]) Then Begin {*u
v. *}
< (v,u) >;
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
, .

4.

158

,
, , , ,
, . G, . .
, . ( ) . ,
, , , .
, . ,
( ) .
, . ,
, .
Procedure
Search(v:Integer);{*
: - , CV - ;
yk - . *}
j:Integer;

Var
Begin
For j:=1 To N Do
If A[v,j]<>0 Then
A[v,j] :=0;A[j,v]
Search (j)
End;
Inc
End;

Begin
:=0;

(yk);Cv[yk]:=v;

Cv Search.
4.5.2.
,
,
. .
.
, .
G.
, . (backtracking). , , . ,
k .

4.

159

, . ,
, .
(k+1) . ,
, .
. () ,
.
,
, .

Procedure Gm(:Integer); {* - .
: - ; St
; Nnew - :
. *}
Var j,v:Integer;
Begin
v:=St [k-1]; {* . *}
For j:=1 To N Do
If (A[v,j]<>0) Then {*
v j . *}
If (k=N+l) And (j=l) Then < >
Else
If Nnew[j] Then Begin {^
. *}
St[k]:=j;
Nnew[j]:=False;
Gm(k+1) ;
Nnew[j]:=True;
End;
End;

4.

160

.
St[l] :=l;Nnew[l] :=False; Gm(2) ;
4.5.3.
(V,T) G=<V,E>
N-1 , N G.
, , . . -, .
G .
N1 , G -N+l ,
G.
,
.

, .
,
. ( ) St,
( Gnum) , . <v,j> ,
/ , 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 Do
If A[v,j]<>0 Then
If Gnum[j]=0 Then Circl[j] {* j
.*}
Else
If (j<>St[yk-l]) And (Gnum[j]<Gnum[v]) Then
< St>(*j
,
. *};
Dec (yk);
End;
,
.

,
G .
( ). \ .

4.6.
4.6.1. .
G=<V,E>, A[i,j]
(i,j=l..N, ./V ),
, i ,
s t .

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
< St>;
< t St>;
v:=t;
While vOs Do Begin
:=< , D[v] =D[u] +A[u,v]>;
< v St>;

4.

163

v:=u;
End;
End;
, D .
, . . D. . .
, . ,
D[vJ, , ,
D[u]+A[u,v]<D[v]. ,
D[vJ D[u]+A[u,v].
4.6.2.
G=<V,E>, s -; (Array[1..NJ..N] Of Integer);
,

D.
,
, - (
), ,
, , .
. ,
, ,
.
. :

()
. D
.
, (3,4,5,6)
, .

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
:=< 1,
min(D[l])>;
T:=T-[u];
For i:=1 To N Do
If i In T Then D[i] :=min (D[i] rD[u] +A[u, i] ) ;
End;
End;

4.6.3.
G=<V,E> ,
.
() D s .

, <i,j>
i /.
.
:
Numln, Numln[i]

,
i;

Num,
Num[i]

i;

4.

165

St, , . ;
, .
. i, Numln (
), St, ( Num), Numln ,
I. , St . .

Numln

Num

St

Nm

[2,2,2,1,0,1]

[0,0,0,0,0,0]

[5]

[2,2,1,0,0,1]

[0,0,0,0,1,0]

[4]

[1,2,0,0,0,1]

[0,0,0,2,1,0]

[3]

[0,2,0,0,0,0]

[0,0,3,2,1,0]

[6,1]

[0,1,0,0,0,0]

[0,0,3,2,1,4]

[0,0,0,0,0,0]

[5,0,3,2,1,4]

[1]
[2]

[0,0,0,0,0,0]

[5,6,3,2,1,4]

[ ]

5
_|

Procedure Change_Num; {*A, Num


. *}
Var Numln,St:Array[1..N]
Of Integer;
i,j,,nm,yk:Integer;
Begin
FillChar(Numln,SizeOf(Numln),0);
For i:=1 To N Do
For 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 yk<>0 Do Begin
u:=Stack[yk];Dec[yk];Inc(nm);Num[u]:=nm;
For i:=1 To N Do
If A[u,i]<>0 Then Begin
Dec(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 Do
For j:=1 To i-1 Do
If A[j ,i]Ooo Then D[i] : =Min (D[i] ,D[j] +A[j ,i] ) ;
End;
, A[i,j] . . Num.
. ?
4.6.4. .

G=<V,E> ([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 Do
Forj:=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 Do
For 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 . *}

Begin
If 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;
Begin
If (Qp=[ ]) And (Qm=[ ]) Then Begin Print () ;
Exit;End;
< Gg
(
Ss) Qp Qm - "
".>;
1:=1;
While i<=N Do Begin
If i In Gg Then Begin
Ss[k]:=i/
Find (k+1, Qp-A [i]-[i] , Qm-A [i] - [i] ) ;
< Qp, Qm ( )
, ,
Gg - " ".>;
End;
Inc(i) ;
End;
End;

171

4.

.

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;
, .
.

1
2

Qp
[1-5]
[4,5]

Ss

Gg
[1..5]
[4,5]

[]
[]

(1)
(1,4)


2, 3
,
4,5

.
If Qm<> [] Then < Gg
>
Else Gg:~Qp;
,
, Qp,
Qp. Find.
3

[]

[]

[]

(1,4)

[5]

[4]

[5]

(1,5)



Find
4 Qp
Qm

While Find k, 2. 5. Find .


3

[]

[]

[]

[]

[4,5]

[]

(1,5)



While ,
Find (/c=1)

4.

172

. : i Qp Qm. : Gg. ,
i, , Qp A[i]. ,
.
Qp: =Qp- [i ] ;Qm: =Qm+ [i ] ;
Gg:=Qp*A[i] ;

Find,
k=l.
1

[2..5]

[1]

[1-5]

[2..5]

[1]

[2..3]

[3,5]

[]

[3,5]

(2)
(2,3)

[5]

[]

[5]

(2,3,5)

[]

[]

[]

[]
[5]

[5]

[]

[3]

[]

[3..5]

[1.2]

[2,3]

[5]

[2]




Gg

(3)
,
Find, Gm


Gm. 1. /, Qm, , A[j] Qp ,
A[j] (
). 2.
Qm, , Qp ? :


/77 QP . . ,
.
Begin
delt:=W+1;
For j:=2 To N Do

4.

173

If j In Qm Then
If Number (A[j] *Qp)<delt Then Begin
i:=j;
delt:=Number(A[j]*Qp);
End;
Gg:=Qp*A[i] ;
End
.
2

[5]

[5]

[2]
[1,2,3]

[]
[]

.
4.7.3.
G~(V,E)

, j,
S, , S /. , , .
.
(1, 2,
3), (4, 5, 6, 7, 8, 9), (1,2, 3, 8, 9), (1,
2, 3, 7) . . (1, 2, 3),
(4, 5, 6, 7, 8, 9) . Q ,
, S*,
,
.
. , 16 . , , - , , .

, , .
,
.

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; Var
Bl: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 :

4.

176


*, . 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;
Begin
k:=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 Begin
If 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;
Begin
P:=P+Price[];{* . *}
S[]:=True;{* . *}
For j:=1 N Do
If A*[j,]=1 Then R[j]:=True; {*,
"" .*}
EndsProcedure Exclude(:Integer);{*
. *}
Var j:Integer;
Begin
p:=p-Pri[];
S[k]:=False;
For j:=1 To N Do
If (A*[j,k]=l) And R[j] Then R[j] :=False;
End;
, , ,
R ,
.

Function Result:Boolean;
Var j:Integer;
Begin
j:=l;
While (j<=N) And R[j] Do Inc(j);
Result:=j=N+l;
End;

, k .
k *
, True R.

4.

179

Function Cross (:Integer) -.Boolean; {*'


,
.*}
Var
j:Integer;
Begin
While (j<=N) And Not(R[j] And (A*[j,k]=l)) Do Inc(j) ;
Result :=j'=N+l;
End;
(Find) ( Bl)
. Find(1,1).
Procedure Find (, jnd: Integer) ;
Begin
If Result Then Begin
If P<Pbetter Then Begin
Pbetter:=P;
Sbetter:=S;
End;
End
Else If Bl [bloc, jnd] =0 Then Exit
Else If Cross(Bl[bloc,jnd] ) Then Begin
Include (Bl[bloc,jnd] );
Find(bloc+l,l);
Exclude(Bl[bloc,jnd]
);
End
Else If R[bloc] Then Find(bloc+1,1)
Else

Find(bloc,jnd+1);

End;

Find * ( )? .
, .
Program R_min;
Const MaxN=.. .;
Type . . .
Var . . .
Procedure Init; {* . *}
Begin
End;

180

4.

Procedure
Begin

Print;{* . *}

End;
{* , . *}
Begin {^ . *}
Init;
Blocs;
Find (1,1) ;
Print;
End.

4.8.
4.8.1.
G~(V,E) .

, fc- G.
,

v. , -, fe-. , G -,

.

- .
.


. .
Const Nmax=100; {^
. *}
Type V=0..Nmax;
5s=Set Of V;
MyArray=Array[1..Nmax] Of V;
Var Gr: MyAr ray; {*Gr -
. *}

4.

181

, , Gr :
Gr[l]=Gr[3]=Gr[5]=l; Gr[2]=Gr[4]=Gr[7]=2; Gr[6]=3.
.
<
>;
For i:=l N Do Gr[i] : = [Color (i ,0) ] ;
<
>;
:
Function
Color(i,t:V):Integer;{*i

, t - ,

, - , Gr . *}
Var Ws:Ss;
j:Byte;
Begin
Ws:=[ ];
For j:=l To i-1 Do If A[j,i]=l Then Ws:=Ws+[Gr[j]] ;
{* ,

. *}
j:=t;
Repeat {* ,
. *}
Inc(j) ;
Until Notlj In Ws) ;
Color:=j;
End;
. : Gr[l]=l, Gr[2]=Gr[4]=2, Gr[3]=3,
Gr[5]=4. : Gr[l]=l, Gr[2]=Gr[5]=2
Gr[3]=Gr[4]=3,
.

4.8.2.
[16],
. , q . , q 1 , , q, g,

182

4.

q. q,
. , , . ? , q, ,
. .

. , . ,
, . , .
, Color .
Var MaxC,MinNum, i : V;
Procedure MaxMin (Var c,t:V) ;
Begin
End;

Procedure Change(t:V);
Begin
End;
Begin
< , >;
For i:=l To N Do Gr[i]:=Color(i,0);{*
. *}
MaxC:=0;MinNum:=0;
Repeat
< (MinNum) ,
() MaxMin(MaxC,MinNum) >;
<
-
Change(MinNum)>;
Until MaxG=Gr [MinNum] ;{ * ,
. *}
< ( !)
>;
End.

4.

183

MaxMin ,
Change .
Procedure Change (t: V) ;
Var r,q:V;
Ws:Ss;
Function MaxCon(1:V;Rs:Ss):V;{*
1
Rs. *}
Var i,w: V;
Begin
i :=1-1; w:=0;
While (i>=l) And (i In Rs) And (w=0) Do Begin
If A[lri]=l Then w:=i;
Dec(i) ;
End
MaxCon:=w;
End;
Begin
Ws: = [] ;
q:=MaxCon (t,Ws) ;
While q<>0 Do Begin
r:=Color(qrGr[q]);
If r<MaxC Then <
, t,

>
Else Ws:=Ws+[q];
q:=MaxCon(t,Ws);
End;
End;
, , t, Gr
, ( ) , .
/
i
,
i i 1 , , , /> . , 1
1, 2 1 2 .
( ) , q
G. ,
-

184

4.

.
,
, .
, . .


, .


.

6
,

.

. .
( -

185

4.

, ), , ,
. , ,
N!.
4.8.3.

G ,
, . . ,
, G .
, ,
. , .

M(N*W), W
. M[i,j] , i , .
, , , . .
.
( ).
4 ,
.
1
2
3
4
5

1
1
1
0
0
0
*

2
1
0
0
0
1
*

3
0
0
1
1
0
*
*

4
0
1
0
1
0
*

5
0
0
0
1
1
*

186

4.

4.9. ,
4.9.1.

, s
() t ().
( ) (/,/)
C{i,j),
, . , , , .
s t
,
, .

. , , s *.
, .
.
( ). s
t
, s t,
,
.

, .
. , 1, 6, .
(1, 2) (3, 4), , 4.
. . . -

4.

187

, . ,

. .
,
, . ,
, . . ,
- .
() ,
( ),
. ( ) . .

[9].
,

.
10 ,
. , .
, 1, (5, 6). ,
11. ,
; ,
, . ,
. ,
.
4.9.2.
. G=(V,E), - 1, -
6. (F) . F . ,
F ,
, .

188

4.

. 1 [1,@]. . , 1 (1,2) (1,3). 2 3 ,


, 2- [1,2] 3- [1,6]. ?
,
,
,
.
.
,
. 2.
,
(2,4) (2,5). 4 5 . [2,2] [2,2]. , 2 , 3, 4, 5 , ,
. . 3. (3,4). 4 . , (4,6). 6 . [4,2].
-, (
), .
.
, , , 2.
2.

4.

189

.
.
1 [1,@].
,
1.
(1,2) (1,3).
2
,

(1,2) . 3 [1,6]. . , .
3. . 4
[3,2]. . 4, . 6
[4,1]. ? , . - . ,
. 3.

.
1
[1,@].
.
[1,5] 3. [3,1]
4. .

(4,6)
.
(2,4),
, ( ,
). . 4 ,
( ).
2,
4 2.
2 [-4,1]. 4
, 2,
2
, . , 4 ,

190

4.

2 , 5 6 .
. 2
6 5. - ,
, . , .
4 .
.
1
[1,@]. .
3
[1,4]. . ,
3. (3,4).
4
.
, - . . . .
, ?
.
: , ; , ,
;
. . , v
, , . , -,
G.
-, , , .
. . G=(V,E) C[1..N,1..N], N
. - s - t. . , F[1..N,1..N]. . P[1..N,1..2].
P[i,l] ,
, P[i,2J, i. Lg, True , , False .

4.

191

.
Begin
< (Lg:=True) >;
While Lg Do Begin
FillChar(P,SizeO(P),0);
< (Mark),
t , Lg:=False;
- ( ) >;
If Lg Then < Stream(t) -

- t - s;
- , -
F>;
End;
< F> ;
End.( )
( ).
Procedure Mark;
Var M:Set Of 1. .N;
i, 1 -.integer;
Begin
M: = [l..N]; (* . *}
P[s,l]:=s;P[s,2]:=maxint;{*
-. *}
l:=s;
While (P[t,l]=O) And Lg Do Begin
For i:=l To N Do {* . *}
If (P[i,l]=0) And ((C[l,i]<>0) Or (C[i,l]<>0))
Then
If F[l,i]<C[l,i] " Then 1{* ?*)
P[i,l]:=1;
If P[l,2]<C[l,i]-F[l,i] Then P[i,2]:=P[1,2]
Else
P[i,2]:=C[l,i]-F[l,i];
End
Else
If F[i,l]>0 Then 1(* ?*)
P[i,l]:=-1;
If P[l,2]<F[i,l] Then P[i,2]:=P[1,2] Else
P[i,2]:=F[i,l];
End;
M:=M- [1] ; { * 1 .*}
1 :=1; { *
. *}

192

4.

Repeat
Inc(l)
Until (1>N) Or ((P[l,l]<>0) And (1 In M) ) ;
If 1>N Then Lg:=False;
End;
End;
F :
Procedure Stream (q:Integer);
Begin {* - ,
-
. *}
If P[q,l]>0 Then F[P[q,1],q]:=F[P[q,1],q]+[t,2]
Else
F[q,abs(P[q,l])]:=F[q,abs(P[q,l] ) ]-P[t,2];
If Abs(P[q,l])<>s Then Begin{*
-,
. *}
q:=Abs(P[q,l]);
Stream(q);
End;
End;
, .
4.9.3.
G=(V,E)
,
. G,
,
. G ,

=(,),

G=(X,Y,E).
. .


G.
S(G) s
t
:

4.

193

s
X;
Y t;

X Y;
C[i,j]=l.
s t .
[18]. (
) .
. , . .
. X Y .
: P={(xo,yt), (yvxt), (xvy2), ...,
(yk,xk), (xk,yk+1)}, k>0, , 0 X, yk+1 ,
,
[18]. G , G .
.
G, .

.
, .
.
. -

.
Begin
< >;
< >;
While < > Do <
>;

194

4.

< >;
End.
. A[N,M], N
X, Y.
XN . XN=[0,1,2,4] =[2,3,0,4,0].
. ,
.
For i:=l N Do Begin {* i,
j , . *}
j :=1; :=True;
While (j<=M) And pp Do Begin
If (A[i,j]=l) And (YM[j]=0) Then Begin
XN[i] :=j;YM[j] :=i;
pp:=False ;
End;
Inc(j) ;
End;
End;

, ? , , Chain:array[l..NMmax]
of -NMmax.. NMmax, NMmax
, [1, -4,
4, -2, 3, -3], . . YM
( ). ,
.
Function
FindChain:Boolean;
Var
p,yk,r:Word;
Begin
< , :=1;>;
< XN>;
If < > Then FindChain:=False
Else Begin
:=< >;
Chain[]:=;
Repeat
:=< Chain>;
For < , r> Do

4.

195

If < XN> Then


Begin If < ""> Then <
>
End
Else Begin If < ""> Then
< >
End;
Until < Chain> or
< YM,
>;
FindChar:=< YM,
>;
End;
End;
.

4.10.

4.10.1.

, , . ( N)
. ( ), .
() .
1. ( ).
2. ,
, 4.
3. i,j,
(l<i<j<N), :
, . .
.

. , .

196

4.

,
.
2.
4. .
()
(Way).
:
Begin
Init;{* ,
. *}
One Way;{*
. *}
Local;{*
. *}
;{* . *}
End.
. Init, OneWay, Out .
. Local. , . , Bestl Best2,
Way, ,
, ,
. . ,
(
Way). .
Swap,
Way). , , , - ,
, - , . , Local.

4.

Procedure Local;
Var i,j :Integer;
Change

197

'.Boolean;

< Bestl Best2,


Swap>;
Begin
Repeat
Change:=False;
For i:=l To N-l Do
For j:=i+J To N Do
If i=j+l Then Begin If Bestl(i,j) Then
Swap (i,j);End
Else If (i=l) And (j=N) Then Begin If
Bestl (i,j) Then Swap(i,j); End {*
,
, "
" -
. *}
Else If Best2 (i,j)
Then Swap(i,j);
Until Not(Change);{* -
Change True.
, , Bestl,
Best2 Swap
Local.*}
End;

4.10.2.
,
. ,
i, j , k (
)

.
1. (
). . , .
2. .
3. .
4. (
). : ,
.

198

4.

5. . .
, CostAp
CostBet. CostFr. CostFr<CostBet,

, CostAp. ,
2*CostFr. : 4 CostAp<2*CostFr, a
, CostAp<2*CostBet.
. .

( . 3.2), ,
. , . : (8,9), (8,1), (9,2), (1,7), (7,5),
(5,10), (10,6), (1,3) (3,4).

4.

199

2. . ( 244) ( 4). : d2 9+dgs>d2 8;


d2 g+ds P*d2 L; d21+d1 7>d2 7. , : d29+dg8+d81+d1 7>d2J.
. CostAp
202. , .
.
Way
, - .
(Array[l..Nmax,l..Nmax] Of Boolean). B[i,j], True, , (/,/)
.
.
Begin
Init;{* - ;
. *}
Solve;{* . *}
Out;{* . *}
End.
Init Out ( ). Solve. .
Procedure
Solve;
Var ?{* .*}
< >;
Begin
InitSolve;{^

Solve. *}
FindTree;{*
. *}
EilerWay;{* . *}
KommWay;{* . *}
End;
,
. -, , ( ). ,
FindTree , , . : -

200

4,

. .
- . St (Array[1..N*(N-1)
Div 2] Of Byte). St Count.
Solve, InitSolve. , EilerWay(l).
.
, . KommWay, .
Procedure EilerWay (v:Byte);
Var j :Integer;
Begin
For j :=1 To N Do
If B[v,j]
Then Begin [v,j]:=False;
B[j ,v] : =False /EilerWay (j) ;End;
Inc (Count);St[Count]:=v;{*3
. *}
End;
Procedure KommWay;
Var s:Set Of 1..Nmax;
i,j:Integer;
Begin
i:=0;s:=[];
For j : = 1 To Count Do{*
. *}
If Not (St[j] In s) Then Begin
Inc (i) ;
Nay[i] :=St[j] ;
s:=s+[St[j]];
End;
End;

4.10.3.

. .
1. ( ).

201

4.

2. (
V), , .
.
.
3. , .
4. .
5. :

:
1-7-5-10-6-4-3-1-8-9-2-1

, 2 3. .
[16], CostAp<1.5*CostBest
.
( )
( ),
( 1, 2, 4, 6).
.
1

00

32

33

41

00

58

42

33 I

58

00

37

41

42

37

32

CostAp, 191.
Solve (. ) (). Pair. (-

202

____

4.

), (
), , .
Procedure
Pair;
Var ?{* .*} ;
< , Pair>;
Begin
InitPair;{*

,
,
. *}
First; {* .. *}
Find;{* . *}
Ad; {* , , .*}
End;

4.11.
1. N (N=<50). 1 (=<6).
,
. : , , ;
;
. , .

, .
2. .
:
-, ,
( ). ,
, .
, ,
,
.
,

. , .

4.

203

,

. - .
3. . ( )
.
q
w.
4. N (20=<=<N=<150), N . , . , .
, .
5. N . N
. ,
. , .
6. N ,
. ,
.

, , .
7. *
N (N 50, N+5), 1 N.

. , ,
.
. 1993 .

204

4.

,
.
.

, . . .
.

, .
, , 6
:
1:
2:
3:
4:
.
8.
(0,0) (100, 100) N (l=<N=<30) .
(0,0) (100,100),
. :
5;
;
;
.
. ,
( ).
, , .
9. N , 1 N. ,
:
;
,
;
, ,
;

4.

205

, ;
, ,
.
10. N (l<iV<33) ,
1 N. ,
.
11.
. 1 N
(l=<N=<30), .
.
, .
:

;
;

,
;
,
.
,
, .
12. ,
. . . ,
, , .
U (), D (), L (), R (). , ( ). , (
,
, ) , . ,
, .

206

4.

.


T ( 1,

90). 1=<, T=<100.
N (0=<N=<100).
N
, ,

U, D, L, R.
. , , .
.
g.in
2 1
51
DRURRRRRDDRDRRRDDLLLLLLLURULUUUUUUUULLLDRRRRDRRDDLU
g.out
44
g.in
10 1
9
RULDRUDUL
g.out
32
. . .

100 ,
, 100 .
,
. (
)

4.

207

. ?
, . , . ,
. 7 4 , 9 3 . 2, 3. 26,
27. ,
. ,
, , 30, 28. , ,
, . .
13. . . . NxM,
.

.
.
(input.txt) N , N
.
. l=<N=<30,
1=<=<30,
0 100.
. (output.txt)
.
. , , , . ,
, .
, . . ,
900*900 .

208

4.

1
.
2
4
5
3
. 1
-7
-2
-7
-107
-102
-7
-7
-7
2
-3
-103

-4
-4
-7
-107
3
-9

(). . .
, , (
),
.
.
Abs(Mn[N,M]).
14. 8x8. N (0<iV<63). . .
, .
, , .
. input.txt
, .
,
, .
-.
. output.txt
,
.
. ().
() ,
( ).
, , .

( ). , .
, .

4.

209

.
(i, j) (, z) Max(Abs(i-p), Abs(j-z)).
,
. (i,j)
sc . ,
,
.
15. N , 1 N. ,

.
. (input.txt) N (1=<N=<33) ,
.
. (output.txt)
N*N, (i,j) , i j -1,
.

.

.
. F[1..N,1..N]

, r+1.
? F[i,k]<>0 i k - r A[k,j]<>0,
i j r+1.
k,
r+1. , ( )
. , ,
.

210

4.

F
- N . N? ,
N .
16. M*N . ,
. : , , .
. ,
,
. . , :
;
, .

, . , , .
, , .
. 1 6. (input.txt) N (1=<,
N=<50).
N ( 1 6) . . 50.
. (output.txt)
.
.
. , . () (Ob), ( )
(k). Ob
. (1, 2, 1, 1, 4, 3, 3, 2), k 8.
(G), .

4.

211


, ,
1 8.
. 8 ( ) .
?
. , . <x,y,play,color>,
, , play
color ,
.
?

.
, .

, .
17. N , .

, ,
.
. , , - , , . ,
,
, . , ,
.

212

4.

. (input.txt) N (l=<N=<500). i- N
,
i. N
[1,1000] ,
.
. (output.txt) , ,
.
. , , . ,
- (),
. 500 . , , , , ,
. . . .
k,
(
):
B[k] ,
,
k ;
W[k] ,
k .

, W
k1 k2, ...,
ki . B[k] k1 k2 ki , ,
. .
B[k]+min(B[k1],W[k1])+min(B[k2],W[k2])+...+min(B[ki],W[ki]).
k
kt, k2, ..., kt ,

4.

213

, . . W[k]=B[k1]+B[k2]+...+B[kl].
min(B[j],W[j]), j
, .
18. . .

( ) .
:
;
, ;

, .
. (input.txt)
:
N Xj 2 ... xN
(1=<N=<30, 1=<i=<30000);
b, ;
;
i1 j1 , ;
i2 j2 , ;
...
iM j M , .
.
(output.txt)
,
. ,
, solution.
2
0
0
0
0
. 5
1
3
4
0

2
5
0
0
0
. 2
0
0
0
0
, 3
2
0

, , , ,
. b
( )

214

4.

, ,
. ,
.
,

.
. , 2
5, 3, 1, 4. ,
, , , . , .
1-4-6, .
.

, ,
6- .
, -,

, -,
. , ,
.
1- 7-. , 2, 3, 4 , .
, , .
19. * .
, ,
. , .
,
,
.


2000 .

4.

215

( ,
). ,

,
- .
, .

, .
, . , - , . .
- .
,
.
, , ,
, ,
.
1 N , N
. (. 214) , , , . ,
,
3, 6 9.
. 2,
: 9 2 4, 6 4 7,
3 .
, , .
. walls.in.
: , 2<<200.
:
N, 3<N<250. : L, l<L<30, L<N.
L : , .

216

4.

2 ,
. , ,
. . / ;
I .
() ,
,
. . , , 1,
2, . .
,
, , ()
.
. walls.out.

: .
: . ,
.
. .
,
, ()
,
. , ,
. ,
. , .
-

4.

217

( ).
, .
.
20. Ga=(Va,Ea) Gb=(Vb,Eb) ,
,
, . .
,
. ,
. .

. ,
N!, N Va Vb,

N (N<12).
( ,
)
, . , , .
21. G . .
. , MN 3*N

(1, 2, 3), (4, 5, 6), ...
(3*N-2, 3*N-1, 3*N).
.
, . -

218

4.

- N=2,
iV=3.
3^ ,
N . N
-.

G . . G'.
G' (
) .
22. 5 2 =[2, 3], S 2 =[l, 2, 4],
S 3 =[3, 4], S^=[l, 3, 4]. Xj, x2, 3, 4,
i = l , 2, 3, 4.
Xj=2, x 2 =4, 3 =3, ^=1,
. ,
. . . M(S)={Sp S2, ..., SN}
,

..
.
S= [1, 2, ..., N]. , M(S)={Slt S2,...,
SN} .
. M(S) .

, iV .
23. N*M, 1 N*M.
, , . ,
, . *.

, .

4.

219

, , . .
1 , +1 2,
. .
.
. Slt S2, S3, S4. ,
St (l<i<N), , N Kt
. , q
q .
Si

15

8
11

13
9

10
1

s2
s3
s4

5
2

12

14

16

16

10

15

10

11

11

14

13

12

2
7

. (
). S1
S2
S3
2 , 1 , S4 16,
3, 4 7 .
. S1 8, S2 11, S3 2. S4 . ( ),
S4 (S4,3), (3,S 3 ), (S 3 ,14). pa ,
,
. S3 14 4, S4 3
Kj ( ). . Sj 10, S2 5,
S3 2. S4.
:
(S4,4), (2,S 3 ), (S 3 ,12), (lO.St),
(S p 15),

(
).

220

4.

. . . ,
;
.
24. ( ) G=(V,E),
. D(v,u)
D(G) = Max(D(v,u))
.

.
.
, R(t)=R(G), . , .
.
. .
25. ,
,
,

, , . , , . -:
,
5 3 ( 2). 5 33 .

N ( ), G 5
33 . , , .
26. (S) , (
)
. . (1758 .)
,

4.

221

G=(V,E), | v\ =N, | E \ =M: S+N=M+2. G


(
),
.
3*S<2*M.
G M<3*N-6 (
N>3). , 5 33 .
27. . (1936 .)
,
. , .
. 0, 1, 2, ... ,
, 0, ,
, 1 . .
: X , Y .
.
28. . G U

, :
U;
,
.
. , , . G,
U. ,
, N.
29. . 8*8 , .
. . .
30. , , , .
.
.
( ) ,
. ,
.

5.

, . . . ,
( . , . [24]) , -,
, -, . , ,
, , . , ,
, ,
. - .
, ,
.

5.1.
,
.
. .
. , ,
=, , . , .

4
4
. : =0,34567*10 fc=0,98765*10- .
: , () .
4
=0,0000000098765*10 .
4
: 0,3456700098765*10 .
( , ,
Real 11-12 ),

5.

223

0,345670*104. , += fe>0!!!
.

, .
Type
Real=Extended;
TPoint=Record x, : Real; End;
Const
Eps: Real=le-3;{* .*}
ZeroPnt: TPoint = (X:0; Y:0);{*
0,0.*}
: =, <, >, <, >.
Function RealEq(Const a, b: Real): Boolean;
{* . *}
Begin
RealEq:=Abs(a-b)<=_Eps;
End;
RealMore ( ), RealLess
( ), RealMoreEq ( ), RealLessEq
( ) .
.
Function RealMax(Const a, b: Real): Real;
{*' . *}
Begin
If RealMore (a, b) Then RealMax:=a Else RealMax:=b;
End;
RealMin ( ) EqPoint .
Function EqPoint(Const ,
:
TPoint):Boolean;
(* ?*}
Begin
EqPoint:=RealEq (A.x, B.x)
And RealEq(.,
.);
End;

.
Function Dist(Const , : TPoint): Real;
{*
. *}
Begin
Dist:=Sqrt (Sqr(A.x-B.x)
+
Sqrt (A.y-B.y));
{* -

224

5.


. *}
End;

,
(0,0).
() v
(vx,vy), w={wx,wy), q=(qx.qy)- :
: q=v+w, qx=vx+wx, qy=vy+wy;
: q=v-w, qx=vx-wx, qy=vy-wy;
: v*w=vxwx+v w ;
: vxw=(vxwy-vywx)k.
v w ,

I | ,
.
, v w .
:
(v+z)*w=v*w+v*z

i,j,
. . v=(vx,vy) w=(wx,wy),
v*w=vxwx(i4)+vwx(j*i)+vxwy(i*j)+vywy(j*j)=
v w i i +v w
( * ) y y(rih
vxwx+vywy.
x x



,
, ,
.
vxw , , :
vxw
vxw v iv, . .
;

5.

225

vxw ,
v w
.
,

.
v niv i,
j, k (vx,vy,vz) (wx,wy,wz). vxw =
(Vxi+Vyj+Vzk> Wxi+Wyj+Wzk) =VxWx i x i + VxWy ixJ + VxWz i x k + VyWx
jxi + vywy jxj + vywz jxk + vzwx kxi +vzwy kxj +vzwz kxk. : ixi =0, jxj =0,
kxk =0, ixj =fe, jxi=-k, jxk=i, kxj =-t, kxi =j ixk =-/'.
: vxw =(vywz-vzwy)i + (vzwx-vxwjj + (vxwy-vywx)k.
, ( ):

, . , , v w
, vxw=(vxw -v wx)k.
, .
.
. , ,
.
, SOACB=Ax*By-Ay*Bx,
SoAB=(l/2)*(Ax*By-Ay*Bx). , SOBA
( ).
.
: Type TVecCart=TPoint; (Record x, : Real;End)
Procedure AddVecCart(Const a, b: TVecCart; Var c:
TVecCart) ; {*
. *}

226

5.

Begin
.:=.+.;.:=.+b.;
End;

SubVecCart, MulKVecCart, SkMulCart VectMulCart . :
Function SkMulCart (Const a, b: TVecCart): Real;
{* . *}
Begin
SkalarMulCart: =a . x*b. x+a . y*b. ;
End;
v
() v X.

(vx,v ) :

v=Sqrt(vJ+v 2),
Type TVecPol=Record rst, angle: Real';End;.

.
.
Function GetAngle(Const x, : Real): Real;
{ * 0 2*Pi
( ) . *}
Var rs, : Real;
Begin
rs:=Sqrt (Sqr(x)
+ Sqr(y));{*
(0,0) (,) .

Dist.*}
If RealEq(rs, 0) Then GetAngle:=0
Else Begin
c:=x/rs;
If RealEqfc, 0) Then c:=Pi/2
Else c:=ArcTan (Sqrt (Abs(1-Sqr(c)))
/c);
If RealLess (c, 0) Then c:=Pi+c;
If RealLess (y, 0) Then c:=2*Pi-c;
GetAngle:=c;
End;
End;

5.

227

Procedure CartToPol(Const a: TVecCart; Var b:


TVecPol);{*
. *}
Begin
b.rst:=Sqrt(Sqr(a.x) + Sqr(a.));b.angle
:=GetAngle (a .x, .);
End;
Procedure PolToCart (Const a: TVecPol; Var b: TVecCart);
{*
. *}
Begin
b.x:=a.rst*cos
(a.angle);b.y:=a.rst*sin
(a.angle);
End;

5.2.
,
(vx,uy) (wx,wy),
: (wx-vx)*(y-vy)=(wy-vy)*(x-vx).
: -(w v )*x
+(wx~vx)*y +
(wy-vy)*vx ~ (wx~vx)*v!/=0
, :
*+*+=0,

A=v.y-w.y,
B=w.x-v.x,
C=-(v.x*(v.y-w.y)+
+v.y*(w.x-v.x)).
. : Type TLine=Record , , : Real;End;
Procedure Point2ToLine (Const v, w: TPoint; Var L:
Thine) ; {*
. *}
Begin
L.A:= v.y - w.y;
L.B:= w.x -v.x;
L.C:=-(v.x*L.A
+
v.y*L.B);
End;
2*+2*{/+;=
2*+2*+2=0, , , (1*2~2*1<>0):
= (, * 2 - 2 * ; )/(1 *2-2"1),
y^A^C.-A^C^/fA^B.-A^Bj)
. CrossLine -

228

5.

( ,
), Line2ToPoint
.
Function CrossLine(Const LI, L2: TLine) : Boolean;
{*
. True,
, False,
. *}
Var st: Real;
Begin
st:=Ll.A*L2.B-L2.A*Ll.B;
CrossLine:=Not RealEq (st, 0) ;
End;
Procedure Line2ToPoint(Const LI, L2: TLine; Var P:
TPoint);{*
. *}
Var st: Real;
Begin
st:=L1.A*L2.B-L2.A*L1.;
P.X:=(L1.C*L2.B-L2.C*L1.B)/st;
P.Y:=(Ll.A*L2.C-L2.A*Ll.C)/st;
End;
t:
x=vx+(wx-vx)*t,
0<<1 (,) , t<0 t>l
, .

(wxvx)*(yv)=(wvy)*(xvx)
, , ,
.
Function AtSegm(Const , , : TPoint): Boolean;
{* . *}
Begin
IfEqPoint(A, ) Then AtSegm:=EqPoint (A, P)
{* ,
.*}
Else
AtSegm:=RealEq((.-.) *(.-.),
(.-.) *
(.-.))
And ( (RealMoreEq(P.x, A.x) And RealMoreEq (. ,
.)) Or

5.

(RealMoreEq(.,

.)

229

And

RealMoreEq(A.x,

.)));

End;

,
.
,
,

( ,
).
Procedure FindPointCross(Const fL, fR, sL, sR:
TPoint; Var rs: TPoint);{*
, rs (0,0). *}
Var LI, L2: TLine;
Begin
Point2ToLine (fL, fR, LI);
Point2ToLine (sL, sR, L2);
If CrossLine (L1,L2) Then Line2ToPoint (, L2, rs)
Else
rs:=ZeroPnt;
End;

, (0,0).
.
.
Function SegmLineCross (Const fL, fR, sL, sR:
Tpoint): Byte; {*0
, .
0,
, 1 2
. *}
Var Minf, Maxf,Mins ,Maxs : Real;
Begin
Minf:=RealMin(Dist(fL,ZeroPnt),Dist
(fR,ZeroPnt));
Maxf:=RealMax(Dist(fL,ZeroPnt),Dist
(fR,ZeroPnt));
Mins:=RealMin(Dist(sL,ZeroPnt),Dist(sR,ZeroPnt));
Maxs:^RealMax(Dist
(sL,ZeroPnt),Dist(sR,ZeroPnt));
If RealEq(Minf,Maxs) Or RealEq (Maxf, Mins) Then
SegmLineCross:=0
Else If RealMore (Mins,Maxf) Or RealMore (Minf ,Maxs)
Then SegmLineCross:=1
Else SegmLineCross:=2;
End;

230

5.

, . ( ).
Function SegmCross(Const fL,
fR,
sL,
sR:
Tpoint):
Byte; {* ,
, 1 -
, 2 -
, 3 - , 4 -
, 5 - , 6 -
,

,
7 -
, . * }
Var rs:TPoint;
LI ,L2-.Thine;
Begin
Point2ToLine (fL, fR, LI);
Point2ToLine (sL, sR, L2);
If CrossLine (LI,L2) Then Begin
Line2ToPoint(Ll, L2, rs)
If EqPoint (rs,fL) Or EqPoint(rs,fR) Or EqPoint
(rs,sL) Or EqPoint (rs,sR)
Then
SegmCross:=5
Else If AtSegm(fL,fR,rs)
And AtSegm(sL,sR,rs)
Then SegmCross:= 7
Else If AtSegm(fL,fR,rs) Or AtSegm
(sL,sR,rs) Then SegmCross:=6
Else
SegmCross:=4;
End
Else
If EqPoint(LI.A*L2.B,L2.A*L1.B) And Not
(EqPoint (L1.C,L2.C))
Then SegmCross:=3
Else SegmCross.-= SegmLineCross ( fL, fR,
sL, sR);
End;

() -. .
, .

5.

231


i> V2> V3> V4- ,
v1*v2<0 v3*v4<0, ( vp v2, v3,
v4). v1*v2>0 v3*v4>0 . ,
. ; =0,
v2<>0 v3*v4<0,
.
v2~0 , v3=0
v4=0 . , 1^=0, v2=0, v3=0, v4=0.

, ,
.
?

- , 2- 3-.
.
v



. : (,) (x^i)}) (2,2) - (xq,y3). :
x=xo+t1*{xrxo). y=yo+tt*(yryo), t^[0,l], x=x2+t2*(x3-x2),
y~y2+t2*(y3y2), t2<=[0,l]. , :

232

5.
=

xo+t1*{x1-xo)
+1:

1*()

x2+t2*(x3-x2)
+

2 2*(~)-

tl t2.
tv t2e(O,l), . tt t2 0 1,
[0,1],
, . .
() L (*+*+=0) , . ,
L - =(,).
,
L,
=(,), (-0, )=0.
*{-0) +*(-)=.
Procedure PerLine(Const n: TLine; Const P: Tpoint;
Var L: TLine); {*, ,
.*}
Begin
L.A:=n.B;
L.B:=-n.A;
i
L.C:=-L.A*P.X-L.B*P.Y
End;
(,)
L :
p(P,L)=Abs(LA*xo+L.B*yo+L.C)/
(sqrt(LA2+L.B2).
Function DistPointToLine (Const P: TPoint; Const L:
TLine): Real;{* . *}
Begin
DistPointToLine:=Abs
((L.A*P.x+L.B*P.y+L.))
/Sqrt(Sqr(L.A)+Sqr(L.B));
End;

5.3.
, , .

(+>), (+>) (+>).

5.

Function

233

IsTrian(Const a, b,
: Real): Boolean;
{^
, , .*}

Begin
IsTrian:=RealMore (a+b, c) And RealMore (a+c, b)
And RealMore(b+c, a);
End;
. ,

S=Sqrt(p*(p-a)*(p-b)*(p-c)),
=(++)/2.
Function
Sq(a,b,c:Real):Real;
Var p:Real;
Begin
p: = (a+b+c)/2;
Sq:=Sqrt (p* (p-a) * (p-b) * (p-c) ) ;
End;

:
S = (*)/2 = (a*b*sin(C))/2 = {a*a*sin(B)*sin(C))/(2*sin(A))=
= (h*h*sin(A))/(2*sin(B)*sin(C)),
.
.
, . j p / =(x i ,t/ i ), 2=(2,2), 3=(3,3)
, .
. I

-,

2 -,

- ,

3 -,

. -

U h
+(2*33*2) ( ).
Function SquareTrian(Const pi, p2, : TPoint) :
Real; {*
. *}
Begin
SquareTrian := (pi. x* (p2 . y- .y) -pi . (2 . x- . x)
+ (p2.x*p3.y-p3.x*p2.y) ) /2;
End;

234

5.

. ,
.
f2 2

(2,2)

(-1,-1)

-1 -1
-2
1
(1,-2)
9.
'2
2 1
1 - 2 1 9. :
-1 -1 1;
, .
.
'4
-11
0 0 1 10,
-2 3
V

(-2,3)

.(3,2)

(4,-1)

'4
2
15. : 3
, .

,
, ha.

ha=2*SQRT(p*(p-a)*(p-b)*(p-c))/a,
=(++)/2,
Function GetHeight (Const a, b, : Real):
Real;{* ,
,
. *}
Var p: Real;
Begin
p:=(a+b+c) /2;
GetHeight :=2*Sqrt (p* (p-a) * (p-b) * (p-c) ) /a;
End;
(
), . 2:1,

5.

235

. .

2
2
2
ma=SQRT(2*b +2*c -a )/2.
Function GetMed(Const a, b, c:
Real): Real; {*
, ,
.
Begin
GetMed:=Sqrt(2
*(b*b+c*c)-a*a)/2;
End;
( ), .
r=SQRT((p-a)*(p-b)*(p-c)/p).
,
fia=2*SQRT(b*c*p*(p-a))/(b+c), .
Function GetRadlns(Const a, b, : Real): Real;
{* ,
, , .*}
Var p: Real;
Begin
p: = (a+b+c) /2;
GetRadlns :=Sqrt ( (p-a) * (p-b) * (p-c) /p) ;
End;
Function GetBis(Const a, b, c: Real): Real;
{* ,
,
. *}
Var p: Real;
Begin
p:=(a+b+c) /2;
GetBis :=2*Sqrt (b*c*p* (p-a)) / (b+c) ;
End;
, , , .
, -

236

5.

, .

R=(a*b*c)/(4*SQRT(p*(p-a)*(p-b)*(p-c))).
, ,
, , , ;
.
. , .
Function GetRadExt(Const a, b, : Real): Real;
{* ,
, , .*}
Var p: Real;
Begin
p:=(a+b+c) /2;
GetRadExt:=a*b*c/ (4*Sqrt (p* (p-a) * (p-b) * (p-c) ) ) ;
End;

5.4.
,

.
1
,


.
, . , ,
.
( , ).
Function
IsPoligonSimple (Const
A:Array
Of
TPoint;Const N:Word):Boolean;
{* . False, , True, .

. , , . *}

5.

237

Var i , j : Integer;
pp:Boolean;
Begin
pp:=True;
i:=2/
While (i<=N-l) And pp Do Begin
j:=i+l;
While (j<=N) And pp Do Begin
Case SegmCross (A[i] ,A[i+l] ,A[j] ,A[ (j+1) Mod
N]) f{*
. *}
0,2,6,7:
pp:=False;
End;
Inc (j) ;
End;
Inc(i) ;
End;
Is
PoligonSimple:=pp;
End;
. iV- Q. ,
/
,,
JV-yro
Q, .
L, X.
L Q, Q,
Q. (w) ,
. Q , w . () . , ,
. ,
, ( ).
L ; L
. . - L ,
,
.
Function IncludPoint(Const A: Array Of TPoint;
Const N: Word; Const P: TPoint) : Boolean;
{^
( ) . * j

238

5.
Var i, nxt, sc:
If, rg: TPoint;
nx: Real;
Begin

Integer;

sc:=0;
IncludPoint:=True;
For i:=0 To N-l Do Begin {*

. *}
nxt:=(i+l) Mod N;
If AtSegm(A[i] , Afnxt] , P) Then Exit;{ *

(A[i] rA[nxt]) .*}
lf:=A[i];
rg:=A[i];
If RealLess (A[i] .y, A[nxt] .y) Then rg:=A[nxt]
Else.
lf:=A[nxt];
If RealLess(If.y,
Afij.y)
And RealLessEq
(A[i].y, rg.y) Then Begin

Nx:=((rg.x-lf.x) *P.y+lf.x*rg.y-lf.y*rg.x)/
(rg.y-lf.y); {*

, X.

(wx-vx) * (y-vy) = (wy-Vy) * (x-vx)
,
. *}
If RealMore (nx, P.x) Then Inc(sc);
End;
End;
IncludPoint:=Odd(sc);
End;
. . . . .
.
.
* (, , ),
(+)*, , ,
h .

5.

239

0123' Pt~(xi>ydm , :
S2=x2*y2-xl *(2+1 )*(-1 )=..~Xj *2-2*2
S3=x3*y3-x2*y2-(y3+y2)*(x3-x2)=...=x2*y3-x3*y2
S4=x0*y0-x3*y3-(y0+y3)*(x0-x3)=...^x3*y0-x0*y3

, ( ), .

:
Sl+S2+S3+S4=x1*(y2-y0)+x2*(y3-yl)+x3*(y0-y2)+x0*(y1-y3).
. (S) 3,5.

2*5=1*(1-2)+4*(4-1)+1*(3-1)+1*(3-4)+
+2*(2-3)+2*(2-3)+1*(1-2)=7.

.
Function Square(Const A: Array Of TPoint; Const N:
Word): Real;{*
N . *}
Var i: Word;
sc: Real;
Begin
If N<3 Then Square:=0
Else Begin
Sc:=A[O].x*(A[l].y-A[N-l].y)+A[N-1].x*
(A[0].y-A[N-2].y);
For i:=l To N-2 Do sc: =sc+A[i] . x*
(A[i+1] .y-A[i-l] .y) ;
Square:=sc/2;
End;
End;

240

5.

. . ,
. , . 180*(N-2), N .
-. , , .
,
.
, ,
,
.
Function
IsPoligonConvex(Const
A:
Array
Of
TPoint;
Const N: Word): Boolean;{* , True, . *}
Var bn, nw: Byte;
i: Integer;rp: Real;
Begin
IsPoligonConvex:=False;
If N>3 Then Begin
bn:=1;
For i:=0 To N-l Do Begin
rp:=SquareTrian (A[ (i+N-1) Mod N] , A[i], A[(i+1)
Mod N]) ; {*
,
. *}
If RealEqfrp, 0)
Then nw:-l{*
. *}
Else If RealLess (rp, 0) Then nw:=0 Else nw:=2;
If (bn=l) Then bn:=nw
Else If (nwOl) And (nwObn) Then Exit; {*
,
. *}
End;
End;
IsPoligonConvex:=True;
End;

5.

241

5.5.
S, N .
.
. .

,
,
. 9;929 , ,
qtq243
,
.
:
t;
t ,
S
t;
, q2 ( ).
.
, .
. .
, N O(N*logN) ( , ,
). ( ) . , N.
, , N*logN.
.
Var A:Array[l. .MaxN] Of TPoint; { * . *}
N:
Integer;{* .*}
M: Integer;{*
. *}
Is: Array[I..MaxN] Of Integer;{* .
Init
: For
i:=l To N Do ls[i] :=i.*}
. [24].

242

5.

bb:

Rd:

Array[I..MaxN]
Of Boolean;{*

, FillChar(bb,
SizeOf(bb),
True),

. *}
Array[1..MaxN] Of Real;{* . *}

:
WriteLn (M) ;
For i:=l To N Do If bb[i] Then Write (Is [i] ,

'

') ;

, ,
.
Procedure
Solve;
Var
aa: TPoint;
i , j , r : Integer;
Begin
j:=1;{*,

X
. *}
For i:=2 To N Do {* ,
. *}
If RealMore(A[j] .,
A[i].x)
Or (RealEq(A[i] .x, A[j].x) And RealMore (A[i] .y,
A[j] .y))
Then j:=i;
aa:=A[l] ;A[1] : =A[j] ;A[j] : =aa;
r:=ls[1];ls[1]:=ls[j];ls[j]:=r;{*
,
,
. *}
{* ,
, ,
. *}
For i:=2 To N Do Begin
aa . : =[i] .x-A[l] . x;aa .: =A [i] .y-A[l] .y;
Rd[i]:=-aa.y/Sqrt(Sqr(aa.x)
+
Sqr(aa.y));
End;
Sort (2, N) ;{* ,
.... ,
- Rd[i]

5.

243

Is (
). *}
{* . *}
If N>3 Then Rounds;{*. *}
End;

, .
Procedure Rounds;
Var
If, md, rg: TPoint;
lfi,
mdi,
rgi:
Integer;
r: Real;
Function Predd(Idi:
Begin
Dec(ldi) ;
While Not(bb[ldi])
Predd. =ldi;
End;

Integer): Integer;

Do

ldi: = (ldi+N-2) Mod N + 1;

Begin
M:=N;{* .*}
lf:=A[2]; md:=A[3];
rg:=A[4];
1fi :=2/ mdi :=3; rgi:=4;
While rgi<>2 Do Begin
r:--SquareTrian(If, md, rg);{*
.*)
If Not(RealMore(r, 0)) Then Begin{*
mdi . *}
bb[mdi]:-False;mdi:=lfi;md:=lf;
1fi:=Predd(1fi);{*

. *}
lf:=A[lfi];
Dec (M) ;
End
Else Begin {* . *)
lfi:=mdi; mdi:=rgi; rgi:=rgi Mod N + 1;
lf:=md;
md:=rg;
rg:=A[rgi];
End;
End;
End;


. .
L, , ,

244

5.

S L .
. N 2
2
C N, . . O(N ) .
N-2 . , , 3
O(N ), ,
.
. , ,
. , qtq2 ,
q2, .

,
O(N2). , ,
.
:
ConstMaxN=100;
Var

A:
N:
rs:

M:

Array [I. .MaxN] Of TPoint; { *


. *}
Integer;{^
.*}
Array[I..MaxN]
Of
Integer;{*
,

Init
(
)

FillChar(rs,SizeOf(rs),0).*}
Integer;{*

.
*}

.
. .
Function
GetLeft:
Integer;
Var i, If: Integer;
Begin
lf:=l;
For i :=2 To N Do
If RealLess (A[i].x,
A[lf].x)
Or
(RealEq(A[i].x,
A[lf].x)
And RealLess (A[ If ]. ,
A[i].y))
Then
lf:=i;
GetLeft:=lf;
End;

5.

245

.
Procedure
Solve;
Var
nx: Integer;
Is:
TPoint;
Begin
M:=0;(* . *}
nx:=GetLeft;{*
. *}
While (M=0) Or (nxOrs [1]) Do Begin { *
.*}
Inc(M); rs[]:=;{*
. *}
If M>1 Then Is:=[rs[M-l]]{*
. *}
Else
Begin
ls:=A[rs[1]];
Is.y:=ls.y-1;
End;
{*
,


. *}
nx:=GetNext(Is,
A[rs[M]]);{*

,

. *}
End;
End;
, , GetNext
( , ). ,

.
, , , (A[i],gn) (gn,pr),
. , ,
gn.
, .
Function GetNext (Const pr, gn:
Var
i, fn: Integer;
mx, rsx, nw: Real;
Begin

TPoint):

Integer;

246

5.

:=-10;{*
- ,
. *}
For i:=l To N Do Begin
nw:=GetAngle (pr, gn, A[i]);(* . *}
IfRealLessfmx,
nw)
Then Begin fn:=i;mx:=nw;
rsx:=Dist(gn,
A[i]);End
Else If RealEq(nw, mx) Then Begin
nw:=Dist(gn,
A[i]);
If RealLess (rsx, nw)
Then Begin fn:=i;
rsx:=nw;End;
End;
End;
GetNext:=fn;
End;
. ,
. Ll L2 ,
2*+2*+2=0,

2*2+2*2>0

. L1 L2 n;={AI,Bi} 2={2,2}
. ,
cos(a)=(nvn2)l( \nj I *\n 2 1 ;= (A1*A2+B1*B2)/
/(Sqrt(A*A1+B*B1)*Sqrt(A2*A2+B2*B2)).
Function GetAngle(Const , , : TPoint): Real;
Begin
If RealEq(Dist (C,B) , 0) Then GetAngle:=-11
Else GetAngle: = ( (B.x-A.x) * (C.x-B.x) + (B.y-A.y) *
(C.y-B.y))/(Dist (A,B) *Dist(C,B));
End;

, , , , ,
. , .
, , , .
. S -

5.

247

Sj S2 .
.
O(N), :
T(N)<2T(N/2)+0(N).
, S N ,
,

.
w , , ,
. L,
S Sj (
L) S2 ( L). h, <whr>
{<wpr>:peSj}. ,
, <hwr> .
h .
, h , L,
S. w h Lv
h L2. Sj
.
L ; ,
L2, , ,
, <wrh>
. , Lt L2 .
.
Const MaxN=100;
Type TSpsk = Array[0..MaxN] Of Integer;
Var
A: Array[1 . .MaxN] Of TPoint;
N:
Integer;
rs:TSpsk;{* ,
, rs[0]
. *}
:

248

5.
NriteLn (rs[0]);
For i : = J To rs[O] Do Write (rs [i],

' ') ;

,
( ).
Get Sign. ,
,
.
Function GetSign (Const L: Thine; Const A: TPoint):
Integer;
Var rs: Real;
Begin
rs:=A.x*L.A + A.y*L.B + L.C;
If RealEq(rs, 0) Then GetSign:=0
Else If RealMore (rs, 0) Then GetSign:=l
Else GetSign:=-l/
End

, SquareTrian ( ), .
Function Square(Const , , C: TPoint) : Real/
Begin
Square:=Abs((A.x*(B Y-C ) + B.x*(C .y-A.y) + C.x*
(A.y-B.y))/2);
End;

, . . , fr. (If, rg) (L). rs


,
L,
(). , ,
, .
.
Procedure SelectPoints (If, rg: Integer/Const
nn: TPoint/Const fr: TSpsk/ Var rs: TSpsk) ;
Var i, lzn, nw: Integer;
L: TLine;
Begin
rs[0]:=2;
rs[l]:=lf;
rs[2]:=rg;

5.

249

Point2ToLine (A[If] , A[rg], L) ;{*


, . *}
lzn:=GetSign (L, nn);{*
,
nn. *}
For i:=3 To fr[0] Do Begin
nw:=GetSign(L,
A[fr[i]]);
If nw*lzn=-l
Then Begin
Inc(rs[0]);rs[rs[0]]
:=fr[i];End;
End;
End;

.
(ow). , . . ,
.
Function MDist(Const ow: TSpsk): Integer;
Var max, nw: Real ;
i, rs: Integer;
Begin
max:=Square(A[ow[l]],
A[ow[2]],
A[ow[3]]);
rs:=3;
For i:=4 To ow[0] Do Begin
nw:=Square(A[ow[l]],
A[ow[2]]r
A[ow[i]]);
If RealMore (nw, max) Or (RealEq(nw, max) And
RealMore(A[ow[rs]].x,
A[ow[i]].x))
Then Begin max:=nw;rs:=i;End;
End;
MDist:=ow[rs];
End;

. ,
.
.
Procedure FindMinMax(Var If, rg: Integer; Var All:
TSpsk);
Var
i, sc: Integer;
Begin
If:=1; rg:=1;{*
(If) (rg)
,

250

5.


. *}
For i:=l To N Do Begin
If RealMore(A[i].x, A[rg].x) Or
(RealEq(A[i] .x, A[rg].x) And RealMore (A [ rg] . ,
A[i].y)) Then rg:=i;
If RealMore (A[lf] .x, A[i].x) Or
(RealEq(A[i] .x, A[lf].x) And RealMore (A [If ]. ,
A[i] .) )Then lf:=i;
End;
All[0]:=N;All[l]:=lf; All[2]:=rg; sc:=2;
For i:=2 To N Do
If (iolf) And (iorg) Then Begin Inc(sc);
All[sc]:=i/ End;
End;
. , ow, .
Procedure GetSpisok(Const ow: TSpsk; Var rs: TSpsk);
Var i, nw: Integer;
If, rg, rssc: TSpsk;
Begin
rs:=ow;
If ow[0]>2 Then Begin
nw:=MDist(ow);(*
ow[l] ow[2]. *}
SelectPoints (ow [1], nw, Afow[2J]/ ow, If);
{*' ,
,
ow[2], ,
ow[l] nv/. *}
SelectPoints (nw, ow[2], A[ow[l]], ow, rg) ;
{* ,
,
ow[l] , ,
nw
ow[2]. *)
GetSpisok (If, s);{*

.
2,
. *)
GetSpisok(rg, rssc);

5.

251

For i:=2 rssc[0] Do rs[rs[0]+L-1]:=rssc[i];


{* .*}
Inc(rs[0],
rssc[O]-l) ;
End;
End;

.
Procedure Solve;
Var
If, rg, i: Integer;
fr, frl: TSpsk;
nw: TPoint;
Begin
FindMinMax(If, rg, rs);{*

. *}
nw:=A[lf] ;nw.y:=nw.y-10;
SelectPoints(lf, rg, nw, rs, fr);{*B fr
,
,
If rg. *}
nw. : =nw. +20 ;
SelectPoints (rg, If, nw, rs, frl) ;{*B frl ~ no
. *}
GetSpisok(fr, rs);{*
fr. *)
GetSpisok (frl, fr);{*
frl. *}
For i:=2 To fr[01-1 Do rs[rs[0]+i-l]:=fr[i];
{* . *}
Inc(rs[0] , fr[0]~2) ;
End;

5.6. ,
1. N (0<JV<5000) , .
.


1
[-10000, 10000]
-

252

5.

.

,
. .
7 . .
:
7 ( )
-15 0 5 10 ( )
- 5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
: 228.
.
- . ,
. .
, .

(2, 5), (4, 6 ) (8,10). ^
l l l l
I
.
j
4 5 6
8
10
? .
,
(1) (-1).
,
.
. ,
.
. ,
, . ,
Y, X.
(). .
Y.
: (0,1),
(1,2), (2,3), (3,4) (4,6). , . .

5.

253

2:(5,0,2)
( 3:(7,19,4)
4:(4,,6)
X
4.,
),
.

, , 10
,
. X , . ,
( 1) 1,
2.

.
.
. , .
Const MaxN=5001/
Type
MasPn=Array[1..MaxN*2] Of Integer;
MasSw=Array[l..MaxN*2,
1..2]
Of Integer;{*

.*}
Var N, i: Integer;
, Ay: MasPn;{*
. *}
.
Begin
Assign (Input, '...') /Reset (Input) /Read (N) /
For i:=l To N Do Begin Read (Ax [ i*2-l ] ,
Ay [i* 2-1] ) /Read(Ax[i*2] , Ay[i*2]); End/
Close (Input)/
Assign (Output, '...') /Rewrite (Output) /
WriteLn(GetPerim(Ax, Ay) + GetPerimfAy, Ax));
Close(Output);
End.

O(N*logN) .

254

5.

Procedure Sort(Var nn: MasSw; If, rg: Integer) ;


Begin
End;

GetPerim.
Function GetPerim (Const Ax, Ay: MasPn): Longlnt;
Var i: Integer;
sc: Longlnt;
D: MasSw;
Begin
sc:=0;{*
. *}
FillChar(D, SizeOf (D) , 0) ;
For i:=l To 2*N Do D[i, 1 ] :=Ax [i] ; { *
. *}
Sort(D, 1, 2*N) ;{* . *}
For i:=l To N*2-l Do
IfD[i, !]<>D[i+l, 1] Then{*'
,
- GetPr,
2.*}
sc:=sc+GetPr(Ax, Ay, D[i, 1]) * (D[i+1, 1] D[i, 1]) * 2;
GetPerim:=sc;
End;
.
, .
Function GetPr(Const Ax, Ay: MasPn; x: Integer):
Integer;
Var
i, b, d, sc: Integer;
nn: ^MasSw;{*
( 5000), . *}
Begin
New (nn) ;

GetRect(Ax, Ay, x, , sc);{*


,
, sc,
nn*. *}
If sc<>0 Then Sort (, 1, sc);{*

. *}

5.

255

:=0/{* . *}
If sc>0 Then d:=l Else d:=0;
For i:=l To sc-1 Do Begin
b:=b+nn~[i, 2];

If (b=0) And (nn*[i+l, 1]<> [1, 1]) Then Inc (d) ;


{* b
,
. *}
End;
GetPr:=d;
Dispose(nn);
End;
.
Procedure GetRect (Ax, Ay: MasPn; x: Integer;
Var nn: MasSw;Var sc: Integer);
Var i: Integer;
Begin
sc:=0;
For i:=1 To N Do
If (Ax[i*2-l]<=x) And (x<Ax[i*2]) Then Begin
{*
,
(
)
. *}
Inc (sc) ;
nn[sc,
1]:=Ay[i*2-l];nn[sc+l,
1]:=Ay[i*2];
nn[sc, 2]:=1;nn [sc+1, 2]:=-l;
Inc (sc);
End;
End;
2. N (1<JV<3OO)
, ( ). (,) . ,
.
X.

X -

256

5.

,
().

X, < > * < >. .
, 1, - 1 Y.
. , .
,
.
ConstMaxN=300;
Type
TPoint=Record X,Y:Real/End;
Var
PrM: Array[1..2,1..MaxN]
OfTPoint;
N: Longlnt;
Res: Real;
Ox, Oy: Array[ 1. .MaxN*2] Of Real;

.

4 (

)
, [1]
0 0 10.1 5.2 (

( ) )
, [2] -3 3 5.36 7
.
1 6 9 15
(X,Y) 8 3 20 8
[1]. ,
.
X 195.188
.
.
Procedure Solve;
Var i: Longlnt;
m: Real;
Begin
Sort (Ox, 1, N*2);{^
X .
, ,
. *}

5.

257

:=0;Res:=0;{* - , Res -
.*}
For i:~l To N*2 Do Begin
If i o l Then Res:=Res + Abs ((Ox[i] -Ox[i-1]) *m) ;
{* .*}
If (i=l) OrNot(Eq(Ox[i], Ox[i-1])) Then Calc
(Ox[i] , m) ; { *
. *}
End;
End;
Calc.
Procedure Calc(Const x: Real; Var rs: Real);
Var i, M: LongInt;{*M -
. *}
Begin
,
:=0>:
For i:=l To N Do {*
. *}
< ,
Y ,
Y ,.

. -
.>;
If M=0 Then rs:=0

Else Begin

,] ?

<

12

?1 f ?

10 1112 13


Y,

>;
< -
rs>;{*, 10.*}
End;
End;

.
3. ^ , .
. N (
2<TV<1000) Z
. V Xn, Yn, Xi2, Yi2,
9

3500

258

5.

.
. ( )
, , .
. .
3
0 0 10 10
, 5 5 15 15
, . ,
-1.5 0 7 8
: 6
X
Y. ,
.
Const MaxN=1000;
Eps=le-7 ;
Type Segm=Record L, R: Real; End;
SegmArray=Array [1. .MaxN] Of Segm;
Var
N: Integer;
OnX, OnY: SegmArray;
ObX, ObY: Segm;
Procedure
Init;
Begin
< >
End;
Function Cross (A,B:Segm;Var New:Segm) -.Boolean;
{^
.*}
Begin
If (B.L-A.L)<Eps Then New.L:=A.L Else New.L:=B.L;
If (A.R-B.R)<Eps Then New.R:=A.R Else New.R:=B.R;
Cross:=(New.L-New.R)<Eps;
End;
Function SolSegm(Const On: SegmArray;Var Ob:
Segm): Boolean;{*
,
. *}
Var
i ; Integer;
Begin
Ob:=On[l] ;
i:=2;
While (i<=N) And Cross(On[i], Ob, Ob) Do Inc(i);
SolSegm:=i>N;
End;

5.

259

Begin
Init; {* . *}
Write (' > ');
If SolSegm(OnX, ObX) And SolSegm (OnY, ObY)
Then WriteLnC ' ,Abs ( (ObX.L-ObX.R) *
(ObY.L-ObY.R))
:0:6)
Else NriteLn(* . *);
End.

5.7.
1. , - (1<NS5O).
(1<<50) .

.
.
-1 .
.
,
, . , .

, ,
. , >$ /< >, .
1.
AjAtfAtf.!, .
AtAN1AN_2- - , . ,
,
- S' (
(S'') ), , . . ,
S'/S' '=/.

260

5.

( ,
). ,
cAjAN, AJ^A^J , AtA2, ^^ . .
.
2. , . . , ,
.
N (N<100)
. . Yes No.
:
. (
). (max),


(min).
, max<min. , X, . .
.
, .
.
3. . R
, . N
, (
), (2, 2), ..., (xN, yN). i (l<i<N) Rr
, :
, ,
. . ,
;
, . . , -

5.

261

. ( 0 360),
, . , (R,0) 0, (0,) 90.

N (l<JV<50) R.
N (xt, yt, Rt),
. / .
1000.

Yes/No. (),
.
.
. . , -
. , , .
2 3 , 1, 2, 4, 5
. , , , () , . - .
4. .
.
5. (
).
;
, , ( ).
6. . , , .
7. ,
. .
, , :
10 3500

262

5.

, , ;
, ,
;
, .
. .
8.
. , .
9. N . ( ). .
.
10. N . k
Sj, S2, ..., Sk , .
11. . , .
12. N . .
13.
, ,
, , .
14. , .
15. N .
, .
16. . , .
17. ().
N . , , .
18. . TV .
,
.
19. .
2*N . ,
, .

5.

263

S
O(N ).
20. .
N . (
), .

. tQ=(x0,y0)
( ), . t0
Min{Max{(xrx0)2+(yry0)2}}.
21. . N .
,

.

.

, , , O(N*LogN).
22. Qt Q2 N
. .
. N
,
N+M .
23. Qt Q2 N
. .
. . t Qt
(
Qj). , t Q2 ( , O(N)).
t
Q2,
Qv Q2
t. , O(N), Q ; , Q2
.
. t

264

5.

Q2, Q2
, .
v Q2,
Q2. v
Q2 ,
t.
, . (
).
Qt . ,
t, .
24.
iV- Q.
. .
,
qt qi+1,
,
(ptqt+1) ,
(ptqt) . .
, (q^i+jP)
.
25. :
;
;
.
26.

.
[2].
. (,). .
(xi,j//), X (,) - (xlyyt) . ,
, .
, ,
{xi+rx^*{y-y^ - {yi+ry^)*{x-x), {xt,y^
, , (xi+1,yi+1)
, (,)
, .
, .

5.

265

27. N . , .
.
( X) .
X
.

6.

1. N (JV<32767). 1} N 1 0. , 7V=19 19 1 0 =
0
10011
. .
11001
10011 2 . i i ?
.
.
. , 19 00111
,
10011
1-24+1-23+1-22+0-21+0-2 = 28.
.
N, , , 000000001010 , . , , N
. ,
N (iV>32767). .
Procedure Solve;
Var i, j: Integer;
Begin
Res:=N; i:=0;
While ((N shr i)>l) Do Inc (i) ;{ *

N. *}
j :=i;{* *}
While j<>0 Do Begin
N:=(N shr 1) + (N And 1) shl i ; { *

. *}
If N>Res Then Res:=N;
Dec(j) ;
End;
End;
2. .

6.

267

, , ,

.

.
. N ,
(l<N<500). N 1 N. , [32768, 32767] .

, . .
.
.
,
d.in
d.out
4
3
,
0 0 30
3 4 1
-15 15 20
.
15 10 5
. 10 10 10
, .
. .
(Const MaxN=500;
Var Ax, Ay, Ar: Array[l.MaxN] Of Longlnt;).
i ?
(Ax[i]+Ar[i], Ay[i]), (Ax[i]-Ar[i], Ay[i]),
(Ax[i], Ay[i]+Ar[i]), (Ax[i], Ay[i]-Ar[i]) ;, i ) . ()
, .
.
, ,
Sc, Ls:Array[l..N] Of Integer; - ( / XV )
- :
Sc (3, 0, 1, 0, 1, 2) Ls (6, 0, 2, 0, 4, 5).
Sc ()
(k) .
WriteLn(mx+l) SayRes(k) (
SayRes(l))

268

6.

Procedure SayRes (:
Integer);
Begin
If Ls[k]<>0
Then SayRes(Ls[k]);
Write (k, ' ');
End;
:
4
4 5 6 1, . , ,
Sc Ls.
3. N (
.
). , 4
01

0 0
. 0 0
.
1 1
2 2
. N
(1<<500). N [-32767, 32767],
.
. , .
. ? ?
, . . X, Y, . .
N , . ,
, . ? , (1, 5, 1000). 5.
4. .
, ,
. , ,
2 3
1
. :
, . -

,
, . , :
4, 1, 2 3.

6.

269

,
, , .
,
.
. (a.in)
, 500.
.
. (a.out) in
1
a.out
, 1 1 01

a.in
.
4
:
out
1 1 4 1
, .
1 2 23
: 1 in

, 2 14
.
out
1 1 01
, .
1 2 20
2 1 14 3
0.
2 2 4 13
, 3 1 12 5
.
3 2 6 11
. 4 1 10 7
, , 4 2 8 9
.
, . .
Var n:Integer;
Function Out (q:Integer) -.Integer ;
Begin
If q<=n Then Out:=q Else Out:=0;
End;
Procedure Solve;
Var i,nn:Integer;
Begin
If n=l Then WriteLn ('1 1 0 1 ') ;
Else Begin nn:=n;
nn:=nn+(4-nn mod 4) ;{*
, 4. *}

270

6.

For i:=l To Div 2 Do Begin


If i Mod 2=1 Then WriteLn ( (i+1) Div 2,' 1 ',
Out (nn+l-i), ' ', Out (i)) {*
. *}
Else WriteLn (i Div 2, ' 2 ',Out(i), ' ',Out (nn+l-i) );
{* . *}
End;
End;
End;
5. ,
,
N ( N5).
.

200 .
:
( )
, ( , ,
). .
, . ,
. ,
. ( 1) (. ). 1 200 (
).
. (b.in)
N (1<7V<128)
- ,
(
).
.
: b.in
2
-,
1 1 5 3 15 1
-5 1
.
1 3

() .

,
3 3 13 3 13

6.

271

. (b.out)
-
. ,
. .
. . ( )
. f(N) N . 1)=1. , ,
6*(N-1). , f(N)=f(N-l)+6*(N-l),

1+3*(N-1)*N. N, 128, 48769 .


Var a: Array[1. .200] Of Word;
b : Array[1..50000] Of Byte;
i,n,k,t : Word;
f : Text;
Begin
Assign (f, 'b.in ') ;Reset (f) ; Read(f,n) ;n:=l+3* (n-1) *n;
For i := 1 To 200 Do a[i]:=i;
For i := 1 To n Do Read(f,b[i]);
While Not SeekEof (f) Do Begin
Read(f,k,t);a[k]:=t;
End;
Close (f) ;
Assign (f, 'b.out ');
Rewrite(f);
For i := 1 To n Write(f,a[b[i]]) ;{*
. *}
Close(f);
End.

, N 128, , 1000? 3 . ?
, .
.
6. . ,
. , ,
, . ,
.
. (c.in) . , -

272

6.

. 10000.
.

c.in
soundblaster
255 .
sound
.
sound
"ZZZZZZ" ( blaster
),
soundblaster
. ,
master
, last
task
.
sos
. {c.out)
test
, bonus
.
done
,
ZZZZZZ
c.out
.
sound
.
sound
blaster
ZZZZZZ.
soundblaster
. :
last
.
sos
bonus

done
.
ZZZZZZ
(, , ,.., z) .
, .
7. .
. N av 2,..., aN, bv b2,..., bN ,
. . . : al*a2*...*aNA, b1*b2*...*bN=B ; * 7 = 2 * 2 =
=...=aN*bN, alt 2
aN , a bv 2, .... bN
. 1 .
.
(d.in) , N.
Aw 2147483647, N 31.
. .
(d.out)
d.in: 4 16 2
av 2, ..., bv b2, ..., bN
d.out:
( ).
2 2
, 4 4
.
.
, h. h -

6.

273

h:=Round(Exp(ln((A*1.0)*B)/N )).
j , h /, . h
Div j. ;', Div j.
.
N /', . .
320, 2278125 N 6,
h, 30, (2, 2, 2, 2, 2, 10) (15, 15, 15, 15, 15, 3).
8. . . : ,
(1<<100) ,
. , ,
. . , , , . , ,
, , 5 /, . , . .
. (m.in) N . N+1 :
,
.
, ... m.in
3
( 1
200 100 200 200
100000). N
2 19
m.out
( )
55.5556
( 1 300).
. (m.out)
( ) , .
.
, .

.
. -

274

6.

, . ,
. ,
, () ,
, . . , . :
.
9. .
k (1<<500) .
N (l<iV<100000) . (N,
k ). , .
( 0
k-1), , :

;
.
. (t.in) k, N, N
( 0 100000).
. (t.out)
, , .
t.in
. 100
, .
5
, 0
, .
210
. 99
551
k.
99
0 k1
t.out
. 10

51
0 k-1
.
i

1 2

....

k-1

6.

275

, , .
Procedure
SolvePA;
Var i , j : LongInt;
sc, Min: Longlnt;
Begin
Min:=MaxLongInt;
For i:-0 To k-1 Do Begin{*
. *}
sc: =0 ; { *
. *}
For j:=0 To k-1 Do sc:=sc + (( - j + i) Mod ) * A[j] ;
If sc<Min Then Begin ResA:=i;Min:=sc;End;
{^. *}
End;
End;
10. . . -. . .
. , .
(
!).
( !). , ,
, ( ). . , , . ,
(
). .
. . ,
(. . ,
. , . ).
,
( ).
. (p.in) :

. ;
. (0<<20);

276

6.

,
;
N (0<iV<1000);
N , 3 : . , , . , .

.
p.in
. 70
0 5000.
2
40

50
. .
4
. (p.out)
60 1200 2
200 1120 1
, 1200 1440 2
p.out
.
2
. , . . .
, - i
, - *~

, , , . . i
1 1 ,
i. (), , .
()
, . , .
.
, , .
Procedure Solve;
Var i, j: Integer;
Begin
ResP:=0;(*. *}

6.

277

For i:=l N Do Begin {*


i . *}
Res[i]:=1;{*
, ""
. *}
For j:=l To i-1 Do{ *
i
. *}
If IfPossib(j,i)
Then Res[i]:=Max(Res[j]+1,Res[i]);
{* i j
,
- , Res [j] +1
Res[i]. *}
ResP:=Max(Res[i], ResP);{* ,
i, Res[i]
, ,
,
. *}
End
End;
,
IfPossib
i .
11. . [,) ,
. , 0
1.000.000.000 .
N (l<=Af<=100) . , , ( , ).

.
. (z.in)
iV iV (
Longlnt).
.
.
z.in
(z.out) 4
.
.
1Q
40 90
. -. 100 1000000000
z.out
.
15
, . -

278

6.

. 0 3 .
.

1
2
3


.
1

6
. 4
: l
2

5
n
, ,

6
. .,
. ,
.
12. . N (l<,N<500)
. . ,
.
. (o.in)
.
0. in
N . 3
JV
2 5
.
1 3
. (o.out) 4 6
(
o.out
Integer). 1 3
4 6
.
. .
500
, . , . : ,

,
. :
Const NMax=500;
Var
A:Array[0..NMax,1..2] Of Longlnt;
Procedure Solve;
Var i:Integer;
t:Longlnt;

6.

279

Begin
t:=-MaxLongInt;
For i:=1 To N Do
If A[i,l]>t Then Begin
Writeln(A[i,l],
'
',A[i,2]>;
t:=A[i,2};
End; { * t,

t
. *}
End;
.
13. . , 1 (), 2, ..., N (2<N<30).
( 1) (
N) .
, ,
:
, ,
;
;
V;
, , .
.
. (p.in)
p.in
V ( /).
.
.
N .
5
d2
dn, di 1111
(-1)
p.out
2.5
i ( ). ,
2.0
N, .
. (p.out) ( ,
) ( ).
. .
. .
(t) ( ).
t/2, .
- .

280

6.

, , (, , ' ). .
. , , : =1.0, =5.0, N=3
10.0 7.0. 22 . 11.
4 , 1
. , 11-1 10 .
14. N (10<2V<100) , 1
N. , . :
1 ;
2
, ;
3 ,
;
4
, ;+1(;>0), . . 1, 4, 7, ...
.
(1<<10000),
.
, , N .
. Input.txt :
N;
;
,
;
, .

, - 1 .
. Output.txt ( ),
. N
( , N-
JV- ), 0 , , 1
.

6.

281

.
, , Input.Txt
10
,
1
2. ,

7 -1
.
Output.Txt

. , 4
0000000000
, =10000 0110110110
0101010101
, . -,
,
1-2,
, . . 2-1 (). -, - . ,
4, 2. , , 4,
, 3, , 5, , 4.

<4, 15 (1; 2; 3; 4; 1,2; 1,3; 1,4; 2,3; 2,4; 3,4; 1,2,3;
1,2,4; 1,3,4; 2,3,4; 1,2,3,4), , , 4 .
, , , , 3, , , 1.
15. . , ()
.
, ,
, .
.

.
,
. ,
.
II

3500

282

6.

, ,
, ,
. 1,
, 0 .
,
.
;
.
,
.
. starry.in
W , . ,
W .
. starry.out
, starry.in,
, .
: 0< )<100, 0<
)< 100, 0< <500, 0<
<26(..), 1< <160.

starrydn
23 "
15
1 0 0 0
1 1 1

1 0 0
1 1 0

0
0

0 0 0 0 0 0

1 0
1 0

1 0
0

0
0

0
0

0
1 0

1 0
0

0 0
1 0

0
0

0
0
0
0
0
0

0
0
0
0
0

0
0

0 0
0 1
1 0
0 0
0 0

0
0

0 0 0
1 1 1 0

0 0
1 0
1 0
0

1 0

1 1

0 0
1 0

1 0 0 0
1 1 1 1

1 0
1 0

1 0
0

0 0 0 0 0 0
1 0 1 1 0 1
1 1 1 1 1 1

1 0
1 0

0
0

1 0

0
0

0
0

0
0

0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 1 0 0 1 1 1 1
0 1 1 1 0 1 0 1 0 1 0 1 0 0 0 1
0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0
0 1 1 1 0 1 1 1 1 1 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0
0

0 1
0 0
0 0
1 0 0
0 0 0

1 0

0 1 0 0
1 1 1 0

0
0

0
0

1 0
1 0

1 0 0 1 0
1 1 1 0 0

0
0
1
0
0
0
0
1
0

283

6.

starry.out
0 0 0 0 0
a a a 0
0 a 0 0 0 0 0
_0j 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0
b 0 0 0 0 0 0
0 0 b 0 f 0 0
0 0 0 0 f 0 0
0 0 0 0 0 0 0
0 0 0 0 0 b 0
0 0 0 g 0 0 0
0 0 g 0 0 0 0
0 0 0 0 b 0 0
0 0 0 0 0 0 0

~(

0 0 0
0 0
0 0
0 0
0
0
0 0
0 0 0
0 0 0
d d d
0 d d
d d d
d d d
0 d 0
d d d

0 0

0 0
0 b
0 0

0 0
0
0
0
0
0
0 0
0 0
0 0

0 0

0
0
0

0 0

0 0
0 b
0 0

0 0
0 f
0 f

b
0
0
0
0
0
0

0
0
0
0
0
0

0
0

0
0

0
0

0
0

0
0
0
0
0
0
0

0
0

0
0
0
0
0
0
0

0
d
d
0
0
0
0
0
0

0
0
d
0
0
0
0

0
0
0
0

0
d
d
0
0
0
0
0

0
0
0
0
0

0
d
d
0
0
0
0
0

0
0
0
0

0
0
d
0
0
0
0
0

0
0
0
0
0

0
d
d
0
0
0
0
0

0
0
0
0
b
0


.
. (:ray[O..MaxN+l,0..MaxN+l] Of Byte; MaxN=100, ). 1, ,
(ASCII) . .
For i :=1 N Do
For j : =1 Do
If A[i,j]=l
Then Begin
, (i,j), . Work Star
( ) d ( j)>,~
< .
.
- ASCII .
, >;
< ASCII>;
End;
. 100*100. -

284

6.

, . . 10000. ,
26, 260000 .
. , . 8 32500 ,
.

.
Const MaxN=100; {* .*}
MxCh=26; {* . *}
StArt=97;{*ASCII . *}
Dx: Array [I. .8] Of Integer-( 0, 0, 1, 1,
1,-1,-1,-1); {*,

. *}
Dy: Array[1. .8] Of Integer=( 1,-1, 1,
0,-1, 1, 0,-1);
Type MasSt=Array[0. .MaxN+l, 0. . (MaxN+8) Div 8] Of
Byte;{*

,
. *}
Star=Record A: MasSt; N, M: Integer; End;
{*0 . *}
Var A: Array [ 0. .MaxN+l, 0..MaxN+l] Of Byte ;
{* . *}
N, M: Integer;{* .*}
Rs: Array[1..MxCh] Of Star;{*
. *}
Sc:
Integer;{* . *}
Solve .
Procedure Solve;
Var i , j , d : Integer;
Work_Star: Star;
c: Byte;
Begin
For i:=l To N Do
For j:=1 To M Do
If A[i, j]=l Then Begin
d:=GetSt(i,j,Work_Star);{*

.
d -
.
,
,

6.

285

.
,
. *}
c:=Check(Work_Star);{*
. *}
SetUp (i,j-d,c,Work_Star);{*

. *}
End;
End;

. .
16. .
.
. . . ,
S
, .
input.txt
1 ,..., , , 5
() 1,..., S.
1
1,...,

.
2

,
3

.
2
,


2
{, , , , }. S

data.txt
S .

,


,

input.txt

, data.txt

, . input.txt


(1<<100).

. output.txt
L (l<L<20),
11

286

6.

( '' 'Z') L. . data.txt


. data.txt '.' () . 1 500 000.
. output.txt , .
. Longlut .
: ?
,
. ? -
t.
, t-ll\\
, - ,
, . t1
,
. , , . , , . ?
,
.
MaxN
(Array[l..M,0..MaxN] of Integer). M? , .
.
: ( , );
( );
( ).
17. ,
( ).
S. ,

6.

287


.
, .
( /
) .
.
S (l<S<80). ,
500 . S.
.
.
.
input.txt
10
aaa-bbb-bbbb

10
aa-bb---bb

10
aaaa-bbbb-bbbb

13
----

output.txt
15
-aaa--bbb-bbb
b-aaaa-ccc-ccc
8
-aa-bb-bb-aa-c
--

6
aaaa-bbbb-bbb
b-aaaa-cccc-cc
35
----
13
5
-----
----
13
59
----
--
-
----

( - ).
. , , , ( ) .

288

6.

q ,
w. S . (d), (q+1), ,

(S-w) q+1.
(m;=(Sw) Mod (q+1))
.
18. *.
, . , .
. ,
.
, . .
, , . . .
,
.

.
, N W . ,
, N/(W-1), , . .
\N/(W-1)\ ,

[N/(W-1)].

. 10
1, 2, 3, 4 .
, 2 3 3 4 4 2 1 1 3 1 .
3, , :
2 1 1 4 4 2 3 3 3 1 ,
2 1 1 2 4 3 3 3 4 1 ,
1 1 1 2 2 3 3 3 4 4 .
car.in.
. N,
2<<20000. , 2<<50,
1 .
2000 .

6.

289

. W, 2<W<M.
N , i-e i- , .
car.out.
R . R
, +1 i- . , , .
, .
,
. , , ,
. 1 iV .
, .
,
car.in
.
10 4 4
. , 2334421131
, car.out
, R, a \N/(W-1)~] = Q. -
3
427387283
R
3 4 9 9 6 6 4

,
3 1 5 5 10 10 1
0 .
:
R<Q
100%
R=Q+1
50%
R=Q+2
20%
R>=Q+3
0%
.
. . Q 100%- .
W-1 ,
. W , W-1 ,
.
, ,
, , , ;
( )
,

290

6.

. ,
, , . .

,
, , , .

.

1
2
3

1
2
2
1
1

2
3
3
3
1

3
3
3
3
1

4
4
2
2
2

5
6
4
2
4
3
2
3
2
3

7
1
1
1
3

8
1
1
1
3

9
3
4
4
4

10
1
1
4
4

( car.out):
4
4 1 4 4 9 9 6 6 1
4 1 5 5 10 10 1 2 2
4 2 7 7 2 3 8 8 3
111
,
.
19. *. N , 1 N. , N . ,
, , 1<Y</V.
X, , , X, , ,
X. ,
. , ,
,
.
. device
:
GetN, ;
; N;
2000 .

6.

291

Med3, ; ,
() ;
Answer, ;
X,
.
.
device median.out median.log. median.out
: ,
Answer. : Med3, . median.log.
Pascal: uses device; .
.
, device.in. . : N. 1 N
: i-e i.

device.in
5
2 5 4 3 1
device.in, , 5
:

1
2

2
5

3
4

4
3

5
1

:
1. GetN 5.
2. Med3(l,2,3) 3.
3. Med3(3,4,l) 4.
4. Med3(4,2,5) 4.
5. Answer(4)
:
N , 5<iV<1499;
i l<i<iV;
Y 1<Y<N,
;

292

6.

Pascal : device.tpu
Pascal:
Function GetN-.Integer;
Function
Med3(x,y,z:Integer):Integer;
Procedure Answer(m:Integer);

7777 Med3;
-
.
. .
1

2
1

3
3

4
7

5
5

6
4

7
13

8
10

9
2

10
11

11
12

12
9

13
6

, Med3,
,
( ) . ,
,
. =5 =12. ,
True.
True.
Med3(a,b,i) :
, i ,
( _Left);
, i ,
( _Middle);
, i ,
( _Right).
L, M, R _Left,
_Midlle, _Right. N _None,
.

10

11

12
N

13

, , ?
( ).
. ?
4 , 4 ( ) 3. 7

6.

293

13 .
,
_Middle. : , ; .

( ). .

Med3(an,bn,ac)obn To ...
, ( ) . (
an, ). (Med3(an,bn,ac))
,
. an . ,
,
, .
.
1.
. .
2. Med3
.
*.
Program Median;
Uses device;
Const MaxN = 1500;
Type TDir = (_None, _Middle, _Leftj __Right) ;
Var n: Integer;
Mas : Array [L.MaxN] Of TDir;
. .

294

6.

Lt, Rt : Integer; (*
*)
Procedure MakeMiddle(Dir: TDir);{^
, . *}
Var i: Integer;
Begin
For i := 1 To N Do
If Mas[i] = Dir Then Mas [i] : = _Middle Else
Mas [i]:= _None;
End;
Procedure Swap (Var a,b: Integer);
Var c: Integer;
Begin := a; a := b; b := c: End;
Procedure GetNext(Var a,b: Integer; len: Integer);
{* b. *}
Var
i,t,ca,cb:
Integer;
Begin
t := 0;
ca := Random (len)+1;
cb := Random(len)+1; While ca=cb Do cb ;=
Random (len)+1;

For i : = 1 To N Do {^ ,

. *}
If Mas [i] = _Middle Then Begin
inc(t) ; If ca = t Then a := i; If cb = t Then
b := i;
End;
End;
Procedure Doit(Dir: TDir; res, len, 1b: Integer);
{* ,
. *}
Var a, b, i, t: Integer;
Begin
MakeMiddle(Dir) ; {* ,
. *}
If len = 1 Then Begin a := 1; While Mas [a] <>
_Middle Do inc(a);
Answer(a);
End
Else Begin
GetNext(a,b,len);

6.

295

If (Dir=_Right) Then Begin If Med3 (lb,a,b) <>


a Then Swap (a,b) End
Else If Med3 (a,b,lb) <> b Then Swap(a,b) ;
{* b
.
. *}
If len = 2 Then Begin If res = 1 Then Answer (a)
Else Answer (b) End
Else Begin
It := 0; rt := 0;
Mas[a] := _None; Mas[b] := _None;
For i := 1 To n Do
If Mas [i] = JMiddle Then Begin
t := Med3(a,b,i) ;
If t = b Then Begin inc(rt) ; Mas[i] : =
_Right End{* . *}
Else If t = a Then Begin inc(lt) ; Mas[i]
:= _Left End;
End;
If Res = lt+1 Then Answer (a) Else {*
,
. *}
If Res = len-rt Then Answer (b) Else
If res < lt+1 Then Doit (_Left, res, It ,a)
{* . *}
Else If res < len-rt Then Doit (_Middle,
res-It-1,len-lt-rt-2,b)
Else Doit(_Right,res-len+Rt,Rt,b);
End;
End;
End;
Procedure SolverBegin
n ;= GetN; Randomize;
FillChar(Mas, SizeOf(Mas) , Jiiddle) ;
Doit (_Middle,n shr 1+1, n, 1);{* , n
shr 1+1 - , 1 ,
( ). *}
End;
Begin Solve End.

6.

296

20. * -
. (
) 4 .
( ), . , , ,
4 . ,

.
, 0 1 ,
.

5 *
..:.:
;
v
.4sii

'ft '

0
0
0
0
0
0
0

0
0
0
0
0
0
0

0
0
0
0
0
1
1

0
0
0
0
1
1
1

0
0
1
1
1
1
1

0
0
1
1
1
1
0

0
0
1
1
1
0
0

.'.J

7 8 9 10

0
0
1
1
1
0
0

12 1 3

1|
1
|7

15 16 17 18

, , , , , .
.
.
. 4
, ,
. , . ,
, .

6.

297

, . , 2 ,
, (
, ).
. -
-, ( ,
, , ).
: , 1 4 , (
1, 2 , 3,
4 ). 5,
10.
, 4 -, -. 32 5 ( 5) 17 1 0 ( 10).
12 -, -, 43 5 23 1 0 . 15 -, -,
-, 134 5 =44 1 0 . : 9 14 17 22 23 44 63 69 88 94 113.


.
. . , , .
, \\ (\\ < 64). 0, \\ \\ , 0 1. 1 . 0,
, - 1 .
0.
( ).
, 0.
.
. ,
0 1,
.
. ,
12 , 12 . -

298

6.

. , ,
, '.' 0, '*' 1.
.

8
00000000
00000000
00001111
00001 1 1 1
00011111
00111111
00111100
00111000
-8
9 14 17 22 23 44 63 69 88 94 113-1
2
00
00
-4
0-1


1
9 14 17 22 23 44 63 69 88 94 113
11
2

****
****
*****
****
***
3
0
4
****
****
****
****

.
.
.
Begin
Readln(N);{*
While NO0 Do Begin

*}

If N>0 Then SolveA Else SolveB;{*


, . *}
End;
End.

6.

299

, . ,
,
, . 1 4. RecA,
Function RecA(i,j,d:Integer;Var Way.String ):Boolean.
True , . , . (Cp:Array[l..MaxCn] Of
Longlnt, , 64*64),
Cnt. ,
A (A:Array[l..MaxN,l..MaxN] Of Boolean,
MaxN , 64).
A[i,j], True, (,/), False . , SolveA.
Procedure
SolveA;
Var i :Integer;
Begin
ReadA;{*
, . *}
Cnt:=0;
If RecA(l,l,N, ' ') Then Begin Cnt:=l; Cp[Cnt] :=0;
End;{*
' ,

,

RecA. *}
Sort;{* .*}
Print ; { ^
OutPut. *}
End;
RecA .
Function
RecA(i,j,d:Integer;Way:String):Boolean;
Var k:Integer;
c:Boolean;
Begin
If d=l Then c:=A[i,j] {*
,
. *}

300

6.

Else Begin
:= d Div 2;
c:=RecA(i,j,k, 4 '+Way) And RecA(i,j+k,k, '2'+Way)
And RecA(i+k,j,d, ''+Way) And RecA(i+k,j+k,d, '4'+Way);
(*

. *}
If Then Dec (Cnt, 4) ; { *
,
. *}
End;
If Then Begin
Inc(Cnt);{* ,
(,
)
. *}
[Cnt]:=< ,
,
>;
End;
RecA:=c;
End;
. Sort
PrintA ( SolveA)
, . , , .
RecB.
, ,
. , , . .
, . .

? , RecB,
ij+r
SolveB. .
i+rj+r
i j,
i+rj
.
k,

301

6.

k~Way Mod
51. , k
i+r*(k Div 2) j+r*(k Mod 2)
. .
RecB
1
2
3

i
1
1
3

j
1
5
5

d
8
4
2

Way
32 5 17 1 0
3
0

1
2
-

4
2
-

.
21. , ( ).
, (+), (*), (-) ,
- .
, ,
,
, , . . , . , (
), .
,
.
. (input.txt)
N
. N
: , , .
[0, 10 9 ], . 250 .
.
. (output.txt) .

,
. ,

302

6.

.
input.txt
: , 3

; .
0 30
15

b 10
15 20

(A-(a+b))+(a*b)
, output.txt
[0;10)
{15}
.
.
(20;30]
, . , [0, 10, 15, 15,
20, 30] ( ). -
, (0, 10), ,
(
(0, 10) , ).
,
. : [0, 5, 10, 12.5, 15, 15,
15, 17.5, 20, 25, 30]. N '
N*41. ? , . (+) (
Or ), (-) (A And Not ) (*) (A And ).

- :
<>=<>{<><>}
<>=+|-|*
<>=(<>)|<>
<>=||.. z|A|B|C. .Z
. ( ) .
Const MaxN~52;{* ,
26 .*}
Var
:Array[1..MaxN*2] Of Longlnt;{*
. *}
N:Integer;{*
. *}
Numb:Integer;{* ,

. *}
,
N*4 1, ,
. ,
.

6.

303

Function
Get(:Integer):Real;
Begin
If Mod 2=0 Then Get: = (OnX[k Div 2]*1.0+OnX
[k Div 2+1])/2
Else Get:=OnX[k Div 2+1] ;
End;

1
2
3
4
5
6
7
8
9
10
11

Get(k)
0
(0*1.0+10)/2=5
10
(10*1.0+15)/2=12.5
15
(15*1.0+15)/2=15
15
(15*1.0+20)/2=17.5
20
(20*1.0+30)/2=25
30

Atln(Get(k))
True
True
False
False
True
True
True
False
False
True
True

Get 2- .
Atln ,
. :
Function
Atln
(x:Real)
-.Boolean;
Begin
Numb:=0/{*
. *}
AtIn:=Expr(x);(*
(
) - ,

NriteRes. *}
. End;
,
WriteRes.
Procedure NriteRes;
Var a,b:Integer;
i .-Integer;
Begin
i : =1 ;

304

6.

While

(i<=N*4-l) And (Not Atln (Get (i))) Do Inc(i);


{* ' ,
,
. *}
While i<=N*4-l Do Begin
a:=i; (*
. *}
While
(i<=N*4-l) And Atln (Get (i)) Dolnc(i);
{* ,
. *}
b:=i;{* . *}
SayRes (a,b);{* .
. *}
Inc(i) ;
While (i<=N*4-l) And (Not Atln (Get (i))) Dolnc(i);
( *
, ,
. *}
End;
End;
. SayRes
,
.
22. , .
N (0<N<20)
(0<<<12) , .
. input.txt . , , iV, 0
1, 2.

2- .
. output.txt
N , N
. :
...
,

. -

305

6.

.
.
input.txt
2
4
.
10
N 010100100100010001111
,
0110000101
N .
001100111100001001001
1110010000
. 0002
.
output.txt
. 23 00

15 10 01
, . 8191
12 100
1
2
3
12
13
(2 +2 +2 +...+2 =2 -1=8191)
11 001 000 11
10 010
.
8 0100
(D) 7 1001 0010
6
0000 111
.
5 1000 110 011
4 1100 0001
, D.
10
, (
1). , D, ,
.
L

0
1
2
3
4
5
6
7
8
9
10
11
12
13

0
1
00
01
10
11
000
001
010
011
100
101
110
111

1
1

3
2

5
3

6
4

8
5

2
3

4
3

2
3

1
1

10
4

1
1

9
6

D
39
26
23
15
15
11
11
11
10
5
12
3
5
6

306
L
4

6
N
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

12 4094

1
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
00000

10

1
2

1
1

1
1

D
6
4
7
4
8
2
2
3
5
7
2
1
4
1
3
3

0...0

, D, N . .
D, ,
.
Const Rt:Array[0. .13] Of Integer=(0, 0, 2, 6, 14,
30, 62, 126, 254, 510, 1022, 2046, 4094,
8190);{*Rt[i] ,

i . *}
=12;
MaxN=21;
Var
D: Array[0..8190] Of Longlnt;
A, B, N: Integer;
, , .
? Sold -

6.

307

, (0, 1, 1, 5, 5).

0101, '0' ( 4, 5 Sold). (
Rt)? Sold ,
,
D. ,
,
' 1 ' , . 2, . . '01' (
). 3 , . . '101'.
4 ,
. . '0101'.
. :
1 1- , 0 (0+1=1 "1' D);
2 1- (2+1=3 '01' D);
3 5 (6+5=11 '101' );
4 5- (14+5=19 '0101' ).
'0' :
1 - 0*2+0, 0- ;
2 1*2+0, '10' 2-
;
3 - /*2+0, '010' 2-
;
4 5*2+0, '1010'
10-
Sold : (0, 0, 2, 2, 10). '0'.
1 - 0*2+0, 0- ;
2 - 0*2+0, 0- ;
3 - 2*2+0, 4- ;
4 2*2+0, 4- .
Sold (0, 0, 0, 4, 4). '0'
'00' .
'100' '0100' . .
, ?

308

6.


Sold 10 ,
.

1
2
3
4
5
6
7
8
9
10

0
1
0
1
0
0
1
0
0
1

Sold
0,0, -1, -1, -1
0,1, 1,-1,-1
0, 0, 2, 2, -1
0, 1, 1,5,5
0,0,2, 2, 10
0, 0, 0, 4, 4
0, 1, 1, 1,9
0, 0, 2, 2, 2
0, 0, 0, 4, 4
0, 1, 1, 1,9

Procedure
Sobve;
Var: nch: Char;
Sold,Nw:Array[0..MaxB]
Of
Integer;
i:Integer;
Begin
FillChar(Nw, SizeOf(Nw) , 0) ;
For i:=l To MaxB Do Sold[i] :=-l ;Sold[0] : =0;
Read (nch) ;
While nch<>'2' Do Begin{*
. *}
For i:=l To Do Begin
If Sold[i-1]<>-1 Then Begin
Nw[i] :=Sold[i-l] *2 + Ord(nch) -Ord (' 0 ') ;
If (i>=A) Then Inc(D[Rt[i] + Nw[i]]);
{*
D.*}
End
Else
Nw[i]:=-l;
End;
Sold: =Nw; { *3

.*}
Read(nch);{* . *}
End;
End;
. , . .

6.

309

Procedure

SayW(num:
Integer);{*
. *}
Var i , j : Integer;
s:
String;
Begin
i:=MaxB;
While (i>0) And (Rt[i]>num) DoDec(i);
{* . *}
num:=num - Rt [i]; s:=' ' ;
For j:=0 To i-1 Do Begin
s:=Chr(num Mod 2 + Ord('O')) + s;
num:= num Div 2;
End;
Write ('
', s);
End;
23. () .
NOT, AND OR.
: OR(a,b)=NOT(AND(NOT(a),NOT(b))).
, N
(<1995), a, b,c,vid N :
d , ;
( ).
1. =6, =011000, =111010, =010001
d= 100010 :
;
AND(NOT(a),OR(b,a)).
2. /=6, =0110000, =111010, =010001
d=000010, .
.
, , .
,
, , - . .
:
=1 1 1 1 1 1 1 1
=1010 1010
=0 1 0 1 0 1 0 1
2
d=01010101

01010101
00110011
00001111
8
10100101

10101010
00001111
11110000
4
11011010

310

6.

,
. . , , ( ) ,
d,

.
, ,
d. , ,
(, , d)

. ? 8 256. . ,
, , 256 . , (NOT, AND OR),
. . , , , .
Const Max=256;
Ch=Array[l. .3] Of Char= ('a' ,'b','') ;
Type Action=Record
operation: 0.. 3;(*0 - ; 1 NOT; 2 - OR; 3 - AND. *}
opl,op2:Word;{* -
.*}
r e s u l t -.Byte; {^ . *}
End;
Var A: Array[0. .Max] Of Action;
cnt,oldent:Word;{*

.*}
ok
-.Boolean;

operation, opl, 2 , [0].resulted, A[l ].result=a, A[2].result^ A[3].result=c.

. () , ,
d.

6.
Procedure
Out
(p:Nord)/
Begin
With A[p]
Do
Case
operation
Of
0:
Write(Ch[p])/
1 :
Begin
Write('
NOT
')/Out(opl)/End;
2:
Begin
Write (' C)/Out (opl) /Write ('
OR
Out (op2) /Write (')
') /End/
3:
Begin
Write ('(') /Out (opl) /Write ('
AND
Out (op2) /Write (')
') /End/
End/
End/

311

')/
');

.
Begin
< >;(* - 4
,
. *}
If < > Then < >
Else Begin
ent:=3/ok:=False/
Repeat
oldcnt:=cnt/
<>/
Until (oldcnt=cnt) Or ok/
End/
End;
24. N (l<iV<100),
0 1 ( )*.
. ,
, , . . . , 11010100
,
.
input.txt
3- 6- . 9
11101000. , 001011001
.
output.txt
yes
, :
6 9
3 8
;
1 5
.

312

6.

, .
input.txt.
N. . .
.
output.txt. , .
,
yes, . i, j
( ), ,
, i, i+1,.... j.
.
. W S (SczW), , teW
reS.
, . S,
S, , .
S? 11...100...0100...,
, ,
( 0) . , ,
, .
() i ;
.
?
9

1
(
1
1
1111000010
5
4

2
2
11111001
6
0
, 1
0000001000
3
3
6
4
2
10010001100
4
5
). , 2
5
11000100100
4
5
,
4
2
6
11100100000
5

7
2
0100101101
5
3

2
8
1001001101
5
3
. 9
2
1100100101
5
3
.
10
2
1110010001
5
3

2
11
1111000100
?>>
5
3
.

6.

313

,
(4- )
(5- ) ( 4-6
7-11).
( )
cnt:=0;
chl:=0;ch2:=0;
For i:=2 To Length (S) Do
If S[i]='O' Then If cnt Mod 2=0 Then Inc(chl) Else
Inc(ch2)
Else Inc (cnt) ;
. , 1, 5 (5 ), 4
(4 )
0000010000.
. : ,(1 / *
i , 1 . . s ,
.
,
. , (x+y)/2=s, . .
y=2sx, ai>2s-ai. ,
l
l k
H(~l ) *ai=Y^(~l) ' (2saj.
1
. , . , 0101 (-1) *2
+(-1) 2 *4=2, 1010,
(-1) 1 *1+(-1) 2 *3=2. , .
(110100 110001) -3 - 5 ,
.
l<iV<60.
, .
input.txt.
.
\

JV. ,
.
output.txt
.
I2

3500

input.txt
9
101010011

output.txt
30

314

6.

, .
. 0 1
. , 0 ( ) .
. 101010010. 2, 3.
,
, 3, 2.
lOlOlOOlO. : lOllOOlOO,
lOlOOlOOl. OOlOllOOl. OlQlOlOOl. OlOllOOlO (
, ,
). :
1 0 l 0 l 0 0 l 0 , llOlOOOlO, lOOlOlOlO, lOOOlOllO.
,
, . 6, 4. 24. , , , , :
(-l) 1 *H-(-l) 2 *4+(-l) 3 *6+(-l) 4 *8=-l+4-6+8=5
(-1) 1 *1+(-1) 2 *5+(-) 3 *7+(-1) 4 *8=-1+5-7+8=5.
, . ,
Find, k I
, Longlnt. t ,
. .
1 I. (+1)- t+1 , 1 t
( ). , ,
.
Function
Find(,1:Integer):
Longlnt;
Var
Q:Array[l..2,0..MaxN]
Of
Longlnt;
i, j
:Integer;
Begin
FillChar (Q,SizeOf (Q) ,0) ;
For j:=0 To 1 Do Q[l,j] :=1;
For i:=l To k-1 Do Begin Q[2,0] :=1 ;
For j:=l To 1 Do Q[2, j] :=Q[1, j]+Q[2, j-1 ] ;
:=Q[2];

315

6.
End;
Find:=Q[1,1];
End;

25.
. , .
q. N, , ,
q *.
, , . . . . ,
N>max(p,q). , q=lp vinvip=tq ( ) . ,
q=lp av a2, ..., aN.
q
X1

__
X1

i=l

i=l

2
V

1*_
X1

l=pA

i=(l-\ypA

/ U- / (X. - / .-..+

rv

/ CL. >U

.
: q>p ( ); , . .
(,7)=1. . ,
,
. q ( ).

1
2
3

5
5
5

12


6"=1*5 + +
1=2*5 + +
7"=1*5++2"
5 + =2*2"+1 +
+
12"=2*5
+2"
5+=2*2~+1+
8"=1*5++3"+
5 + =1*3>2
3"=2++

+-|
?
+
+
?

III | ?

L
9
14
10

-2,9
-3, 13
-7,5

+--(--++--1+-1 +-+?

15

-10,7

-++-+-|-+-?

11

-5,8

316

6.

q
13

14


+
13~=2*5 +3~
5 + =1*3"+2 +
+
3 =1*2 +1
+
9~=1*5 +4~
+
+
5 =4"+1
+
14"=2*5 +4"
+
+
5 =1*4"+1

-++-++-+-1 -+-?

L
16

-7, 11

+++++-1 +++?

12

-11,3

+++-++++-++++-1 +++?

17

-15,4

q, =.
- ( ). . . + . , .
| . ? ( )
. ,
?, , +.
. + . ?
> ( 2
). .
( ). , .
L L=(p+q-2) (,<7)=1. (
) , . ,
, ,
. ,
L>p+q2. ,
,
, L<p+q-2.
, (!)
+ . ,
( ).
, q +
- .
wt + tt, -
+ (tr-wj/w^ , .

6.

317

: (t-wi)/wi<(ti+1-wi+1)/wi+1 i >
i. . ?
tj=q, t2=p i=\. (q-w1)/w1>(p-w2)/w2
(i ).
,
,
.
.
(q-w1)/w1>(q+p-w1-w2)/(w1+w2)>(p-w2)/w2.
+ x=p+q-w1-w2 -
y=(u>1+w2). ,
, , w2*x+(p-w2)*y>0, a
q w^x+fq-WjJ^yKO.

. KOmp,q)=d>l? p/cl q/d
.
,
(d-1) . , (jp/d+q/d-2)*d+d-l,
p+q-d-1.

7.


. ( 80- ) .

, . ,
,
. (
) .
, , , .
, ( ) .
. , . , - ,
, . ,
, ( ), . , , ,
, , , . , .
. .
. , (
) -

7.

319

, ,
.
( , ). ,
, ... .
. [12] . , , , , (
) .
. , , ,
,
. .

7.1.

, , . , , , .
.
, . ,
- , , . , :
, . . ,
, ?.
. , ,
.
, , , ,
, , -

320

7.


. , : (
) . , ? , ,
, .

7.2.
. .
. , , , ,
, ,
( ) .
. . ,
. ( ).
. . : , ; ,
, , , ; , ,
. , , ,
.
. , .
1. , ()
(

7.

321

). , .
While < 1> Do Begin
If <

2>

Then

Exit;

End;
.
While True Do Begin
If < 2>

Then

Exit;

End;
2. , , ,
For, ,
, .
For i:=l To N Begin
If

<>

Then

i:=N+l;

End;
3.
.
Type BArray=Array[l..N]
Of Byte;
Procedure
Swap (Var
B:BArray;i,k:Byte);
Var
x:Byte;
Begin
x: =B [i ] ;B [i ] : =B [k] ;B [k] : =x;
End;
Swap(B,i,k) :
Procedure
Swap(Var
,z:Byte);
Var
x:Byte;
Begin
x: =y ;y: =z ; z : =x;
End;
Swap(B[i],B[k]).

322

7.

4. ,

() .
Procedure Generate;(*t - *}
Begin
t:=t+l;
Generate;
t:=t-l;
End;
( ,
).
Procedure
Rec(t:Byte);
Begin
Rec(t+1) ;
End;
5. , ,
, . , . .
Program
Var i,j:Integer;
Procedure A;
Begin
< i, j>;
End;
Procedure B;
Begin
< i, j>;
End;
{**}
Begin
< i, j>
End.

7.

323

().
. N. t~O(N), t~O(N4),
N
q 2 3, t~O(q ).
. , , ( ). N, 50,
, , .
:
? , $40:$6 ( Timer)
1/18.2 (
55 ). ,
5 ( 77meTest). $40:$6 , TimeOld.

Timer-TimeOld>18.2*TimeTest. , .
( ,
),
,
Timer-TimeOld>18.2*(TimeTest-0.5).
, , .
Program TimeCheck;
Uses Crt;
Const N=5;
Var Timer:LongInt Absolute $40:$6C;
TimeOld, TimeTest,L, i -.Longlnt;
pp:Boolean;
Begin
ClrScr;
For i:=l To N Do Begin
TimeOId:=Timer;( .}
pp:=True;
TimeTest:=i;{* . *}
L:=0;

324

^ ^

7 .

While pp Do Begin {* ,
. *}
Inc (L) ; { *~ . *}
If Timer-TimeOld>18. 2*(TimeTest{-0.5})
Then
Begin
pp:=False;
WriteLn(L);
End;
End;
End;
End.
, . , . (1) .
, (). (, - )
. ,
, (

). (2)
( )
. ,
.
, . (3) .
, ,
.
Ctrl+0+O :
{$ 16384,0,655360}.
, , R R+
. ,
, . . ,
.
, ( -

7.

325

).
.
. , ,

.
.
.

, . ,

. ,
, , :
;
(,
); ( Integer,
32767);
, , ,
( ).
.
.
. ,
. -, ,
. .
, . ,
.
, .
Program add;
Var a,b : Integer;
Begin
Assign
(input,'input.txt');
Reset
(input);
ReadLn (a,b);
Close (input);
While a=10 Do;
If a = 20 Then b : = Round (a/0) ;
Assign(output,'output.txt');
Rewrite(output);
NriteLn(a+b);

326

7.

Close(output);
End.
.
( i.tst, i ) ( i.ans, i ).

I.tst
2.tst
3.tst
4.tst
5.tst

25
45 45
35
10 10
20 0

I.ans
2.ans
3.ans
4.ans
5.ans

7
91
8
20
20

input.txt

. .
.
Checker.dpr ( ), ,
Delphi. Checker.exe. (
) : (
,
, .); ;
( ). Checker.exe
. 5 2 (5 , 2 ). .
, Checker, .
program* Checker;
{$apptype console}
-{* .*}
uses
Windows,SysUtils,Classes;
var SInf
: StartUpInfo; {*
CreateProcess. *}
PInf
: Process_Information;{*
. *}
TickCount
: Longint;{* .*}
YourAnswer
: TStringList;{*
. *}
PightAnswer
:
TStringList;{* .*}
4- .

7.
FileName
Feedback
TestCount
TimeLimit
IsTimeLimit
ExitCode
i
begin
FileName

327

: String;{*
.*}
: String;{* .*}
: Integer;{* .*}
: Integer;{*
.* }
: Boolean;{*
. *}
: Cardinal;{*
. *}
-.Integer;

:= ParamStr (1) ; { * -
. *}
TestCount
:= StrToInt (ParamStr(2));{* .*}
TimeLimit
:= StrToInt(ParamStr(3));{* ~
. *}
for i:=l to TestCount do begin
RenameFile(IntToStr(i)+'.
tst','input,
txt');
{* input.txt. *}
FillChar(SInf,SizeOf(SInf),0);
SInf.cb
:= SizeOf(PInf);{*
. *}
Slnf.dwFlags
:= STARTF_USESHOWWINDOW;
StartUpInfo
SInf .wShowWindow ;= sw__Hide; { *
. *}
CreateProcess
(PChar(FileName),nil,nil,nil,
false,{*3
. *}
CREATE_NEW_CONSOLE or NORMAL_PRIORITY__CLASS,
nil,
nil,SInf,PInf)
;
TickCount
:= GetTickCount;{*3 .*}
IsTimeLimit
:= true;
While ((TickCount+TimeLimit*1000) >= GetTickCount)
and IsTimeLimit do
if
WaitForSingleObject(PInf.hProcess,0)<>
WAIT_TIMEOUT then{*,
-. *}
IsTimeLimit
:= false;
if IsTimeLimit then begin {* -,
. *}
TerminateProcess(PInf.hProcess,0);

328

7.

FeedBack := 'Time limit exceeded!';{*


. *}
end
else begin
GetExitCodeProcess(PInf.hProcessrExitCode) ;
{* . *}
if ExitCode<>0 then FeedBack := 'Run-time error!'
{* ,
. * }
else begin
YourAnswer ;= TStringList. Create;
RightAnswer := TStringList.Create;
YourAnswer.LoadFromFile('output, txt');
{*3
. *}
RightAnswer.LoadFromFile(IntToStr(i)+' .ans');
{*
. * }
if YourAnswer. TextoRight Answer .Text then
Feedback := 'Wrong answer . . .' {*
, , *}
else FeedBack ;= 'Ok.';{* .*}
YourAnswer.Free;
RightAnswer.Free;
end;
end;
RenameFile ('input.txt',IntToStr(i)+'. tst') ;
{* .*}
Writeln('Test '+IntToStr(i)+': '+FeedBack);
(* .*}
end;
DeleteFile('output.txt');{*
. *}
writeln ('Completed ...');{*
. *}
Readln;
end.

7.3.
( )
. . 1*1. .

329

7.

, . - .
.

(
)
1-

2-

.
' 1 ' ,
'0' . ,
.
. N 2
1000. 5 .
.
Photol.txt
.
4
. 0100
:
1000
If <[i,j] 1- >=[i,j]
1110
1101
2
Photo2.txt
Then <[i,j] >: = 1
4
Else <[i,j] >: = 0,
0111
1011
0010
.
0001
,
Result.txt
, 4
.
.
Const
Size = 100;{ * . *}
Dx: Array [I. .4] Of Integer = (0, -1, 0, 1)
{* Dx, Dy

. *}
Dy: Array[1.. 4] Of Integer = (-1, 0, 1, 0) ;
Type
PhotoMatch = Array
[L.Size,
L.Size]
Of Byte;
{* . *}

330

7.

Helper

= Array
[1..Size*Size]
Of Integer;
{*
. *}

Var
PhotoM : PhotoMatch;{* . *}
,: Helper;
Result,
TempRes,N:
Integer;

(. 3.2).
Function

Get(Const k,p : Byte):


Integer;{*
. *}
dn, nx, ny, up : Integer;

Var i,
Begin
Ox[l] := k; Oy[l] := p; dn := 1 ; up := 1;
{ *3
. *}
PhotoM [k,p]:=0;
Repeat
For i:=1 To 4 Do Begin
nx ;= Ox [dn] + dx [i]; ny := Oy [dn] + dy [i];
If (nx>0) And (ny>0) And (nx<=N) And (ny<=N)
And
(PhotoM[nxfny]=l)
Then Begin{* . *}
PhotoM [nx, ny] :- 0; Inc (up) ; Ox [up] := nx;
Oy [up] := ny;
End;
End;
Inc (dn);{*
. *}
Until dn>up;{* .*}
Get := up; {*
. *}
End;
.
Procedure
Init;
Var photol, photo2: Text;
charl, char2 : Char;
x,y : Integer;
Begin
Assign
(photol,
'photol. txt');
Reset (photol);
{* . *}
Assign
(photo2,
'photo2.txt');
Reset(photo2);
ReadLn(photol,N);ReadLn(photo2,N) ;

7.

331

For := 1 N Do Begin
For ;= 1 N Do Begin
Read(photol,
charl); Read (photo2,
char2);
PhotoM
[x,y]:=Ord(charl=char2);{*
"" , ...*}
End;
ReadLn (photol); ReadLn (photo2);
End;
End;

.
Procedure
Solve;
Var i , j , t :Integer;
Begin
Result:=0;
For i : = 1 To N Do
For j:= 1 To N Do
If (PhotoM[i,j]=l) Then Begin
t:=Get(i,j)
;
If t>Result Then Result:=t;{*
. *}
End;
End;

.
Procedure WriteAnswer;
Var f: Text;
Begin
Assign
(f ,
'result.txt'); Rewrite(f) ;
(f, Result); Close (f);
End;

Write

.
Begin
Init;
Solve;
WriteAnswer;
End.

, , , ,
. Size ( 200) Error 22: Structure too large. He

332

7.

. ,
64 *.
,
, , -

- . ?
. . : 1 ; 2 ; 3 4 ; 5
.
. .
Const Size = 1000;(* .*}

Answer = Array [0..Size] Of Longlnt;{*


( ). *}
PhotoLine = Array [0..Size]
Of Integer;{*
. *}
Mar
OldLine, NewLine : PhotoLine;{*OldLine , NewLine - . *}
wrlnd : Integer;{* ,
. *}
Procedure
Init;
Begin
FillChar(
OldLine, SizeOf(OldLine) ,
FillChar{ TempRes, SizeOf(TempRes) ,
wrlnd := 1;
Result:=0;
End;

0);
0);

. , , . , -
. 1 - 2- . .

7.

333

Result
:
Integer;{*.*}
TempRes : Answer;
f i l e l , file2 : Text;
Procedure Change (Dellnd, Newlnd: Integer);
(*3
. *}
Var i : Integer;
Begin
For i ;= 1 To Size Do Begin {*
. *}
If NewLine[i] = Dellnd Then NewLine[i] ;= Newlnd;
If OldLine[i] = Dellnd Then OldLine[i] := Newlnd;
End;
End;
Procedure CompareLines;{^
. ,
"". *}
Var
i
-.Integer;
charl, char2 :Char;
Begin
FillChar ( NewLine, SizeOf(NewLine),
0);
For i := 1 To Size Do Begin
Read(filel,
charl);
Read(file2,
char2);
If charl = char2 Then Begin {*
"". *}
If (NewLine [i-1] = 0) Or (i = 1) Then Begin
{* . *}
While TempRes[wrind]<>0 Do
If wrInd<Size Then Inc ( wrind ) Else wrind
:= 1;

NewLine[i] := wrind
End
Else NewLine[i] := NewLine[i-1];{*
. *}
If (OldLine[i]<>0) And_{NewLine[i]OOldLine[i])
Then Begin^,{^ . *}

TempRes OldLine [i] ] .\= TempRes


[ OldLine [i] ] + TempRes [ NewLine [i] ];
{* . *)
If TempRes[ NewLine[i]]<>0 Then Begin
{*. *)
TempRes[ NewLine [i] ] := 0;
{*
. *)

334

7.

Change(NewLine[i],
OldLine[i]);
End Else NewLine [i] := OldLine [i] ;
(*. *}
End;
Inc ( TempRes[ NewLine[i] ] );
End
Else NewLine[i] := 0;
End;
ReadLn (filel); ReadLn (file2);
End;
Procedure Solve;
Var i, j : Integer;
Begin
Assign (filel,
'photoA.txt');
Reset (filel);
Assign(file2,
'photoB.txt');
Reset (file2);
For i ;= 1 To Size Do Begin
CompareLines;{* . *}
For j := 1 To Size Do
If (TempRes [j]<>0) And (TempRes [j] > Result)
Then Result :=TempRes[j];
OldLine ;= NewLine;{*
. *}
End;
Close(filel);Close(file2)
;
End;

.
. 5 . .

,
. (
)
( ). ? ,
, , , ,
( ). , ,
, . , -

7.

335

(,
, ).
? ,
,
.
? .

, Change , . . (
Change 1000*1000,
).
? , !
. , , Change .
. . :
;
() , ;
.
? .
(
) ,

. . TempRes.
: ,
, Used.
Type
Var

UsedAn = Array [1..Size] Of Boolean;{*



. *}
Used:UsedAn;

336

7.


.
Procedure NextWrlnd;{* Used ,
Used[i], False, ,
i -
. *}
Begin
While Used[wrlnd] Do
If wrInd<Size Then Inc ( wrlnd ) Else wrlnd := 1;
TempRes [ wrlnd ] ;= 0;
End;
,
. .
Function FindLast( ind : Integer ):Integer;{*
,
. *}
Begin
If TempRes [ ind ] > 0 Then FindLast ;= ind
Else Begin
TempRes [ ind ] ;= -FindLast ( Abs ( TempRes
[ind] ) ) ;
FindLast := Abs ( TempRes [ ind ] ) ;
End;
End;
. .
Procedure CompareLines;
Var i : Integer;
charl, char2 : Char;
Begin
FillChar ( NewLine, SizeOf(NewLine), 0) ;
For i := 1 To Size Do Begin
Readifilel,
charl); Read(file2,
char 2) ;
If charA = charB Then Begin
If (NewLine[i-1] = 0) Or (i = 1) Then Begin
NextWrlnd;{*
. *}
newLine [i] := wrlnd;
End Else NewLine[i] := NewLine [i-1];
If (OldLine[i]<>0) And (NewLine[i]OOldLine[i])
Then

7.

337

If TempRes [ OldLine[i] ] > 0 Then Begin


(* . *}
TempRes[OldLine[i]]:=TempRes[OldLine[i]]
+TempRes[NewLine[i]]; {*
. *}
TempRes[ NewLine [i] ] := - OldLine [i];
{*'
. *}
NewLine[i] := OldLine [i]
End Else NewLine [i] := FindLast ( OldLine
[i] ); {*
,
i
,

. *}
Inc ( TempRes [ NewLine [i] ] );
Used [NewLine [i] ] := True ;
End;
End;
ReadLn(filel);ReadLn(file2) ;
End;
, Solve.
Procedure Solve;
Var i -.Integer;
Begin
InitTemp;
Assign(fileA, 'photoA. txt'); Reset(fileA);
Assign (fileB, 'photoB.txt'); Reset(fileB);
For i := 1 To Size Do Begin
CompareLines;
FillChar ( used , SizeOf(used), False);
{* . *}
For j :- 1 To Size Do Beg-in
If TempRes [ NewLine [j] ] < 0 Then
NewLine [j] := FindLast ( NewLine [j] );{*


,

. *}
If (TempRes [ NewLine [j] ] > 0) And Not Used
[NewLine [j]] Then Begin

338

7.
If TempRes [ NewLine [j] ] > Res Then Res : =
TempRes [ NewLine [j] ];
Used [ NewLine [j] ] := True;
End;
I
End;
OldLine := NewLine;
End;
Close(fileA);Close(fileB);
End;

, NewLine
TempRes, ,
.

NewLine

TempRes

1 020304050

1111100000

1 -

,

1
2-
. 1-

6000000000
1 000000000

11111-1 0000
2 1111-10000

1 1 00000000

3 1111-10000

1 1 20000000

34111 -10000
-24111-100 0 0
- 2 5 1 1 1 - 1 0 00 0


.

.

1122000000

- 2 6 1 1 1 - 1 0 00 0

1122330000

-2-38 11-1000 0

3-

.

TempRes

339

7.
/

NewLine

TempRes

1122334450

- 2 - 3 - 4 - 5 14-1 0 00 0

10

1122334455

- 2 - 3 - 4 - 5 15-1 00 00


CompareLines

5555555555

- 5 - 5 - 5 - 5 15-1 00 00



CompareLines.

-

5-

1000*1000 3 .
? ,
. . , . , ,
.

1. . . .
. : , 1995.
2. . ., . . :
. .:
, 1999.
3. . ., . ., . ., . . (
). : - , 1997.
4. . ., . ., . .,
. . . :
- , 1995.
5. . ., . ., . ., . . .
: -, 2000.
6. . ., . .
. .: , 1990.
7. . . . .: , 1969.
8. . + =.
.: , 1989.
9. . ., . . :
. .: , 1990.
10. . ., . ., . . 100
. .: , 1993.
11. . ., . ., . ., . . . .: , 1990.
12. . . .: , 1979.
13. . . . . . .:
, 1991.
14. . ., . .
. .: , 1986.
15. . ., . ., . .
. 1989-1996. .:
ABF, 1996.

^^^

341

16. . . . .: , 1978.
17. . ., . .
. , - , 1993.
18. . . .:
, 1988.
19. . ., . . 100 . , - , 2000.
20. . ., . ., . .
. , - , 1998.
21. . . ( ): . , - ,
1996.
22. . . . , - , 1993.
23. . . . .: , 2001.
24. ., . :
. .: , 1989.
25. . . . : , 1975.
26. . . // . 2000.
39.

Оценить