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

: , : 2008

Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)

.
, , ,
, ( , )
, ( ). ROI2002,
, ,
.
[, , -
, , , , , .]
, -
. , , :
(), ,
,
( ..)
, -. , , , ;
, , (
, ). ,
.
( )
, ( ) , backtracking.
, : ,
.
. ,
. , , ,
. , , n, k
, , .

I.
, - .
, , , .
1. 2k k .
: , , ,
. , k- : for i:=0 to (1 shl k) - 1 do
i k- . , ,
, , .

, , k- ( , , 2k ).

var a:array...
procedure check;
var i:integer;
begin
for i:=1 to k do
write(a[k]);
writeln;
end;

procedure find(i:integer);
begin
if i>k then begin
check;
exit;
end;
a[i]:=0;
find(i+1);
a[i]:=1;
find(i+1);
end;
begin
readln(k);
find(1);
end.
1 27

, . a, ,
.
check , : .
- , - ; .
f ind. a
i 1 , .. k . f ind(i) 2ki+1 ,
.. check 2ki+1 . (
i 1 , .. k i + 1, 2ki+1 ; i
, a.)
2. ?. , (..
). :)
.
, f ind(k + 1). , check .
. , f ind(k+1) ,
(k+1)1 , .. k , ..
.
f ind(k). : a[k] 0 f ind(k+1),
( ) . a[k] 1
f ind(k + 1), .
. , k 1
,
. .
f ind(k1). k2 ,
. f ind(k1).
a[k 1] f ind(k), , , 1 . , f ind(k) ,
f ind(k 1), a[k 1] f ind(k),
1- . , f ind(k 1)
2- , , ..
.
f ind(k 2). 3- .
a[k 2] f ind(k 1), , , 2 . a[k 2] f ind(k 1).
3- 2- , 2- ,
f ind(k 2) 3- .
[ . f ind(k 2) f ind(k 1) ..].
, f ind(i) i- . i + 1- , i + 1- .
f ind(i) a[i] f ind(i+1),
i + 1- , a[i] f ind(i + 1).
, f ind(1) k- (.. ,
), .. .
( , ) [ ,
. , ,
:)].
,

:





0

..


0



1

f ind.


0



0

,



1



f ind(1), 1



f ind(1).


0



0

f ind(1): .


1



1

:


0



1

f ind(2). f ind(2)



1
(f ind(1) ), . f ind(2) f ind(3)
f ind(3), . ( , , ). ,
2 27

f ind: a , (
, ),
f ind ( ).
a: , , , ..,
.
, , . , ,
, .
3. . ( k- ) , ,
..:
f ind
(
0
1
, ). , 0
1
0
1
, 0
1
0
1
0
1
0
1
, .
.
, , f ind.
4. check. , , , ,
check. ,
( k- ):
, , - ..
, ;
check . check ,
: , , ,
. . .
5. . , .
check . ,
, check .
,
. a ;
a.
a ; a
: .
:
procedure find(i:integer);
begin
if ( , .. ) then begin
check;
exit;
end;
a[i] begin
a[i]:= ;
find(i+1);
end;
end;

:
1. , . if i > k,
, (, ).
2. a[i]. -, , , .
, f or, if , ,
for j:=1 to n do if (j a[i]) then begin
a[i]:=j;
find(i+1);
end;

3 27

f ind ( , f ind
): , i 1 ,
. , i- , ,
f ind(i + 1), , i
.
check , . , - ( . ), .
, check .
6. k- n- . ( nk )
( , : ,
, , :
; .)
procedure find(i:integer);
var j:integer;
begin
if i>k then begin
check;
exit;
end;
for j:=0 to n-1 do begin
a[i]:=j;
find(i+1);
end;
end;

, , . 1 , 2 ,
n, .
7. N . : N , k ,
.
: , , , , , , , ,
.

k ; , a
.
procedure check;
var j:integer;
s:longint;
begin
s:=0;
for j:=1 to k do
s:=s+a[j];
if s=n then begin
for j:=1 to k do
write(a[j], );
writeln;
end;
end;

procedure find(i:integer);
var j:integer;
begin
if i>k then begin
check;
exit;
end;
for j:=0 to 30 do begin
a[i]:=1 shl j;
find(i+1);
end;
end;

-, check, ,
( , , , , ).
-, j. , ,
-, :
a[i]:=1;
while a[i]<1 shl 30 do begin
find(i+1);
a[i]:=a[i] shl 1;
end;

: , , , , ; , ,
f ind . ( 1: ,
?)
, .
2: (, ,
). ( , n , k,
n). , .
4 27

, , , . , ( ,
, , ).
8. n k (.. Cnk ). a
. : -, , , -, - ( {1, 3}
{3, 1} ).
3: , , check. .. f ind , check , .
. , .
: , a . f ind (, ,
, 0 n 1):
procedure find(i:integer);
var j:integer;
begin
if i>k then begin
check;
exit;
end;
for j:=0 to n-1 do if j>a[i-1] then begin
a[i]:=j;
find(i+1);
end;
end;

f or. ,
. , , f or
for j:=a[i-1]+1 to n-1 do

if ; , , ,
.
, , . , , , k = 3, (.. f ind(1)) a[1] = n1,
, (.. f ind(2)) . , k = 3,
a[1] = n 2, a[2] = n 1 .
4: ) . f ind(1); , (,
n k).
) , ( ) (, a[i] := j; a[i]=j, i
: ,
); , ,
. , , ,
.. , .
, f ind -. ,
, i x. i , , x ,
:
procedure find(i:integer;x:integer);
var j:integer;
begin
if i>k then begin
check;
exit;
end;
for j:=x to n-1 do
a[i]:=j;
find(i+1,j+1);
end;
end;

x a[i 1] + 1, , ,
.
f ind : , , x.
, f ind , , , ,
. , ()
, , .
5 27

9. n! n ( 1 n).
, , : ,
. , , ,
, , .
, .
, , was, ,
. .. ,
. (, a ).
var was:array...
procedure find(i:integer);
var j:integer;
begin
if i>n then begin
check;
exit;
end;
for j:=1 to n do if was[j]=0 then begin
a[i]:=j;
was[j]:=1;
find(i+1);
was[j]:=0;
end;
end;

-, , k, n
, .
-, f ind(i). ,
i- . (.. a),
if was[j]=0. , a, ,
f ind(i + 1) . j , .. was[j] , . ,
.
() 5: Akn n k
( , Cnk , ).

was[j]:=0;

.
. , , ,
. ( , ),
, , ,
, :
f ind , ( , ,
), f ind(i + 1).
f ind , j .
was[j]:=0;

, , .. f ind(i + 1) , i- j, ,
j () . , , ,
. , ; , .
, , I.2,
.
() ; .
, .
, .
.
,
type tWas=array...
var was:tWas;
procedure find(i:integer);
var j:integer;
oWas:tWas; {old was}
6 27

begin
if i>n then begin
check;
exit;
end;
oWas:=was; { }
for j:=1 to n do if was[j]=0 then begin
a[i]:=j;
was[j]:=1;
find(i+1);
was:=oWas; { }
end;
end;

, ,
, f ind, ,
( , ).
: , -, ,
was :
procedure find(i:integer);
var j:integer;
oWas:tWas; {old was}
begin
if i>n then begin
check;
exit;
end;
oWas:=was; { }
for j:=1 to n do begin
was:=oWas; {}
if was[j]=0 then begin
a[i]:=j;
was[j]:=1;
find(i+1);
end;
end;
end;

, , .. ,
f ind was , (
, ,
, ). .
, : , , a
. , .
, , . Borland Pascal
. , , BP , :
F9: breakpoint,
F8: ,
F7: , ,
( , F8), ,
.
F4: Run to cursor: , ,
.
, BP F8 F4 :
, ,
. ,
, , . : f ind
procedure find(i:integer);
begin
...
...
find(i+1);
...
end;

7 27

f ind(i + 1); F8, , f ind(i + 1)


, - f ind . ,
. , F8 i = 1
, i = 1, ( ) i = k .. (..
, f ind(i + 1);). , ..
i , , , ,
, .
. F8, .. ,
breakpoint i =< , >, F9.
, , .
Delphi ( FP?) .
10. I. , - ( )
( ), II. (
) , II,
, :) (
), - , .
, II, . , ,
I , I (
I), . ( , ), . . .
, . . .
6: 0 1 k ,
n . (, k = 2 n = 3 010, 011, 101,
110 111). , ,
( ) .
) , 1, .
0 1 n, , , check.
) , , ..
, check
.
) ( , )
, k (..
k = 2 , k = 3, k = 4 ..) , ,
k = 1 :)
7: . 2N (.. ) . N , (..
: ). [ , ..
. ]. [..
]
) , ,
(.. ).
) , , ,
.
8: N .
1: k
) , (.. 2 + 1
1 + 2 );
) , , ..
2 + 1 1 + 2, ;
) , , .
2: (, )
, , : (
!) ) n = 3 1 + 2 2 + 1. n = 2
1 + 1 ( )?
9: . N .
( ), ,
. ( )
.
8 27

( ) , .
,
.
10: (- ,
). m n ,
. : .
. : ,
1. , .

II.
, , , . :
, ,
, (,
N k k , N )
.., ,
.
, ,
.
, , , . ..
, .
1. N k . :
procedure find(i:integer);
var j:integer;
begin
if i>k then begin
check;
exit;
end;
for j:=0 to 30 do begin
a[i]:=1 shl j;
find(i+1);
end;
end;

- . .. , f ind a[i], .
-, , a[i], n. , s, , a[i],
n s:
var s:...
procedure find(i:integer);
var j:integer;
begin
if ....
end;
for j:=0 to 30 do if 1 shl j<=n-s then begin
a[i]:=1 shl j;
s:=s+a[i];
find(i+1);
s:=s-a[i]; { !!!}
end;
end;

, (,
check, I).
, -:
var s:...
procedure find(i:integer);
var j:integer;
begin
if i>k...
end;
if s>=n then
exit;
for j:=0 to 30 do begin
a[i]:=1 shl j;
s:=s+a[i];
find(i+1);
s:=s-a[i]; { !!!}
end;
end;
9 27

.. f ind. , a[i] > ns,


, f ind(i + 1), , .. s > n.
, (>, >), .. if i>k.
( , , , ,
; ).
11: , s,
s ( n s, .. ) .
f ind ( ,
), . f ind ,
: ( s > n), exit,
,
. , , f ind(i) k i + 1
. (.. 20 ), , s + k i + 1 > n,
. , :)
, , , ,
. , , ..
, ,
.
2. . :
( ..),
, , ,
(, ).

, (. ).
, ,
, (
: s > n, ).
, .
, .
.
: . .
; .
3. . . 10 ( !)
, , .
: m nn ,
, .
-, i = 1 (. ). ,
, . , ,
: , , .
, , .
( . ):
procedure find(i,k:integer);
var j:integer;
prev:integer;
begin
if i>nn then begin
check;
exit;
end;
if k<=0 then
exit;
if i=1 then begin
for j:=1 to k do begin

a[1]:=j;
find(2,k-j);
end;
end else begin
prev:=a[i-1];
a[i]:=prev;
find(i+1,k-a[i]);
a[i]:=prev+1;
find(i+1,k-a[i]);
end;
end;

, . ,
. , , . :
, . , ,
, . nn i + 1,
prev , , k < prev (nn i + 1), .
, , ?
prev + 1 , prev + 2 .. nn i + 1- prev + nn i + 1,
( ) (prev + 1 + prev + nn i + 1) (nn i + 1)/2. ,
10 27

k > (prev + 1 + prev + nn i + 1) (nn i + 1)/2, .

if (k<prev*(nn-i+1))or (k>(prev+1+prev+nn-i+1)*(nn-i+1) div 2) then


exit;

( ):
{$A+,B-,D+,E+,F-,G-,I+,L+,N+,O-,P-,Q+,R+,S+,T-,V-,X+,Y+}
{$M 65520,0,655360}
var a:array[1..100] of integer;
n,m:integer;
nn:integer;
ans:longint;
res:longint;
procedure check;
var i:integer;
s:integer;
begin
s:=0;
for i:=1 to nn do
s:=s+a[i];
if s<>m then
exit;
for i:=2 to nn do
{ , .
}
if (a[i]<>a[i-1])and(a[i]<>a[i-1]+1) then begin
writeln(!!2);
halt;
end;
inc(ans);
end;
procedure find(i,k:integer);
{k --- }
var j:integer;
prev:integer;
begin
if i>nn then begin
check;
exit;
end;
if k<=0 then begin
{ ,
- .
,

if.}
exit;
end;
if i=1 then begin
{i=1 , ..
.
, }
for j:=1 to k do begin
a[1]:=j;
find(2,k-j);
end;
end else begin
prev:=a[i-1];
if (k<prev*(nn-i+1))or
(k>(prev+1+prev+nn-i+1)*(nn-i+1) div 2) then
exit;
a[i]:=prev;
find(i+1,k-a[i]); {k-a[i] }
a[i]:=prev+1;
find(i+1,k-a[i]);
end;
end;
function count(n,m:integer):longint;
begin
ans:=0;
nn:=n;
if n>0 then
find(1,m);
count:=ans;
end;
begin
read(n,m);
res:=count(n-1,m)+2*count(n,m)+count(n+1,m);
{ n=1, count n-1=0.
if n>0 count}
writeln(res);
end.

. , n, m 6 100
(, n = 27, m = 100) , ,
. , .
: , ( ,
, , )
. , , :
, , ..
. , , , ..
() , :
, .. , ( :
check inc(ans), ,
, . , ,
: , check inc(ans); ,
. check , inc(ans),
).
: , ,
, . .
: , , . ,
, , . . ( , . , , . , ,
check; , 2 , ..
).
, , ,
( check , ); ,
11 27

, .
, , . ,
.
12: () , ,
i = 1 . , ,
.
(.. ) :
. , ,
, . , , . .. , :
? ?
? (, , , ) , ,
, , .. ? (
: , ,
if ? ?) , ,
- (. ); , ;
, , , .
, , , .
, i = 1 :
, , ,
. .
, , ( ,
Cnk .. : i = 1 , ,
. , , a[0], ,
). , i = 1
. , f ind,
. . . , , . ?
, , .

4. . .
9 .
( , , ,
. , . ,
.)
, ,
. check , , ,
, , . :
var a,b,ans:array...
nn:integer;
cur,best:longint;
procedure insert(i:integer;v:integer);
var j:integer;
begin
for j:=nn downto i do
b[j+1]:=b[j];
b[i]:=v;
inc(nn);
end;
function delete(i:integer):integer;
var j:integer;
begin
delete:=b[i];
for j:=i+1 to nn do
b[i-1]:=b[i];
dec(nn);
end;
procedure check;
begin

if cur<best then begin


best:=cur;
ans:=a;
end;
end;
procedure find(i:integer);
var j:integer;
x:integer;
begin
if nn=2 then begin
check;
exit;
end;
for j:=2 to nn-1 do begin
a[i]:=j;
cur:=cur+b[j]*(b[j-1]+b[j+1]);
x:=delete(j);
find(i+1);
insert(j,x);
cur:=cur-b[j]*(b[j-1]+b[j+1]);
end;
end;

. a, , , .
. Ans .
Cur ( , ), best
. b , nn .
delete b, nn, . insert : .
12 27

, (.. insert delete


), , ,
. check , , .
f ind . . -,
2 (nn = 2; , , i = n 2),
( , ), check exit.
, . a, cur b ( delete),
: f ind(i + 1). !
, , . :
. cur b :
procedure find(i:integer);
var j:integer;
ocur:... {old cur}
ob:.. {old b}
begin
if nn=2 ...
end;
ocur:=cur;
ob:=b;

for j:=2 to nn-1 do begin


a[i]:=j;
cur:=cur+b[j]*(b[j-1]+b[j+1]);
delete(j);
find(i+1);
b:=ob;
cur:=ocur;
end;
end;

, x . , ..
f ind ob.
a , .
, a (, , ans) .
i; f ind (!),

( nn).
13: a i. .
14: , .
, . -, ,
, cur > best, , .
if cur>=best then exit. ( ), ,
.
: . if cur>=best, if cur>best. ,
. ,
cur > best.
. , ,
(, , ,
> 2).
. , ( ) s,
cur+2s > best. .
(
. ).
15: :
.
, , . ,
, 2, ..
, , , :)
, . .. .
16: ( 7,
: ). .

III.
. ,
, , ..
best. best, ..
, .
. , -,
:
-, - ,
-, , , ,
13 27

-, , , - .
.
1. .
- , ,
. .. -
, - . ( ),
( , ),
, .
, . ,
, , .
,
: , ,
, .., :
best:=0;
ob:=b;
for i:=1 to n-2 do begin
{, }
min:=inf;
{, }
for j:=2 to n-2 do
if b[j]*(b[j-1]+b[j+1])<min then begin
minj:=j;
min:=b[j]*(b[j-1]+b[j+1]);
end;
{ , ans}
best:=best+min;
ans[i]:=j;
delete(minj);
end;
b:=ob;

, f ind(1), .. (
f ind(1)). best
ans, .. , (
, best -
, inf ).
, , ; , , , .
, best = inf .
17: , .
18: .
.
( , ), , , . ,
() , , . (
) .
( , ; ,
. ).
. , . , , .
: ,
. ,
(, : , , -, , -,
, . ,
, .. ).
; , :
, . .
, ,
(!) , . :
, . , , ,
best ans .
.. best ,
.
14 27

19: . , ?
, ,
, . ,
; , ,
(, ),
qsort .. , N ,
, , N , .
, , .. ( ) - . :
, - ,
-, , ! , ..
...
2. .
, . , ,
, .
, f ind(i) ,
, - (for j:=2 to nn-1), ,
, , . ..: . f ind(i + 1).
, , f ind(i + 1). ..
, was ,
:
procedure find(i:integer);
var j,k:integer;
x:integer;
was:array...
maxj:integer;
max:integer;
begin
if nn=2 then begin
check;
exit;
end;
fillchar(was,sizeof(was),0);
for k:=2 to nn-1 do begin
{ ,
}
max:=0;
for j:=2 to nn-1 do

if (was[j]=0)and(b[j]>max) then begin


max:=b[j];
maxj:=j;
end;
{ }
was[maxj]:=1;
a[i]:=maxj;
cur:=cur+b[maxj]*(b[maxj-1]+b[maxj+1]);
x:=delete(maxj);
{, }
find(i+1);
{ }
insert(maxj,x);
cur:=cur-b[maxj]*(b[maxj-1]+b[maxj+1]);
end;
end;

, was , . , :
f ind was. f ind(5) (.. f ind(1), f ind(2), . . . ,
f ind(5), ), , ;
f ind(4) ( f ind(5)) .. , .
, , ( ) (.. , ( ,
) ..).
20: .
, , .
, , , , , .
, ,
, :)
. , , . , check? , ,
.., .. , III.1 -. 19 ,
, ,
. , , .
3. . ,
1 . : - . , . ,
1 , , , . .

15 27

. . , , -. .. n(n 1)/2 (uv)


(u0 v 0 ), , ,
(uv 0 ) (u0 v), - : ( a )
for i:=1 to n do
for j:=i+1 to do begin
{ }
t:=a[i].a;
a[i].a:=a[j].a;
a[j].a:=t;

a .
end;

, , ,
-.
, . : .
, . , ,
, , . , .
21:
( 7, : ). .

IV.
1. . , . ,
0 :) , , , , - .
:
(.. ) ,
, ,
. ,
, , TL, .
, , (, , ). ,
, :
. ( )
, , ,
( . ).
, , . , ( , ,
1432 , . . .
? :) , , , 1432 ),
, , .
: , , ,
: TL, TL,
TL , WA , OK , .

? Borland Pascal ( DOS-)


: 0:$46 (.. 46C16 )
, longint. (!)
18 ( ).
:
var t:longint absolute 0:$46c;
{ absolute ,
}
t0:longint;
procedure find(...)
begin
if i>k...
end;
if t>t0+18 then begin {, 1 }
out; { out }
halt;
16 27

end;
...
end;
begin { }
t0:=t; { t0}
...
end.

, 17, 18, , ..
Windows- , ,
, . , getT ickCount, ,
f ind .
nn, ,
f ind, , , 1024- . :
var t:longint;
nn:longint;

...

procedure find...
begin
if i>k...
end;
if (n and 1023=0)and(gettickcount>t0+1000) then begin
...
end;
inc(nn);
...

begin
t0:=gettickcount;
nn:=0;
...
end.

(n0 and 1023, n0 mod 1024, ; 1024, 1000 ). , ,


: 65 536- ..;
, , .
, , -:
if (n=1000) then begin
n:=0;
if (gettickcount>t0+1000) then begin
...
end;
end;
inc(nn);

.. . ,
.
k, , .
, .
2. . , ,
( , ). , ,
N N . :
procedure find(i,j:integer); {i,j --- , }
begin
if i>n then begin { }
check;
exit;
end;
if j>n then begin { }
find(i+1,1);
{ }
exit;
end;
a[i,j]:=0;
find(i,j+1);
a[i,j]:=1;
find(i,j+1);
end;

.
3. . ( , .) , ,
, . , N 2 , , (.. , ).
, , , : ..
, .. . . : f ind(1) (1, 1),
f ind(2) (1, 2), . . . f ind(n) (1, n), f ind(n + 1) (2, 1) ! f ind(n + 2) (3, 1), f ind(n + 3) (4, 1)
.., , ..,
N = 5 , .
17 27

, : 1 2
3
4
5
, ,
6 10 11 12 13
, N (,
7 14 17 18 19
). 8 15 20 22 23
, .
9 16 21 24 25
, ( N ), (2N ), (3N ), ..,
.
(
), : ( N ), (
2N 1, 2N (!)), (3N 2, 3N ) .. ..
.
, , , .
4. . . ,
, , , :
.
. , ,
, , rewrite , , .
check, .
: .

procedure check;
var i:integer;
begin
if cur<best then begin
best:=cur;
rewrite(f);
writeln(f,cur);
end;
if cur=best then { , }
for i:=1 to n-2 do write(f,a[i], );
end;

.. , rewrite , ,
(, , ), best := cur.
, cur = best,
(.. rewrite ..), ,
, .
, ans .
, (.. >),
(>).
, . .
ans,
procedure check;
var i:integer;
begin
if cur<best then begin
best:=cur;
rewrite(f);
writeln(f,cur);
for i:=1 to n-2 do write(f,a[i], );
close(f);
end;
end;

close(f ) halt
,
, halt.

18 27

V.
1 (. 4): , ?
2 (. 4): (, ,
). ( , n , k,
n). , .
, , , .
, ( , ,
, ).
3 (. 5): , , check. .. f ind
, check , . . ,
.
4 (. 5): ) . f ind(1);
, (,
n k).
) , ( ) (, a[i] := j; a[i]=j, i
: , );
, ,
. , , , ..
, .
() 5 (. 6): Akn
n k ( , Cnk , ).
6 (. 8): 0 1 k
, n . (, k = 2 n = 3 010,
011, 101, 110 111). ,
, ( ) .
) , 1, . 0
1 n, , , check.
) , , .. , check
.
) ( , )
, k (.. k = 2 ,
k = 3, k = 4 ..) , , k = 1 :)
7 (. 8): . 2N (.. ) . N , (..
: ). [ , ..
. ]. [..
]
) , , (.. ).
) , , , .
8 (. 8): N .
1: k
) , (.. 2 + 1 1 + 2
);
) , , .. 2 + 1
1 + 2, ;
) , , .
2: (, )
, , : ( !) ) n = 3 1 + 2 2 + 1. n = 2
1 + 1 ( )?
9 (. 8): . N . ( ), ,
19 27

. ( )
.
( ) , .
,
.
10 (. 9): (- , ). m n , . : .
. :
,
1. , .
11 (. 10): , s,
s ( n s, .. ) .
12 (. 12): () , ,
i = 1 . , ,
.
13 (. 13): a i.
.
14 (. 13): , .
15 (. 13): :
.
16 (. 13): ( 7, : ). .
17 (. 14): , .
18 (. 14): . .
19 (. 14):
. , ?
20 (. 15): .
21 (. 16): ( 7, : ). .

20 27

VI.
1 (. 4): , while.
2 (. 4): .
4 (. 5): ) ; ) ,
.
6 (. 8): ) , , k 1 . ,
, f ind
. .
7 (. 8): ) ) check
(. II). f ind :
N . , , ,
, f ind(i) , i- - .
:
1. (!):
array of record a,b:integer; end;

i f ind , ( , i = 1 ,
, i = 2 ..).
f ind , , ..
, , . ,
, , was, ,
( !)
: N
, a.
2. , , . a
: .. a[i] , i, , .
, a[a[i]] = i. f ind(i)
i- . , - , , . was , ..
, a[i] = 0. ,
a! , ,
.
8 (. 8): , , f ind , ,
. -: f ind , , (.. N ( )).
. 2: , .
9 (. 8): a ( ,
a ). ( a)
, ,
. ,
( ), . .
, ,
.
10 (. 9): ,
, m .
.. count(g, m), m g
, count(n 1, m) + 2 count(n, m) + count(n + 1, m),
:
1. , n1
count(n 1, m).
2. , , n
count(n, m).
3. , , n
- count(n, m).
4. , n+1 count(n+
+ 1, m).
count (..
, f ind(1)). a . 1 check, .
21 27

, () .
, -, ,
, , -, ,
; II.3.
12 (. 12): ;
nn m f ind . -, a
, : count:
ans:=0;
nn:=n;
if n>0 then begin
for i:=1 to nn do begin
a[0]:=i;
find(1,m);
end;
count:=ans;

f ind , ; count .
,
(, 3, a[0] = 2 a[0] = 3). ,
.
, , -,
27 100 ( 94762), :)
14 (. 13): , .
f ind ,
, ( ), ..
.
16 (. 13): ) - ; )
: .
) - ! , , , .
17 (. 14): , , ,
, , ,
, . , , , ,
21 (. 16): , , : ,
. ,
.. - .

22 27

VII.
1 (. 4): a[i] 1 shl 29, , , 1 shl 30, . 1 shl
30 .
2 (. 4): , check .
,
procedure check;
var j:integer;
s:longint;
begin
s:=0;
for j:=1 to k do if s<=n-a[j]
s:=s+a[j]
else exit;
if s=n then begin
for j:=1 to k do
write(a[j], );
writeln;
end;
end;

then

.
, , .
for j:=0 to 30 do if 1 shl j>=a[i-1] begin

3 (. 5): , ,
.. ,
4 (. 5): ) f ind(1) a[0]. , f ind(1)
a[0] = 1 :), .. (
, 0 n1, 1 n:
a[0] = 0 :) ).
) , f ind(i) a[i] = n 1, i k. , ,
a[i] > n (k i) 1 ( , 1, ), ..
. a[i 1] + 1 n (k i) 1.
() 5 (. 6): , ,
if i>k, if i>n.
6 (. 8): ) : ,
k 1 . k = 2 :
procedure find(i:integer);
begin
if....
end;
a[i]:=1;
find(i+1);
if a[i-1]=1 then begin { , --- 1}
a[i]:=0;
find(i+1);
end;
end;

, a[0] = 1, .
k , ,
( a[0], a[1] ..,
) !, , f ind
:
procedure find(i,l:integer);
begin
if...
end;
a[i]:=1;
find(i,0); { , .. :) }
if l<k-1 then begin { }
a[i]:=0;
find(i+1,l+1); { }
end;
end;

f ind(1, 0) a[0] ..
) " ".
23 27

7 (. 8): ) ) ( ,
f ind ); II .
1 :
var a:array... of record a,b:integer; end;
was:array...
procedure find(i:integer); { i- }
var j,k:integer;
begin
if i>n then begin
check; { check }
exit;
end;
{ }
for j:=1 to 2*n do { 2*n !}
if was[j]=0 then break;
{ j --- ( ) .
}
was[j]:=1;
for k:=1 to 2*n do { k:=j+1 to 2*n, .. j- }
if was[k]=0 then begin { , , check}
was[k]:=1;
a[i].a:=j;
a[i].b:=k;
find(i+1);
was[k]:=0;
end;
was[j]:=0;
{ , !
was[j]:=0 , was[j]:=1 !}
end;

2 :
var a:array... of integer;
procedure find(i:integer); { i- }
var j:integer;
begin
if i>2*n then begin { --- 2*n, n!}
check;
exit;
end;
if a[i]<>0 then { , }
find(i+1)
else { }
for j:=i+1 to 2*n do if a[j]=0 then begin {i+1 --- .. i- }
{ i- j- }
a[i]:=j;
a[j]:=i;
find(i+1);
a[i]:=0;
a[j]:=0;{!!!, .. i- j- !}
end;
end;

8 (. 8): , , , ) ,
, ) , ) .
1): s, .
var s:...
procedure find(i:integer);
var j:integer;
begin
if i>k then....
end;
for j:=a[i-1]+1 to n-s do begin { , , n-s}
a[i]:=j;
s:=s+j;
find(i+1);
s:=s-j; { !!!}
end;
end;

, check , s = n.
1 1 : a[i 1] 1 .
24 27

2 , , . ,
if s=n, check .
-, s, check
rem; f ind rem -
. 2:
procedure find(i:integer;rem:integer);
var j:integer;
begin
if rem=0 then begin { rem=0, N, .. }
check;
exit;
end;
for j:=a[i-1]+1 to rem do begin
a[i]:=j;
find(i+1,rem-a[i]); { rem-a[i]}
end;
end;

f ind , .. .
9 (. 8): II.4.
10 (. 9): II.3.
11 (. 10): :) s
procedure find(i:integer;s:integer);
var j:integer;
begin
if ....
end;
if s>=n then
exit;
for j:=0 to 30 do begin
a[i]:=1 shl j;
find(i+1,s+a[i]);
end;
end;

f ind(1, 0);
nn = n s
procedure find(i:integer;nn:integer);
var j:integer;
begin
if ....
end;
if nn<=0 then
exit;
for j:=0 to 30 do begin
a[i]:=1 shl j;
find(i+1,nn-a[i]);
end;
end;

, f ind f ind(1, n).


13 (. 13): , , .
procedure find;
var j:integer;
ocur:...
ob:..
begin
if nn=2 then begin
check;
exit;
end;
ocur:=cur;

ob:=b;
for j:=2 to nn-1 do begin
cur:=cur+b[j]*(b[j-1]+b[j+1]);
delete(j);
find;
b:=ob;
cur:=ocur;
end;
end;

( insert ob ocur).
. , .
f ind b .
? , . ,
, f ind . f ind ,
, . . . , , I
..
14 (. 13): ; ,
record .
25 27

type pnode=^tnode;
tnode=record prev,next:pnode; val:integer; end;
{ }
var head:pnode { }
procedure find(i:integer);
var j:integer;
ocur:...
p:pnode;
begin
if nn=2 then begin
check;
exit;
end;
ocur:=cur;
p:=head^.next; { }
j:=2;

while p^.next<>nil do begin


{ ;
,
!}
a[i]:=j;
cur:=cur+p^.val*(p^.prev^.val+p^.next^.val);
p^.prev^.next:=p^.next;
p^.next^.prev:=p^.prev; { p }
find(i+1);
cur:=ocur;
p^.prev^.next:=p;
p^.next^.prev:=p; { }
p:=p^.next; { }
inc(j);
end;
end;

, , .
p, ( p
x ).
, ,
.
insert delete,
( N ) . , , ,
, , / , , .
, j. ,
, a.
15 (. 13): , .
:
var s:...
procedure find(i:integer);
var j:integer;
ocur:...
ob:..
begin
if nn=2 then begin
check;
exit;
end;
if cur+2*s>=best then exit;
ocur:=cur;

ob:=b;
for j:=2 to nn-1 do begin
a[i]:=j;
cur:=cur+b[j]*(b[j-1]+b[j+1]);
s:=s-b[j];
delete(j);
find(i+1);
b:=ob;
cur:=ocur;
s:=s+b[j];{ }
end;
end;

:
procedure find(i:integer);
var j:integer;
ocur:...
ob:..
s:...
begin
if nn=2 then begin
check;
exit;
end;
s:=0;
for j:=2 to nn-1 do
s:=s+b[j];
if cur+2*s>=best then exit;

ocur:=cur;
ob:=b;
for j:=2 to nn-1 do begin
a[i]:=j;
cur:=cur+b[j]*(b[j-1]+b[j+1]);
s:=s-b[j];
delete(j);
find(i+1);
b:=ob;
cur:=ocur;
s:=s+b[j];{ ,
.. j}
end;
end;

16 (. 13): ,
, .
17 (. 14): , :
2 1 100 1
1(2+100) = 102,
100 (2 + 1) = 300. , ,
, .. 100 (1 + 1) = 200,
1 (2 + 1) = 3, .
, , .
- .
18 (. 14): , , :
, , .
26 27

19 (. 14): .
20 (. 15): :)
procedure find(i:integer);
var j,k:integer;
x:integer;
was:array...
minj:integer;
min:integer;
begin
if nn=2 then begin
check;
exit;
end;
fillchar(was,sizeof(was),0);
for k:=2 to nn-1 do begin
min:=inf; {}

for j:=2 to nn-1 do


if (was[j]=0)and(b[j]*(b[j-1]+b[j+1])<min) then begin
min:=b[j]*(b[j-1]+b[j+1]);
minj:=j;
end;
was[minj]:=1;
a[i]:=minj;
cur:=cur+b[minj]*(b[minj-1]+b[minj+1]);
x:=delete(minj);
find(i+1);
insert(minj,x);
cur:=cur-b[minj]*(b[minj-1]+b[minj+1]);
end;
end;

21 (. 16): , .

27 27

Оценить