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

,

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

(,

, ..)
.
i,
:
Pi = (Xi,Yi), Xi,Yi

R. ( real,
extended).

,

, .

,
.
,

. ,
,

.

: , ,
( ), ,
( ) ...
7-8


.
.
(x,y). , ,
..

a
,
(x,y).
:
x=x2-x1
(x1,y1)

;
y=y2-y1
(x2,y2)

a
:

a = x2 + y2

,
,
A(x1;y1) B(x2;y2).

2
2
AB= ( x1 x 2 ) + ( y1 y 2 )

( )



(a b ) = a * b * cos

a b = a * b * sin

( )
,
:
- a, b ; -
.

, ..
,
.

:

(
a
b ) = x1 * x 2 + y1 * y 2
( )

a
b = x1 * y 2 y1 * x 2 .
( )
, ,

:
x1 * y 2 y1 * x 2
sin =
2
2
2
2
( x1 + y1 )( x 2 + y 2 )

a, b

cos =

x1 * x 2 y1 * y 2
2

( x1 + y1 )( x 2 + y 2 )
,
.
-, ,
,
. ,
, ,
,
.
-,
:

a
b > 0 ,
a

a b
a b = 0 ,

a
b < 0 , a


:
(x-x1)(y2-y1)=(y-y1)(x2-x1)
(
,

(x1,y1) (x2;y2));
y=kx+b
( ,
k, b).
,
, , k,

, ,
,
.
- ,
.
,
:
y=kx+b. (x-x1)(y2y1)=(y-y1)(x2-x1), .. (x1;y1)
(x2;y2),
.
,
.
x1, x2,
, :
y1=kx1+b;
y2=kx2+b.
(x1;y1)
(x2;y2),
(x-x1)(y2-y1)=(y-y1)(x2-x1).

(x-x1)(y2-y1)=(y-y1)(x2-x1).

y=kx+b.
:
x(y2 - y1) - x1(y2 - y1) = y(x2 - x1) y1(x2 - x1);
y(x2 - x1) = x(y2 - y1) + y1(x2 - x1) x1(y2 - y1);
y y1 y1 ( x 2 x1 ) x1 ( y 2 y1 )
y=x 2
+
x 2 x1
x 2 x1
y y1
y y1
y=x 2
+ ( y1 x1 2
)
x 2 x1
x 2 x1
, ,
y y1
y y1
k= 2
b = y1 x1 2
x 2 x1
x 2 x1
, k, b
. ,
(x1;y1):
y=kx+b;
y1=kx1+b;
b=y1-kx1.

, ,
, , x1=x2, ,
.
ax + dy + c=0. y =
kx + b.

:
ax + dy + c=0;

dy=-ax-c;
ax c
;
d
a
c
k = ,b =
d
d
y=

y=

ax c
;
d
d

y = (

a
c
x) + ( ) .
d
d

, ,
.
(x - x1)(y2 - y1) = (y - y1)(x2 - x1).
ax + by + c = 0.
:
x(y2 - y1) - x1(y2 - y1) = y(x2 - x1) - y1(x2 - x1);
x(y2 - y1) + y(x1 - x2) + y1(x2 - x1) - x1(y2 y1)=0.
, :
a=y2-y1;
b=x1-x2;
c=y1(x2-x1)x1(y2-y1).
( x 0 , y 0 ) ax + by + c = 0

l=

ax 0 + by 0 + c
a2 + b2


, a, b c
, .
,
.
Turbo Pascal,
(unit), ,
.
,
(x-x1)(y2-y1)=(y-y1)(x2-x1).

,
, .
1. A(x1;y1), B(x2;y2) (x-x3)(y4-y3)=(y-y3)
(x4-x3). ,
. ,
C(x3;y3) D(x4;y4).

, ,

, C

CB
CD, CA, CB . A

CD ,
.
.
, ,
, ,
,

, CD . ,
:

Sign( CA CD ) Sign( CB CD ).
Sign -
:

Sign(x)=
:
function VectorMult(x1,y1,x2,y2:real):real;
begin
VectorMult:=x1*y2-x2*y1;
end; {VectorMult}
function Sign(n:real):shortint;
begin
if n>0 then Sign:=1 else
if n<0 then Sign:=-1 else
Sign:=0;
end;
, ,
.
,
.
, ,
.
function OtherSide(x1,y1,x2,y2,x3,y3,x4,y4:real):boolean;
var v0x,v0y,v1x,v1y,v2x,v2y:real; { }
begin
v0x:=x4-x3; v0y:=y4-y3;
v1x:=x1-x3; v1y:=y1-y3;
v2x:=x2-x3; v2y:=y2-y3;
if (v0x*v1y-v0y*v1x)*(v0x*v2y-v0y*v2x)<0 then OtherSide:=true
else OtherSide:=false;
{

if
Sign(VectorMult(v0x,v0y,v1x,v1y))<>Sign(VectorMult(v0x,v0y,v2x,v
2y))}
end; {OtherSide}
2. A(x1;y1), B(x2;y2), C(x3;y3) D(x4;y4). ,
AB CD.
, :
A B CD;
D AB.

OtherSide.
Function LinesCross(x1,y1,x2,y2,x3,y3,x4,y4:real):boolean;
begin
if OtherSide(x1,y1,x2,y2,x3,y3,x4,y4) and
OtherSide(x3,y3,x4,y4,x1,y1,x2,y2)
then
LinesCross:=true
else LinesCross:=false
end;
3. (x-x1)(y2-y1)=(y-y1)(x2-x1) (x0;y0).
, .
. ,
, .
, :
(x0 - x1)(y2 - y1) = (y0 - y1)(x2 - x1).
function PointOnLine(x0,y0,x1,y1,x2,y2:real):boolean;
begin
if (x0-x2)*(y1-y2)=(y0-y2)*(x1-x2) then PointOnLine:=true
else PointOnLine:=false
end;
4. A(x1;y1), B(x2;y2), C(x3;y3) D(x4;y4). ,
AB CD .
AB:

(x-x1)(y2-y1)=(y-y1)(x2-x1).

CD , ,
C D , ..
.
PointOnLine:
function SameLine(x1,y1,x2,y2,x3,y3,x4,y4:real):boolean;
begin
if PointOnLine(x3,y3,x1,y1,x2,y2) and
PointOnLine(x4,y4,x1,y1,x2,y2) then SameLine:=true
else SameLine:=false
end;

5. (x - x1)(y2 - y1) = (y - y1)(x2 - x1) (x - x3)(y4 y3)=(y - y3)(x4 - x3). (


).
,
:

,

,

.
y=kx+b. ,
. :

k1x+b1=k2x+b2;
x(k1-k2)=b2-b1;
x=

b2 b1
k1 k 2

y .
, ,
( ) y,
k.
y. , ,
(x - x1)(y2 - y1) = (y - y1)(x2 -x1) y, ..
x2 = x1 y = k2x + b2,
y, .. k2 .
, , x1,
.
, y.

, , ..
k1=k2. ,
y - ,
.
x1, y1, x2, y2, x3, y3, x4, y4,
- : x,y ( );
err , , .

procedure CrossPoint(x1,y1,x2,y2,x1,y1,x2,y2:real; var


x,y:real;
var err:boolean);
var k1,b1,k2,b2:real;
begin
if (x1=x2) and (x3=x4) then err:=true else
begin
k1:=(y1-y2)/(x1-x2); b1:=y1-k*x1;
k1:=(y3-y4)/(x3-x4); b1:=y3-k*x3;
if x1=x2 then
begin { - y, - }
x:=x1; y:=k2*x+b2;
end else
if x3=x4 then
begin
x:=x3; y:=k1*x+b1;
end else
if k1=k2 then err:=true { } else
begin
x:=(b2-b1)/(k1-k2);
y:=k1*x+b1;
end;
end;
end; {CrossPoint}

" ".
.

(
)
(xi;yi), 1 i N .
, (x0;y0)
.
input.txt:
- (x0;y0), (i+1)- i- (xi;yi).
output.txt:
- 1,
( ), 0 -
.
.
Input.txtt
5 3.1

Output.txt
1

Input.txt
5 3.7

1 1

1 1

6 3.8

6 3.8

3 3

3 3

Output.txt
0

5 3

5 3

2 2

2 2

2 4

2 4

.
. , .
(x;y)

. , ,

.
.
(x0; y0) .
XOR.
, XOR
, -
, .
- .
, CrossPoint,
Geometry, .
uses Geometry;
const maxn=100; { }
type index=1..maxn;
coord=real;
{}
var dat,out:text;
n,i,i1:index;
x,y:array[index] of coord;
x0,y0:coord; { }
inpoly:boolean;
function min(a,b:real):real;
begin
if a<b then min:=a else min:=b;
end; {min}
function max(a,b:real):real;
begin
if a>b then max:=a else max:=b;
end; {max}
function ExCrossPoint(i:index):boolean;
{, i-
}
var cx,cy:real; { }
err:boolean;
begin
if i=n then i1:=1 else i1:=i+1;
CrossPoint(x0,y0,x0+1,y0,x[i],y[i],x[i1],y[i1],cx,cy,err);
if err then ExCrossPoint:=false else
if (cx>=x0) and (cx>=min(x[i],x[i1])) and
(cx<=max(x[i],x[i1]))

and (cy>=min(y[i],y[i1])) and


(cy<=max(y[i],y[i1]))
{
}
then ExCrossPoint:=true
else ExCrossPoint:=false;
end; {ExistSamePoint}
begin {Main}
Assign(dat,'input.txt'); Reset(dat);
ReadLn(dat,x0,y0); { }
while not eof(dat) do
begin
Inc(n);
ReadLn(dat,x[n],y[n]);
end;
Close(dat);
for i:=1 to n do
begin
if (x0=x[i]) and (y0=y[i]) then
begin { (x0;y0)
}
inpoly:=true; Break;
end;
inpoly:=inpoly xor ExCrossPoint(i);
end;
Assign(out,'output.txt'); Rewrite(out);
if inpoly then write(out,1) else write(out,0);
Close(out);
end.
" " (SqMax)
. N
(xi;yi), 1 i N, 4 N 300.
, ,
. , ,
, p.

input.txt
p: p N+1 p, p p
N - ,

N cp p p (xi;yi) - i , p p.

output.txt

p ,
: 'NO SOLUTION';
o

p
,

p:

p
p

,
o

p p,
p p p
p
,
p

p,

.
.
Input.txt Output.txt

Input.txt

11

Output.txt
NO SOLUTION

100

100

100 0

10

200

-100

100

100

11
100

50

50

150

150

50

300

100

-30

-30

-100 0
0

-100

. ""
. ,
.
,
.
,

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


. ,
. ,
.
uses Geometry;
const maxn=400; { - }
maxs=4; {- }

type index=0..maxn;
coord=longint;
{}
indexs=1..maxs;
var dat,out:text;
n,i,j,k,col,max:index;
x,y:array[index] of coord;
{ }
solx,soly:array[indexs] of coord; {}
s:indexs;
x3,y3,x4,y4:coord;
s1,s2,s3,s4:shortint;
function ExistPoint(px,py:coord):boolean;
{, (px;py) }
begin
for k:=1 to n do
if (px=x[k]) and (py=y[k]) then
begin
ExistPoint:=true; Exit;
end;
ExistPoint:=false;
end; {ExistPoint}
begin {Main}
Assign(dat,'input.txt'); Reset(dat);
ReadLn(dat,n);
for i:=1 to n do
ReadLn(dat,x[i],y[i]);
Close(dat);
for i:=1 to n-1 do
for j:=i+1 to n do
begin { - (x[i];y[i])
(x[j];y[j]) }
x3:=x[i]+y[i]-y[j]; y3:=y[i]+x[j]-x[i]; { }
x4:=x[j]+y[i]-y[j]; y4:=y[j]+x[j]-x[i]; {
}
if ExistPoint(x3,y3) and ExistPoint(x4,y4) then
begin { - ,
}
col:=0;
for k:=1 to n do
begin {s1,s2,s3,s4 -
}
s1:=Sign(VectorMult(x[j]-x[i],y[j]-y[i],x[k]x[i],y[k]-y[i]));
s2:=Sign(VectorMult(x4-x3,y4-y3,x[k]-x3,y[k]y3));
s3:=Sign(VectorMult(x3-x[i],y3-y[i],x[k]x[i],y[k]-y[i]));
s4:=Sign(VectorMult(x4-x[j],y4-y[j],x[k]x[j],y[k]-y[j]));
if (s1<>s2) and (s3<>s4) then Inc(col); {
}
end;
if col>max then

begin { }
max:=col;
solx[1]:=x[i]; soly[1]:=y[i];
solx[2]:=x[j]; soly[2]:=y[j];
solx[3]:=x4;
soly[3]:=y4; {
}
solx[4]:=x3;
soly[4]:=y3;
end;
end;
end;
Assign(out,'output.txt'); Rewrite(out);
if max=0 then writeln(out,'NO SOLUTION') else
begin
writeln(out,max);
for s:=1 to 4 do
writeln(out,solx[s],' ',soly[s]);
end;
Close(out);
end.


.
A(x,y)
B(x1,y1) C(x2,y2)? Z
pOB+(1-p)OC=Z, 0<=p<=1,
OB OC - .
p, 0<=p<=1,
pOB+(1-p)OC=A (1),
A ,
- .
(1)
:
px1+(1-p)x2=x
(2)
py1+(1-p)y2=y
(3)
p, :
0<=p<=1, A , - .
.
uses crt;
var d:boolean;
x,y,x1,y1,x2,y2:extended;
function pixelon(x,y,x1,y1,x2,y2:extended):boolean;
var p1,p2:extended;
begin
if (x1=x2) and(y1=y2) then begin pixelon:=true;exit;end;
if x1=x2 then p1:=(y-y2)/(y1-y2)
else p1:=(x-x2)/(x1-x2);
p2:=p1*y1+(1-p1)*y2;
if (p1>=0)and(p1<=1)and(p2=y)
then pixelon:=true else pixelon:=false;
end;
begin
clrscr;
write(' (x,y):'); readln(x,y);

write(' B(x1,y1):'); readln(x1,y1);


write('

C(x2,y2):');readln(x2,y2);
d:=pixelon(x,y,x1,y1,x2,y2);
if d then writeln(' ')
else writeln(' ');
readkey;
end.

1, 2, 3

.
P1 P2, b - P2 P3.

m - ,

-
, P1P2 P2 P3.
, ,
m -1/m,
, a b P1P2
P2P3

, x

x
. :
y, x.

(mb - ma) , .
, .
- ,
m . ,
,
.

( ).


.
P1 P2.
,
, P1 x P2

( ),
(0,0), P1, P2, P1 + P2 = (X1 + X2 , Y1 + Y2).



P1 x P2 = X1 Y2 X2 Y1.

. 1.
() P1 P2
( ), ()
P
, .

W = X1Y2 X2Y1. W>0,
P2
(0,0) P1,
, W<0, . 1 ()


, .

,
. .
,
01 0 02 (
). 0,

1 0 = (1 0, Y1 - Y0) P2
P0 = (2 0, Y2 - Y0).
W = (P1 P0) x (P2 P0) = (X1 X0) (Y2-Y0) (X2 - X0) (Y1
Y0).
W>0, (
), W<0 .
.

(X1,Y1),
B(X2,Y2),
C(X3,Y3).

, B(X2,Y2)
C(X3,Y3) A(X1,Y1).
uses crt;
var x1,y1,x2,y2,x3,y3:extended;
d:boolean;
function poworot(x1,y1,x2,y2,x3,y3:extended):boolean;
var d:extended;
begin
d:=(x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);
writeln(d);
if d=0 then begin writeln(' ,
');halt;end;
if d>0 then poworot:=true else poworot:=false;
end;
begin
clrscr;
write(' :');readln(x1,y1);
write(' :');readln(x2,y2);
write(' :');readln(x3,y3);
d:=poworot(x1,y1,x2,y2,x3,y3);
if d then writeln('" " -
')
else writeln('" " -
');
readkey;
end.

.
, ,

,
N . .

. (1 )
,
.
,
.


. ,

,


, ,
. A B
. A ((X1,Y1) (X2,Y2)), B ((X3,Y3) (X4,Y4)).
A
c X1n = min(X1,X2)

Y1n =
min(Y1,Y2) c
X2w =
max(X1,X2)

Y2w = max(Y1,Y2). B
c X3n
= min(X3,X4)

Y3n = min(Y3,Y4) c

X4w = max(X3,X4)

Y4w =
max(Y3,Y4)..

min

max

,
: ,
(X1n, Y1n)

(X2w, Y2w) ,
B
(X3n, Y3n)
(X4w, Y4w)
,
(X2w>=X3n) (X4w>=X1n) (Y2w>=Y3n) (Y4w>=Y1n).

X ,
Y .
,
, ,
,
. ,

.
.
.
P3 P4 ,
P1 P2,
P1P3
14
12, ..
(3 - 1) (2 - 1) (4 - 1) (2
1) .
, 3 4 ,
1 2
,
:
, - .

. , ,
12 , ,
34 12 , .. . ,

(3 1) (2 1) (4
1) (2 1), .
, : 12 34
,
:
1)
(X2w>=X3n) (X4w>=X1n) (Y2w>=Y3n) (Y4w>=Y1n)
2) ((P3 P1) x (P2 P1)) x ((P4 P1) x (P2 P1))<=0
3) ((P1 P3) x (P4 P3)) x ((P2 P3) x (P4 P3))<=0


, .
((P3 - P1) x (P2 P1)) = (X3 X1)(Y2 Y1)-(X2 - X1)(Y3 Y1)
((P4 - P1) x (P2 P1)) = (X4 X1)(Y2 Y1)-(X2 - X1)(Y4 Y1)
((P1 P3) x (P4 P3)) = (X1 X3)(Y4 Y3)-(X4 - X3)(Y1 Y3)
((P2 P3) x (P4 P3)) = (X2 X3)(Y4 Y3)-(X4 - X3)(Y2 Y3)

.
1) (X2w>=X3n) (X4w>=X1n) (Y2w>=Y3n) (Y4w>=Y1n)
2) (X3 X1)(Y2 Y1)-(X2 - X1)(Y3 Y1) x (X4 X1)(Y2 Y1)(X2 - X1)(Y4 Y1)<=0
3) (X1 X3)(Y4 Y3)-(X4 - X3)(Y1 Y3) x (X2 X3)(Y4 Y3)(X4 - X3)(Y2 Y3) <=0
1)

.

,
.
uses crt;
var x1,y1,x2,y2,x3,y3,x4,y4:extended;
d:boolean;
function min(a,b:extended):extended;
begin
if a>b then min:=b else min:=a;
end;
function max(a,b:extended):extended;
begin
if a>b then max:=a else max:=b;
end;
function potrezok(x1,y1,x2,y2,x3,y3,x4,y4:extended):boolean;
var mx1,my1,mx2,my2:extended;
mx3,my3,mx4,my4:extended;
d,p1,p2:boolean;
begin
mx1:=min(x1,x2);my1:=min(y1,y2);
mx2:=max(x1,x2);my2:=max(y1,y2);
mx3:=min(x3,x4);my3:=min(y3,y4);
mx4:=max(x3,x4);my4:=max(y3,y4);
d:=(mx2>=mx3)and(mx4>=mx1)and(my2>=my3)and(my4>=my1);
P1:=((X3-X1)*(Y2-Y1)-(X2-X1)*(Y3-Y1))*((X4-X1)*(Y2-Y1)-(X2X1)*(Y4-Y1))<=0;

P2:=((X1-X3)*(Y4-Y3)-(X4-X3)*(Y1-Y3))*((X2-X3)*(Y4-Y3)-(X4X3)*(Y2-Y3))<=0;
IF D AND P1 AND P2 THEN D:=TRUE ELSE D:=FALSE;
if d then potrezok:=true else potrezok:=false;
end;
begin
clrscr;
write(' :');readln(x1,y1);
write(' :');readln(x2,y2);
write(' :');readln(x3,y3);
write(' :');readln(x4,y4);
d:=potrezok(x1,y1,x2,y2,x3,y3,x4,y4);
if d then writeln(' ')
else writeln(' ');
readkey;
end.
2 ?
(2 )
----------------------------------------------------------------------

A(ax1,ay1,ax2,ay2)

B
(bx1,by1,bx2,by2),
TRUE - ,

, FALSE ()
----------------------------------------------------------------------function
Intersection(ax1,ay1,ax2,ay2,bx1,by1,bx2,by2:real):boolean;
var v1,v2,v3,v4:real;
begin
v1:=(bx2-bx1)*(ay1-by1)-(by2-by1)*(ax1-bx1);
v2:=(bx2-bx1)*(ay2-by1)-(by2-by1)*(ax2-bx1);
v3:=(ax2-ax1)*(by1-ay1)-(ay2-ay1)*(bx1-ax1);
v4:=(ax2-ax1)*(by2-ay1)-(ay2-ay1)*(bx2-ax1);
Intersection:=(v1*v2<0) and (v3*v4<0);
end;
begin { , - }
writeln(Intersection(1,1,5,5,1,2,3,1));
Intersection}
writeln(Intersection(1,1,5,5,1,2,1,3));
Intersection}
end.

{test1,
{test2,

yes
no

,
.



.

(
).

Z(x,y) ,
.

,
,
Z(x,y)
.
Z(x,y)

1 (
).




. , , -
. , .

.
uses crt;
type b=record
x,y:extended;
end;
var x,y,maxi:extended;
i,n,k,nom:integer;
d:boolean;
a:array[1..100] of b;
function min(a,b:extended):extended;
begin
if a>b then min:=b else min:=a;
end;
function max(a,b:extended):extended;
begin
if a>b then max:=a else max:=b;
end;
function potrezok(x1,y1,x2,y2,x3,y3,x4,y4:extended):boolean;
var mx1,my1,mx2,my2:extended;
mx3,my3,mx4,my4:extended;
d,p1,p2:boolean;
begin
mx1:=min(x1,x2);my1:=min(y1,y2);
mx2:=max(x1,x2);my2:=max(y1,y2);
mx3:=min(x3,x4);my3:=min(y3,y4);
mx4:=max(x3,x4);my4:=max(y3,y4);
d:=(mx2>=mx3)and(mx4>=mx1)and(my2>=my3)and(my4>=my1);
P1:=((X3-X1)*(Y2-Y1)-(X2-X1)*(Y3-Y1))*((X4-X1)*(Y2-Y1)-(X2X1)*(Y4-Y1))<=0;
P2:=((X1-X3)*(Y4-Y3)-(X4-X3)*(Y1-Y3))*((X2-X3)*(Y4-Y3)-(X4X3)*(Y2-Y3))<=0;
IF D AND P1 AND P2 THEN D:=TRUE ELSE D:=FALSE;
if d then potrezok:=true else potrezok:=false;
end;
procedure zapmas;
begin
assign(input,'input.txt');
reset(input);
read(n);
for i:=1 to n do

read(a[i].x,a[i].y);
read(x,y);
close(input);
end;
procedure kolper;
begin
k:=0;
maxi:=a[i].x;
nom:=1;
for i:=2 to n do
if maxi<a[i].x then begin maxi:=a[i].x;nom:=i;end;
a[n+1].x:=a[1].x;
a[n+1].y:=a[1].y;
for i:=1 to n do
if
potrezok(a[i].x,a[i].y,a[i+1].x,a[i+1].y,x,y,a[nom].x+1,a[nom].y
) then k:=k+1;
if odd(k) then writeln(' ')
else writeln(' ');
end;
begin
clrscr;
zapmas;
kolper;
readkey;
end.

potrezok

input.txt.

.


Z(x,y)
1
.
.

, ,
ax +by +c =0 ( y = kx + b)
(X1,Y1) (X2,Y2) .
1. cx+d=y
,
x,
,
.
,

x*(c-a)=(b-d),

y = ax+b,

x1<=x<=x2, y1<=y<=y2.
x

, .
2.
F(x,y)=ax+b-y.
ax+b=y
:
F(x,y)>0,

F(x,y)<0 ax+b=y F(x,y)=0 .

,

,

.


F(x1,y1)*F(x2,y2)<=0.
,

, ,
. ,
.
.
uses crt;
var k,b,x1,y1,x2,y2:extended;
d:boolean;
function potrprm(k,b,x1,y1,x2,y2:extended):boolean;
var f1,f2:extended;
begin
f1:=k*x1+b-y1; f2:=k*x2+b-y2;
if f1*f2<=0 then potrprm:=true else potrprm:=false;
end;
begin
clrscr;
write(' k b ') ;
readln(k,b);
write(' (x1, y1):');
readln(x1,y1);
write(' (x2, y2):');
readln(x2,y2);
d:=potrprm(k,b,x1,y1,x2,y2);
if d then writeln(' ')
else writeln(' ');
readkey;
end.
ax+by+c =
0,

function
potrprm(k,b,x1,y1,x2,y2:extended):boolean; k b

a,b,c, .. function potrprm(a,b,c,x1,y1,x2,y2:


extended):boolean; f1:=k*x1+b-y1; f2:=k*x2+b-y2;
f1:=a*x1+b*y1+c; f2:=a*x2+b*y2+c;


.

.
.
(1, y1), (x2, 2), ..., (N,N)

. S
S =

1
(x1y2 x2y1+
2

x2y3 x3y2

++

xNy1 x1yn)

,
-

- .
, S,

.

,
.
uses crt;
type b=record
x,y:extended;
end;
var a:array[1..200] of b;
s:extended;
i,n:integer;
begin
clrscr;
assign(input,'input.txt');
reset(input);
read(n);
for i:=1 to n do
read(a[i].x,a[i].y);
close(input);
a[n+1].x:=a[1].x;a[n+1].y:=a[1].y;
s:=0;
for i:=1 to n do
s:=s+(a[i].x*a[i+1].y-a[i+1].x*a[i].y);
s:=abs(s/2);
writeln(s:4:2);
readkey;
end.
.
input.txt.

n+1
,
.

. .
(, ) , ,
. pu

[0,2pi]

(x

Function pu (x,y:extended):extended;
Var p:extended;
begin

,y).

If =0 Then p:=pi/2+pi*Ord(y<0)
Else Begin
p:=ArcTan(y/x)+pi;
If x>0 Then If y>=0 Then p:=p-pi
Else p:=p+pi;
End;
pu:=p;
end;

8087

FPATAN,
.

(,),
[-pi, pi].

Function Pu (Const x, : Extended) : Extended;


Assembler; { Delphi }
Asm
FLD
;
FLD
x;
FPATAN;
FWAIT;
End;



.
N (1, y1), (x2, 2), ...,
(N,N).
. ,
.

.
.

,
.
.
{$A+,B-,D+,E+,F-,G-,I+,L+,N+,O-,P-,Q-,R-,S+,T-,V+,X+}
{$M 64384,0,655360}
uses crt,graph;
type b=record
x,y:extended;
no:integer;
pu:extended;
end;
var a:array[1..1500] of b;
i,n,j,gr,z:integer;
cx,cy,f:extended;
Function pu (x,y:extended):extended;
Var p:extended;
begin

If x=0 Then p:=pi/2+pi*Ord(y<0)


Else Begin
p:=ArcTan(y/x)+pi;
If x>0 Then If y>=0 Then p:=p-pi
Else p:=p+pi;
End;
pu:=p;
end;
procedure formmas;
begin
write(' :');
readln(n);
cx:=0;cy:=0;
for i:=1 to n do
begin
a[i].x:=random(600)+30;
a[i].y:=random(440)+30;
a[i].no:=i;
cx:=cx+a[i].x;
cy:=cy+a[i].y;
end;
cx:=cx/n;cy:=cy/n;
end;
procedure posrpixel;
begin
initgraph(gr,gr,'c:\tp7\bgi');
for i:=1 to n do
putpixel(trunc(a[i].x),trunc(a[i].y),7);
circle(trunc(cx),trunc(cy),2);
end;
procedure poiskpu;
begin
for i:=1 to n do
a[i].pu:=pu(cx-a[i].x,cy-a[i].y);
end;
procedure postwogob;
begin
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i].pu>a[j].pu then begin
f:=a[i].pu;a[i].pu:=a[j].p
u;a[j].pu:=f;
z:=a[i].no;a[i].no:=a[j].n
o;a[j].no:=z;
end;
end;
procedure posrline;
begin
a[n+1].x:= a[1].x;a[n+1].y:=a[1].x;
a[n+1].no:= a[1].no;
for i:=1 to n do

line(trunc(a[a[i].no].x),trunc(a[a[i].no].y),trunc(a[a[i+1].
no].x),trunc(a[a[i+1].no].y));
end;
begin
clrscr;
formmas;
posrpixel;
poiskpu;
postwogob;
posrline;
readkey;
end.
.

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

.
Res, ,

N
;

,
,
.
Res
.


, .
W = (P1 P0) x (P2 P0) = (X1 X0) (Y2-Y0)

(X2 - X0) (Y1

Y0).

( ) .
.
{$A+,B-,D+,E+,F-,G-,I+,L+,N+,O-,P-,Q-,R-,S+,T-,V+,X+}
{$M 64384,0,655360}
uses crt,graph;
Const MaxN=5100;
Var x,y: Array[1..MaxN] Of word;

Angle: Array[1..MaxN] Of real;


Res: Array[0..MaxN+1] Of Integer;
N,i, M,gr1,gr2,kol: Integer;
cx,cy:real;
Procedure Init;{ }
Var i: Integer;
Begin
write(' ');
Read(N);
For i:=1 To N Do
Begin
X[i]:=random(600)+10; Y[i]:=random(440)+20;
cx:=cx+x[i];cy:=cy+y[i];
Res[i]:=i;
End;
cx:=cx/n;cy:=cy/n;
M:=N;
End;
Procedure Sort(K, L: Integer);{ }
Var i, j, P: Integer;
T, X: real;
Begin
i:=K; j:=L; X:=Angle[(K+L) Div 2];
repeat
while (X<Angle[i]) do Inc(i);
while (Angle[j]<X) do Dec(j);
if i<=j
then begin
T:=Angle[i]; Angle[i]:=Angle[j];
Angle[j]:=T;
P:=Res[i]; Res[i]:=Res[j]; Res[j]:=P;
Inc(i); Dec(j);
end;
until I>j;
If K<j Then Sort(K,j);
If L>i Then Sort(i,L);
End;
Function ProvRight(x1,y1,x2,y2,x3,y3: real): Boolean;{
, }
Begin
ProvRight:=(x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)>=0;
End;
Function pu (x,y:real):real;
Var p:real;
begin
If x=0 Then p:=pi/2+pi*Ord(y<0)
Else Begin
p:=ArcTan(y/x)+pi;
If x>0 Then If y>=0 Then p:=p-pi
Else p:=p+pi;
End;
pu:=p;
end;

Procedure OprAngle; { }
Var i: Integer;
Begin
x[n+1]:=x[1];y[n+1]:=y[1];
For i:=1 To N Do
Angle[i]:=(pu(cx-x[i],cy-y[i]));
End;
Procedure Over(k: Integer);
Var i: Integer;
Begin
Dec(M);
For i:=k To M Do Res[i]:=Res[i+1];
End;
Procedure Exclud;{ }
Var
i: Integer;
CanGo: Boolean;
Begin
i:=1;
CanGo:= True;
While (i<>1) Or (CanGo) Do
Begin
Res[0]:=Res[M]; Res[M+1]:= Res[1];
If i=2 Then CanGo:= False;
If
ProvRight(x[Res[i-1]],y[Res[i1]],x[Res[i]],y[Res[i]],x[Res[i+1]],y[Res[i+1]])
Then Begin
Over(i);
i:=(i-2+M) Mod M+1;
CanGo:=True;
End
Else i:=i Mod M+1;
End;
End;
procedure strojit;
begin
initgraph(gr1,gr2,'c:\tp7\bgi');
res[kol+1]:=res[1];
x[n+1]:=x[1]; y[n+1]:=y[1];
for i:=1 to n do
putpixel(x[i],y[i],2);
for i:=1 to m do
begin
line(x[res[i]],y[res[i]],x[res[i+1]],y[res[i+1]]);
end;
end;
BEGIN
clrscr;
Init;
OprAngle;
Sort(1, N);
Exclud;
strojit;
readkey;

END.

,
0 640, Y 0 480.
random.

init; OprAngle
.
Sort (
)

ProvRight
, . Over
, , .
Exclud (
).

. .
uses crt,graph;
var f:text;
r:array[1..4000,1..2]of integer;
a,b,c,kol:longint;
r2:array[1..4000]of integer;
n,l,er,er2,la,al,la2:longint;
i:integer;
procedure enter;
begin
fillchar(r2,sizeof(r2),0);
fillchar(r,sizeof(r),0);
write(' :');
readln(n);
for a:=1 to n do
begin
r[a,1]:=random(540)+50;
r[a,2]:=random(380)+50;
end;
end;
procedure naim(var naim:longint);
var cc:integer;
begin
cc:=1;
for a:=2 to n do begin
if r[a,2]<r[cc,2] then cc:=a;
if r[a,2]=r[cc,2] then if r[a,1]<r[cc,1] then cc:=a;
end;
naim:=cc;
end;
function strel(x1,y1,x2,y2,x3,y3:longint):integer;
var bol:longint;
begin
bol:=(x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);

if bol<0 then strel:=-1 else


if bol>0 then strel:=1 else strel:=0;
end;
procedure polugl(x,y:integer);
var kolb:integer;
kodd:integer;
begin
kolb:=r[l,1];r[l,1]:=r[1,1];r[1,1]:=kolb;
kolb:=r[l,2];r[l,2]:=r[1,2];r[1,2]:=kolb;
for a:=2 to n do
for b:=a+1 to n do
begin
kodd:=strel(x,y,r[a,1],r[a,2],r[b,1],r[b,2]);
if kodd=-1 then
begin
kolb:=r[a,1];r[a,1]:=r[b,1];r[b,1]:=kolb;
kolb:=r[a,2];r[a,2]:=r[b,2];r[b,2]:=kolb;
end;
end;
end;
procedure poisk;
begin
naim(l);
polugl(r[l,1],r[l,2]);
kol:=3;
r2[1]:=1;r2[2]:=2;r2[3]:=3;
for a:=4 to n do
begin
er:=strel(r[r2[kol-1],1],r[r2[kol-1],2],r[r2[kol],1],
r[r2[kol],2],r[a,1],r[a,2]);
if (er=1) then begin
r2[kol+1]:=a;
kol:=kol+1;
end;
if er=-1 then
begin
r2[kol]:=a;
repeat
er2:=strel(r[r2[kol-2],1],r[r2[kol-2],2],r[r2[kol1],1],
r[r2[kol-1],2],r[r2[kol],1],r[r2[kol],2]);
if er2=-1 then begin
kol:=kol-1;
r2[kol]:=r2[kol+1];
r2[kol+1]:=0;
end;
until er2<>-1;
end;
end;
r2[kol+1]:=1;kol:=kol+1;
end;
procedure risuem;
begin

i:=0;initgraph(i,i,'C:\TP7\BGI');
for a:=1 to n do
putpixel(r[a,1],r[a,2],15);
for a:=2 to kol do
line(r[r2[a],1],r[r2[a],2],r[r2[a-1],1],r[r2[a-1],2]);
end;
begin
clrscr;randomize;
enter;
poisk;
risuem;
readkey;
closegraph;
end.
.
USES CRT,graph;
Var First,Cur,Min,i,N,M : longint;
z : Real;
k,l:integer;
A:ARRAY[1..2,1..10001] OF INTEGER;
IND:ARRAY[1..10001] OF INTEGER;
Function Det(a,b,c,d : Real) : Real;
Begin
Det:=a*d-b*c;
End;
Function P3Square(P1x,p1y,P2x,p2y,P3x,p3y : integer) : Real;
Begin
P3Square:=Det(P1x-P3x,P2x-P3x,P1y-P3y,P2y-P3y)/2;
End;
Function BtwS(x,a,b : Real) : Boolean;
Begin
BtwS:=(a<=x) And (x<=b) Or (a>=x) And (x>=b);
End;
{ , }
Function PBtwS(x1,y1,x2,y2,x3,y3:integer) : Boolean;
Begin
PBtwS:=BtwS(x1,x2,x3) And BtwS(y1,y2,y3);
End;
Begin
CLRSCR;
{ }
{
}
WRITE(' :');READLN(N);
initgraph(k,l,'c:\tp7\bgi');
FOR I:=1 TO N DO
begin
a[1,i]:=random(600);
a[2,i]:=random(400);

putpixel(a[1,i],a[2,i],7);
end;
First:=1;
For i:=1 To N
Do
If (A[1,i]>=A[1,First]) And (A[2,i]<=A[2,First]) Then
First:=i;
{ " " }
M:=1; Cur:=First;
Repeat
Ind[M]:=Cur; Inc(M); Min:=Cur;
{
! }
For i:=N DownTo 1 Do
Begin
If (A[1,i]=A[1,Cur]) And (A[2,i]=A[2,Cur]) Then Continue;
z:=P3Square(A[1,Cur],A[2,Cur],A[1,i],A[2,i],A[1,Min],A[2,M
in]);
If (Min=Cur) Or (z>0) Or (z=0) And
PBtwS(A[1,Min],A[2,Min],A[1,Cur],A[2,Cur],A[1,i],A[2,i]) Then
Min:=i;
End;
Cur:=Min;
Until Cur=First;
for i:=1 to m-2 do
line(a[1,ind[i]],a[2,ind[i]],a[1,ind[i+1]],a[2,ind[i+1]]);
line(a[1,ind[1]],a[2,ind[1]],a[1,ind[m-1]],a[2,ind[m-1]]);
readkey;
closegraph;
FOR I:=1 TO M-1 DO
WRITE(IND[I],' ');
READKEY;
End.

uses graph,crt;
const filein = 'input.txt'; {
}
var
a: array [1..100] of record x,y: integer; end;
{ }
n: word; {- }
co: array [1..100] of byte;
{; ""
,
}
s: byte;

{"" }
i: integer; { }
label m;
{ }
procedure inp;
begin
close(input);
assign(input, filein); reset(input);
n:=0;
while not eof do
begin
inc(n);
readln(a[n].x, a[n].y);
end;
close(input);
end;
{ x- }
{ - QuickSort}
procedure sort(l,p: integer);
var i,j,revx,revy,x: integer;
label loop;
begin
x:=a[(l+p) div 2].x;
i:=l;
j:=p;
loop:
while a[i].x<x do inc(i);
while a[j].x>x do dec(j);
if i<=j then
begin
revx
:= a[i].x;
revy
:= a[i].y;
a[i].x := a[j].x;
a[i].y := a[j].y;
a[j].x := revx;
a[j].y := revy;
inc(i);
dec(j);
goto loop;
end
else
begin
if l<j then sort(l,j);
if i<p then sort(i,p);
end;
end;

{, 2-
}
function vectz(x1,y1,x2,y2,x3,y3,x4,y4: real): shortint;
begin
if (x2-x1)*(y4-y3)-(x4-x3)*(y2-y1) > 0 then vectz:=1
else
if (x2-x1)*(y4-y3)-(x4-x3)*(y2-y1) < 0 then vectz:=-1
else vectz:=0;
end;
{, ""
}
procedure show;
var gd,gm: integer;
const k = 2;
begin
detectgraph(gd,gm);
initgraph(gd,gm,'');
for i:=1 to s-1 do
line(a[co[i]].x*k, a[co[i]].y*k, a[co[i+1]].x*k,
a[co[i+1]].y*k);
line(a[co[s]].x*k, a[co[s]].y*k, a[co[1]].x*k,
a[co[1]].y*k);
setcolor(red);
for i:=1 to n do circle(a[i].x*k, a[i].y*k, 2);
readkey;
closegraph;
end;

{main}
BEGIN
inp;
{ }
sort(1,n);
{}
fillchar(co, sizeof(co), 0); {"" }
s:=1; co[s]:=1;
{"" 1- ,
-
}
{"" }
for i:=2 to n do
if vectz(a[1].x, a[1].y, a[n].x, a[n].y,
a[1].x, a[1].y, a[i].x, a[i].y)>0 then break;
s:=2; co[s]:=i; {"" }
{
1,n}

while co[s]<>n do
begin
m:
for i:=co[s]+1 to n do
if vectz(a[1].x, a[1].y, a[n].x, a[n].y,
a[1].x, a[1].y, a[i].x, a[i].y) >
0 then break;
while vectz(a[co[s-1]].x, a[co[s-1]].y, a[co[s]].x,
a[co[s]].y,
a[co[s]].x, a[co[s]].y, a[i].x, a[i].y) >
0 do
begin
if s=2 then {; 2-

,
}
begin
for i:=co[s]+1 to n do
if vectz(a[1].x, a[1].y, a[n].x, a[n].y,
a[1].x, a[1].y, a[i].x,
a[i].y)>0 then break;
co[s]:=i;
goto m;
end
else { 2-, ""
}
begin
co[s]:=0;
dec(s);
end;
end;
{ " ",
;
}
inc(s);
co[s]:=i;
end;
{
1,n}
while co[s]<>1 do
begin
for i:=co[s]-1 downto 1 do
if vectz(a[1].x, a[1].y, a[n].x, a[n].y,
a[1].x, a[1].y, a[i].x, a[i].y) <
0 then break;
if i=1 then break;
while vectz(a[co[s-1]].x, a[co[s-1]].y, a[co[s]].x,
a[co[s]].y,

a[co[s]].x, a[co[s]].y, a[i].x, a[i].y) >


0 do
begin
co[s]:=0;
dec(s);
end;
inc(s);
co[s]:=i;
end;
{ =))}
show;
END
.
103.
: - 2001
: 30
: 10

k (1<=k<=20) .
, ,
k , .
.

k, N (1<=N<=10000) N
k , , -1,
.

N : ,
- .

2
3
2 3
3 1
-1 -1

3 2 1

. data,
, (..
, , , ).
, i
data[i]<=k.
.
data, data ,
.
data
O(N), data O(k),
k . N O(N2).
data ,
O(log N) data O(klog N);
O(NklogN).

program HalfSort;
{(c) Semyon Dyatlov, 2002}
const
maxn = 10000;
maxk = 20;
var
n, k, m : integer;
e : array [1..maxn, 1..maxk] of integer;
data, h, h1, ans : array [1..maxn] of integer;
{
m - heap size
h - the heap
e - the graph
data - number of entering edges
ans - the answer
}
procedure Load;
var
i, j : integer;
begin
assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);
fillchar(data,sizeof(data),0);
read(k,n);
for i:=1 to n do
for j:=1 to k do

begin
read(e[i,j]);
if e[i,j]<>-1 then
inc(data[e[i,j]]);
end;
end;
procedure swap(a, b : integer);
var
t : integer;
begin
t := h[a];
h[a] := h[b];
h[b] := t;
h1[h[a]] := a;
h1[h[b]] := b;
end;
procedure heapify(a : integer);
var
l : integer;
begin
l := a;
if (2*a<=m) and (data[h[2*a]]<data[h[l]]) then
l := 2*a;
if (2*a+1<=m) and (data[h[2*a+1]]<data[h[l]]) then
l := 2*a+1;
if l<>a then
begin
swap(l,a);
heapify(l);
end;
end;
procedure Solve;
var
i, j, t, c : integer;
begin
m := n;
for i:=1 to m do
begin
h[i] := i;
h1[i] := i;
end;
for i:=m downto 1 do
heapify(i);
for i:=1 to n do
begin
{Extract minimum}
c := h[1];
swap(1,m);
dec(m);

heapify(1);
h1[c] := 0;
ans[n+1-i] := c;
{Update data}
for j:=1 to k do
if (e[c,j]<>-1) and (h1[e[c,j]]<>0) then
begin
dec(data[e[c,j]]);
t := h1[e[c,j]];
while (t>1) and (data[h[t div 2]]>data[h[t]]) do
begin
swap(t,t div 2);
t := t div 2;
end;
end;
end;
end;
procedure Save;
var
i : integer;
begin
for i:=1 to n do
write(ans[i],' ');
writeln;
close(input);
close(output);
end;
begin
Load;
Solve;
Save;
end.

104.
: - 2001
: 40
: 15

-
. , -
-.
K .
. ,


. ,
.
,
.

. N
(1<=N<=10000) - K (1<=<=N) -
. N P1, P2, ... , PN, (0<=Pi<=10000),
.
N -
( , 100).

K -
( ),
.

2 1
1 2
1 1

1.5

. ,
bound , K
, bound,
bound .
, bound.
i bound, [p[i]bound/c[i], p[i]+bound/c[i]], c - ,
.
.
, .
, :
.
. .
.
,
. , ,
,
.

O(NlogN)
.

program Village_Phones;
{(c) Semyon Dyatlov, 2003}
{$N+}
const
maxn = 10000;
eps = 1e-8;
var
n, k : integer;
p : array [1 .. maxn] of integer;
c : array [1 .. maxn] of byte;
ans : array [1 .. maxn] of extended;
procedure Load;
var
i : integer;
begin
assign(input,'phone.in');
reset(input);
assign(output,'phone.out');
rewrite(output);
read(n,k);
for i:=1 to n do
read(p[i]);
for i:=1 to n do
read(c[i]);
end;
function ok(bound : extended) : boolean;
var
i, t, m : integer;
order : array [1..maxn*2] of integer;
procedure swap(a, b : integer);
var
t : integer;
begin
t := order[a];
order[a] := order[b];
order[b] := t;
end;
function greater(a, b : integer) : boolean;
var
pa, pb : extended;
begin

pa := p[order[a]] - bound / c[order[a]];


pb := p[order[b]] - bound / c[order[b]];
greater := pa > pb+eps;
end;
procedure heapify(a : integer);
var
l : integer;
begin
l := a;
if (2*a<=m) and greater(2*a,l) then
l := 2*a;
if (2*a+1<=m) and greater(2*a+1,l) then
l := 2*a+1;
if l<>a then
begin
swap(l,a);
heapify(l);
end;
end;
begin
for i:=1 to n do
order[i] := i;
m := n;
for i:=m downto 1 do
heapify(i);
while m>1 do
begin
swap(1,m);
dec(m);
heapify(1);
end;
t := 1;
ans[1] := p[order[1]]+bound/c[order[1]];
for i:=2 to n do
if (p[order[i]]-bound/c[order[i]] > ans[t]+eps) then
begin
inc(t);
ans[t] := p[order[i]]+bound/c[order[i]];
end
else
if (p[order[i]]+bound/c[order[i]] < ans[t]-eps) then
ans[t] := p[order[i]]+bound/c[order[i]];
ok := t<=k;
end;
procedure Solve;
var
l, r, m : extended;
begin
l := 0;

r := 1000000;
while r-l>1e-6 do
begin
m := (l+r)/2;
if ok(m) then
r := m
else
l := m;
end;
ok(r);
end;
procedure Save;
var
i : integer;
begin
for i:=1 to k do
writeln(ans[i]:0:4);
close(input);
close(output);
end;
begin
Load;
Solve;
Save;
end.
105.
: - 2002
: 40

(n, p, q), p q
n=pq. c=x2 mod n.

. -
x2 mod p=a, p .

p a, ,
.

x (0<=x<n), x2 mod p=a, ,
.


11
3

5
6

, a p
x2-a p.

. :
1. p+1 4.
, a - p, a(p-1)/2 mod p=1.
a(p+1)/2 mod p=a, , (p+1)/2 , - a(p+1)/4.
2. p+1 4.
k. x+k
(p-1)/2 x2-a p.
mx-n. m ( , k,
, ), nm-1 mod p.

program Kill_Rabin;
const
maxl = 200;
type
TLong = array [1..maxl] of integer;
var
p,a,t,r,zero,px,pxx : TLong;
lastmod : integer;
procedure ReadLong(var x : TLong);
var
s : string;
i : integer;
begin
readln(s);
fillchar(x,sizeof(x),0);
for i:=length(s) downto 1 do
x[i] := ord(s[length(s)+1-i]) - ord('0');
end;
procedure IncLong(var x : TLong);
var
i : integer;
begin

i := 1;
while (x[i]=9) do
begin
x[i] := 0;
inc(i);
end;
inc(x[i]);
end;
procedure DecLong(var x : TLong);
var
i : integer;
begin
i := 1;
while (x[i]=0) do
begin
x[i] := 9;
inc(i);
end;
dec(x[i]);
end;
procedure DivLong(var x : TLong; y : byte);
var
i,c,o : integer;
begin
o := 0;
for i:=maxl downto 1 do
begin
c := o * 10 + x[i];
x[i] := c div y;
o := c mod y;
end;
lastmod := o;
end;
function LongCompLong(x, y : TLong) : ShortInt;
var
i : integer;
begin
for i:=maxl downto 1 do
begin
if x[i] > y[i] then
LongCompLong := 1;
if x[i] < y[i] then
LongCompLong := -1;
if x[i] <> y[i] then
exit;
end;
LongCompLong := 0;
end;

procedure MulLong(var x : TLong; y : byte);


var
i,o,c : integer;
begin
o := 0;
for i:=1 to maxl do
begin
c := o + x[i] * y;
x[i] := c mod 10;
o := c div 10;
end;
end;
procedure LongAddLong(x, y : TLong; var z : TLong);
var
i,o,c : integer;
begin
o := 0;
for i:=1 to maxl do
begin
c := o + x[i] + y[i];
z[i] := c mod 10;
o := c div 10;
end;
end;
procedure ShiftLong(var x : TLong; y : Integer);
var
i : integer;
begin
for i:=maxl downto 1 do
if i>y then
x[i] := x[i-y]
else
x[i] := 0;
end;
procedure LongMulLong(x, y : TLong; var z : TLong);
var
i : integer;
t : TLong;
begin
t := y;
fillchar(z,sizeof(z),0);
for i:=1 to maxl do
begin
y := t;
MulLong(y,x[i]);
LongAddLong(z,y,z);
ShiftLong(t,1);
end;
end;

procedure LongSubLong(x, y : TLong; var z : TLong);


var
i,o,c : integer;
begin
o := 0;
for i:=1 to maxl do
begin
c := x[i] - y[i] - o;
o := ord(c < 0);
z[i] := c + o * 10;
end;
end;
procedure LongModLong(x, y : TLong; var z : TLong);
var
i : integer;
t : TLong;
begin
z := x;
for i:=maxl downto 1 do
begin
t := y;
ShiftLong(t,i-1);
while LongCompLong(t,z)<>1 do
LongSubLong(z,t,z);
end;
end;
procedure LongPowLong(x, y : TLong; var z : TLong);
begin
z := zero;
z[1] := 1;
while LongCompLong(y,zero)=1 do
begin
DivLong(y,2);
if lastmod=1 then
LongMulLong(z,x,z);
LongModLong(z,p,z);
LongMulLong(x,x,x);
LongModLong(x,p,x);
end;
end;
procedure Load;
begin
assign(input,'rabin.'+paramstr(1));
reset(input);
assign(output,'sqrt.'+paramstr(1));
rewrite(output);
ReadLong(p);
ReadLong(a);

fillchar(zero,sizeof(zero),0);
end;
procedure MulX(x1, x2, y1, y2 : TLong; var z1, z2 : TLong);
var
a_,b_,c_,t_ : TLong;
begin
LongMulLong(x1,y1,a_);
LongMulLong(x2,y2,c_);
LongMulLong(x1,y2,b_);
LongMulLong(x2,y1,t_);
LongAddLong(b_,t_,b_);
z1 := b_;
LongMulLong(a_,a,z2);
LongAddLong(z2,c_,z2);
LongModLong(z1,p,z1);
LongModLong(z2,p,z2);
end;
procedure Solve;
var
b : TLong;
ca,cb,da,db : TLong;
begin
b := p;
IncLong(b);
DivLong(b,4);
px := p;
DecLong(px);
pxx := px;
DecLong(pxx);
if lastmod=0 then
begin
LongPowLong(a,b,r);
end
else
begin
randomize;
repeat
ca := zero;
cb := zero;
da := zero;
db := zero;
ca[1] := 1;
cb[1] := random(10);
cb[2] := random(10);
db[1] := 1;
b := p;
DecLong(b);
DivLong(b,2);
while (LongCompLong(b,zero)=1) do
begin

DivLong(b,2);
if lastmod=1 then
MulX(ca,cb,da,db,da,db);
MulX(ca,cb,ca,cb,ca,cb);
end;
if LongCompLong(db,zero)=0 then
db := px
else
DecLong(db);
if LongCompLong(da,zero)=1 then
break;
until false;
LongSubLong(p,db,db);
LongModLong(db,p,db);
LongPowLong(da,pxx,r);
LongMulLong(r,db,r);
LongModLong(r,p,r);
end;
end;
procedure Save;
var
i,l : integer;
rr,t : TLong;
begin
LongSubLong(p,r,rr);
if LongCompLong(r,rr)=1 then
begin
t := r;
r := rr;
rr := t;
end;
for l:=maxl downto 1 do
if r[l]<>0 then
break;
for i:=l downto 1 do
write(chr(r[i] + ord('0')));
writeln;
for l:=maxl downto 1 do
if rr[l]<>0 then
break;
for i:=l downto 1 do
write(chr(rr[i] + ord('0')));
writeln;
close(input);
close(output);
end;
begin
Load;
Solve;
Save;

end.

106. RSA
: - 2002
: 40

,
. ,
,
RSA. Ni
(Ni=piqi, pi, qi - , i 1,2,3).
x, Ni. ,
ei=3 di.
, yi=xei mod
Ni.
,
yi. , ,
Ni yi ,
. : x.

6 : N1, N2, N3, y1, y2, y3 .

x.

161
187
247
55
29
216

6

. x3 mod Ni
i=1,2,3; x3 M=N1N2N3. x<Ni;
, x3<M, x3 M x3. x3,
x3 (, ).

107. -
: - 2002
: 30

-
. p, a Zp*,
b. indab, . . x, ax mod p=b.

p, a b.

x.

7
3
2

2

m=1+[p], [x] - x. , x m ,
, a=my+z, 0<=y,z<m. amy mod p=b-z mod p.
, y, - z. ,
y z,
- (y,z) , amy mod p=b-z mod p. a=my+z.