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

,

,
2013


. Akai . . . . . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .
. . . . . . . . . . . . . . . . . . . . . . . . .
A. Automaton . . . . . . . . . . . . . . . . . . . . . . . .
B. Bims . . . . . . . . . . . . . . . . . . . . . . . . . . .
C. Cutting . . . . . . . . . . . . . . . . . . . . . . . . .
D. Disclosure . . . . . . . . . . . . . . . . . . . . . . . .
E. Embedded circles . . . . . . . . . . . . . . . . . . . .
F. False figures . . . . . . . . . . . . . . . . . . . . . . .
G. Grouping . . . . . . . . . . . . . . . . . . . . . . . .
H. Hidden triangles . . . . . . . . . . . . . . . . . . . .
I. Interactive . . . . . . . . . . . . . . . . . . . . . . . .
J. Journey . . . . . . . . . . . . . . . . . . . . . . . . . .
K. Knuth knows . . . . . . . . . . . . . . . . . . . . . .
L. Lake . . . . . . . . . . . . . . . . . . . . . . . . . . .
M. Match them up . . . . . . . . . . . . . . . . . . . . .
N. Need for sum thing . . . . . . . . . . . . . . . . . . .
O. Open air . . . . . . . . . . . . . . . . . . . . . . . . .
P. Pseudo automaton . . . . . . . . . . . . . . . . . . . .
Q. Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . .
R. Reduction . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
A. . . . . . . . . . . . . . . . . .
B. 1 . . . . . . . . . . .
C. .
D. . . . . . . . . . . . . . . . . .
E. . . . . . . . . . . . . . . . . . . . .
F. . . . . . . . . . . . . . . . . .
G. . . . . . . . . . . . . . . . . . . . .
H. . . . . . . . . . . . . . . . . .
I. . . . . . . . . . . . . . . . . . . . . . . . . . . .
J. . . . . . . . . . . . . . . .
K. . . . . . . . . . . . . . . .
L. . . . . . . . . . . . . . . . . . . . . .
M. . . . . . . . . . . . . . . . . . . .

, , 15-25 2013

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
8
.
8
.
9
. 13
. 13
. 15
. 17
. 19
. 20
. 22
. 24
. 26
. 28
. 30
. 31
. 33
. 34
. 36
. 38
. 40
. 41
. 44
. 45
. 45
. 45
. 50
. 50
. 51
. 52
. 53
. 55
. 57
. 58
. 61
. 62
. 63
. 65
. 66
. 69
3

. . . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. STL (, , , ) . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A. DOMA 2: Last Hit . . . . . . . . . . . . . . . . . . . . . . .
B. Last Effect 3: Danger . . . . . . . . . . . . . . . . . . . . . .
C. Fineage: Training . . . . . . . . . . . . . . . . . . . . . . . .
D. DOMA 2: Inventory . . . . . . . . . . . . . . . . . . . . . .
E. South Mark: 3.50 . . . . . . . . . . . . . . . . . . . . . . . .
F. HOLM 2: The Great Battle . . . . . . . . . . . . . . . . . . .
G. Failout Few Vegas: Slow Save . . . . . . . . . . . . . . . . .
H. Carmarandom TDC2013: New Trace . . . . . . . . . . . . .
I. X-Dom: Railway . . . . . . . . . . . . . . . . . . . . . . . . .
J. MindCraft: Heliport . . . . . . . . . . . . . . . . . . . . . . .
K. Double Life: Amplifiers . . . . . . . . . . . . . . . . . . . . .
. . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A. High . . . . . . . . . . . . . . .
B. Junior . . . . . . . . . . . . . .
C. High . . . . . . . . . . . . . . . . . .
D. Junior . . . . . . . . . . . . . . . . .
E. High . . . . . . . . . . . . . . . . . . . . .
F. Junior . . . . . . . . . . . . . . . . . . . .
G. - High . . . . . . . . . . . . . . . . . . . .
H. - Junior . . . . . . . . . . . . . . . . . . .
I. High . . . . . . . . . . . . . . . . . . . . . . .
J. Junior . . . . . . . . . . . . . . . . . . . . . .
K. High . . . . . . . . . . . . . . . .
L. Junior . . . . . . . . . . . . . . .
M. High . . . . . . . . . . . . . .
N. Junior . . . . . . . . . . . . . .
O. High . . . . . . . . . . . . . . . . . . . . .
P. Junior . . . . . . . . . . . . . . . . . . . . .
Q. High . . . . . . . . . . . . . . . . . . .
R. Junior . . . . . . . . . . . . . . . . . .
S. High . . . . . . . . . . . . . . . . . . . .
T. Junior . . . . . . . . . . . . . . . . . . .
4

72
72
72
84
84
86
88
89
92
94
97
99
101
103
105
108
108
109
115
115
116
121
122
125
126
128
129
131
133
136
136
138
138
140
141
142
143
146
147


, , 15-25 2013

. . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . . . . . . . . . .
A. . . . . . . . . . . . . . . . . .
B. . . . . . . . . . . . . .
C. . . . . . . . . . . . . . . . .
D. . . . . . .
E. . . . . . . . . . . . . . . . . . . . .
. . .
. C . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
A. - . . . . . . . . . . . . . . . . . . .
B. - . . . . . . . . . . . . . . . . . . . . . . .
C. . . . . . . . . . . . . . . .
D. - . . . . . . . . . . . . . . . . . . . . .
E. - . . . . . . . . . . . . . . .
F. . . . . . . . . . . . . . . . . .
G. . . . . . . . . . . . . . . . . . .
H. - . . . . . . . . . . . . . . . . . . . . . .
I. - . . . . . . . . . . . . . . . . . . . . . . .
J. . . . . . . . . . . . . . . . . . .
K. . . . . . . . . . . .
L. . . . . . . . . . . . . . . . .
M. . . . . . . . . . . . . . . . . . .
N. . . . . . . . . .
. K . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
A. Chords . . . . . . . . . . . . . . . . . . . . . . . . .
B. Cyclic suffixes . . . . . . . . . . . . . . . . . . . . .
C. A Coloring Game . . . . . . . . . . . . . . . . . . .
D. Hippopotamus . . . . . . . . . . . . . . . . . . . . .
E. False RSA . . . . . . . . . . . . . . . . . . . . . . .
F. Perspective . . . . . . . . . . . . . . . . . . . . . . .
G. Circular Railway . . . . . . . . . . . . . . . . . . .
H. SETI . . . . . . . . . . . . . . . . . . . . . . . . . .
I. 2-3 Trees . . . . . . . . . . . . . . . . . . . . . . . .
. . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . .

, , 15-25 2013

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

149
149
150
150
150
152
154
157
159
162
162
166
166
168
169
171
173
176
179
181
183
185
186
189
191
192
195
195
195
200
200
201
203
204
205
206
208
209
210
213
213
5

. . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
A. . . . . . . . . . . . . . . . . . . .
B. . . . . . . . . . . . . . . . . . . . . . .
C. . . . . . . . . . . . . . . . . . .
D. . . . . . . . . . . . . . . . . . . . . . . . . .
E. . . . . . . . . . . . . . . . .
F. Shortest Path . . . . . . . . . . . . . . . . . . . . . .
G. . . . . . . . . . . . . . . . . . . . . . . .
H. . . . . . . . . . . . . . . . . . . . . . . . . . .
I. . . . . . . . . . . . . . . . . .
J. . . . . . . . . . . . . . . . . . . . . . . . .
K. . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
A. . . . . . . . . . . . . . . . . . . .
B. . . . . . . . . .
C. . . . . . . . . .
D. . . . . . . . . . . . . . . . . . . . . . . . . .
E. . . . . . . . . . . . . . . . . .
. .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. 20
. . . . . . . . . . . . . . . . . . . . . . . . .
A. n . . . . . . . . . . . . . . . .
B. . . . . . . . . . . . . . . . . . . . . .
C. . . . . . . . . . . . . . . . . . . . . . .
D. . . . . . . . . . . . . . . . . . . . . . . .
E. . . . . . . . . . . . . . . . . . . . . . . . . .
F. . . . . . . . . . . . . . . . . .
G. . . . . . . . . . . . . . . . . . . . .
H. . . . . . . . . . . . . . . . . . .
I. K . . . . . . . . . . . . . . . .
J. . . . . . . . . . . . . . . . . . . . .
K. . . . . . . . . . . . . . . . .
L. . . . . . . . . . . . . . . . . . . . .
M. . . . . . . . . . . . .
N. . . . . . . . . . .
O. . . . . . . . . . .
6

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

214
216
216
219
221
229
233
235
236
238
239
240
244
248
250
250
250
250
253
255
257
260
263
263
264
280
280
281
283
284
285
287
288
289
291
292
293
294
296
297
298


, , 15-25 2013

P. k- . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Q. . . . . . . . . . . . . . . . . . . . . 300
R. . . . . . . . . . . . . . . . . . . . . . . . . . 301


, , 15-25 2013

Akai

(16.02.2013 .)
Akai
...
, 28 1988 . . .. . . .
:
Akai :

2009 6
SEERC, .
2010 3 .
2010 4 SEERC
.
2011 1 II .
2011 2 SEERC
.
2010 2012 1 SEERC
..
Russian Code Cup 2011 2012 .
3 Russian AI Cup 2012.


, , 15-25 2013

Akai

. .

N M ,
1 1. , . .
,

: , , , .
: (U ), (D), (L), (R). .
, . . ,
, .
. : S = a1 a2 a3 . . . al ,
ai {L, R, U, D} , . :

i, (LR, RL, U D, DU ). , S 0 = a1 a2 . . . ai1 LRai . . . al .


. i
.
i, ai ai . ,
S 0 = a1 a2 . . . ai1 Lai Rai+1 . . . al .
. i i 1 i + 1.


, , 15-25 2013

Akai

() ()

, ,
.

.
. , ,
.
,
. , ,
.
10


, , 15-25 2013

Akai

.
,
. , ,
.
. ( ).

.
, , , .
:


, , 15-25 2013

11

Akai

(3, 2)
RRRRDDRU U RDDDLDLLLU LDDLU U U U U . [4, 6, 6, 4, 4, 6, 9, 10, 10, 10, 11, 11, 10, 8, 5, 4].
, .
,
, ,
, . ,
,
.

, . ,
, .
[6, 9, 8, 5].
, , .
, .
. , [9, 8, 5, 6].
,
,
, .
.
, . ,
, ,
.
,
.
. , . ,
. ,
.
, ,
, .
, , .
.

12


, , 15-25 2013

Akai


A. Automaton
:
:
:
:

stdin
stdout
1
256

S . , S ( ).
N
2N . . 1.

abacaba


S , .


N K
. K , ai , bi
ci , ai bi ci .
. ,
.

1 |S| 105
1 K 2N
1 ai , bi N
a ci z

, , 15-25 2013

13

Akai

stdin
abacaba

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

10
2 a
3 b
5 c
3 b
5 c
4 a
5 c
6 a
7 b
8 a

A. Automaton
N S . N + 1 N ,
i i + 1 S[i].
N , ,
.
, , . , 2 N .
, 1 N ,
: , ,
, .
.
, ( ), , ,
,
( 1). , 1 (
), ,
. , ,
.
N 1. ,
,
, 1,
14


, , 15-25 2013

Akai

. ,
.
, , , ( , ,
). , , , , .
L R,
, , T .

, , .
,
.
, O(N log(N )) ,
.

B. Bims
:
:
:
:

stdin
stdout
1
256

k - k - ,
(i1 , i2 , ..., ik ) Cni1 ,i2 ,...,ik , ij 0, n = i1 + i2 + ... + ik .
:
1-
2-
3- ,
Cni,j,k .
, k - ,
p (p ). n-
(, n).
.
, 109 + 7.


, , 15-25 2013

15

Akai


k , p t. t
ni .


t , ni
ni - ,
109 + 7.

1 k 103
2 p 106 + 3, p .
1 t 105
0 ni 1018

stdin
2 7 4
0
6
7
8
3 7 4
0
6
7
8

stdout
1
7
2
4
1
28
3
9

B. Bims
p . ,
, , , p.
p- . , , , Cp0,0,0...p...,0,0,0 , p
, .
, Cni1 ,i2 ,...ik
Cni1 1,i2 ,...ik , Cni1 ,i2 1,...ik , ... Cni1 ,i2 ,...ik 1 ( ,
), ,
p- k
p.
16


, , 15-25 2013

Akai

,
p , , k -
p. :

answern = answern

mod p

answern/p

. (n mod p)-
(n mod p) k ,
k1
C(n
mod p)+k1 .

C. Cutting
:
:
:
:

stdin
stdout
1
256

S M , N .
S ,
M , S . S , , . ,
S . , .


S . N
. N ,
M . .


, S .

1 |S| 100
1 N 100
1 |Mi | 100


, , 15-25 2013

17

Akai

stdin

stdout

abacaba
4
aba
aca
a
b

aca, abba.
b, aba.
aba.

C. Cutting
.
r[i][j] , , S i j . d[i][j][k][l]
, , S i j
l k . :

d[i][j][k][l] = min {d[i][p 1][k][l 1] + r[p + 1][j]}


ipj

, m[k][l] = S[p], m[k][l] l- k -


.

r[i][j] = min{ min {r[i][p] + r[p + 1][j]}, min {d[i][j][k][len(k)]}}


ipj1

1kN

len(k) k - .
P
O(|S|3 len(i)) O(N 5 ). , 1,
, .
, i len(k), i + len(k) > |S|.
N 3 N 2 .

18


, , 15-25 2013

Akai

D. Disclosure
:
:
:
:

stdin
stdout
1
512

N K .
, .
G G0 ,
G (u, v) ,
u v G.


N K . K , ai bi , ,
ai bi . , .


, ,
. .

1 N 50 000
0 K 50 000
1 ai , bi N

stdin
5
1
2
3
4
1
1

6
2
3
5
5
5
3


, , 15-25 2013

stdout
1
2
3
4

2
3
5
5

19

Akai

D. Disclosure
(u, v) ,
u v . .
. u S[u]
,
S
u. S[u] = S[vi ], vi u. S[u], , .
.
S .
N/32 .
O(N K/32)

E. Embedded circles
:
:
:
:

stdin
stdout
1
256

{aij } R C , 0
9. Q :
ik jk rk , 1 k Q
aij ,
(i ik )2 + (j jk )2 rk2 .

(ik , jk ) rk .
, .
:
,
.
, . ,
k l (k < l) , ,
t: k < t l, t k .
.


R C . R
C ( ).
Q . Q
20


, , 15-25 2013

Akai

: ik , jk , rk ,
. 1.


,
.

1 R, C 2 000
0 aij 9
1 Q 106
1 + rk ik R rk
1 + rk jk C rk
min(R, C) 1
0 rk
2

stdin
6 6
123456
234567
345678
456789
567890
678901
10
1 1 0
3 3 2
3 2 1
2 2 0
4 2 0
1 3 0
2 3 0
4 3 0
5 5 1
5 5 0

stdout
141

141 = 1 + 65 + 20 + 3 + 5 + 3 + 4 + 6 + 25 + 9


, , 15-25 2013

21

Akai

E. Embedded triangles
O(ri ), ri .
.
O(1) .
, , O(S),
S .
f (R) , , , ,
, ,
R.
g(S) , ,
S .
f (R) = 2r + f (r 1) + g(R2 r2 )
g(S) = 2r + f (r 1) + g(S r2 )
r .
, f (R) 2R2 g(S) 2S .
f (R) 2r + 2(r 1)2 + 2(R2 r2 ) 2R2 + (4 4)r + 2 2R2
g(S) 2r + 2(r 1)2 + 2(S r2 ) 2S + (4 4)r + 2 2S

F. False figures
:
:
:
:

stdin
stdout
2
256

.
, .
{aij } 1 000 1 000 . Q :
ik jk rk , 1 k Q
aij ,
(i ik )2 + (j jk )2 rk2 .

(ik , jk ) rk .

22


, , 15-25 2013

Akai

k l (k < l) , k l
, t: k < t l,
k . .
, .


Q . Q
: ik , jk , rk ,
.
1.


,
. , . , Ok.

1 Q 106
1 + rk ik 1 000 rk
1 + rk jk 1 000 rk
0 rk < 500

stdin
6
10 10 5
10 10 4
5 10 0
10 5 0
10 15 0
15 10 0
2
6 6 5
11 11 5

stdout
Ok

2 1

F. False figures
, .
. ,
.

, , 15-25 2013

23

Akai

. , . -
, . , ,
.
, . , , ,
- .
, .
, , . ,
.
O(ri ) , O(S), S . - ,
log .

G. Grouping
:
:
:
:

stdin
stdout
1
256

N {ai }. {bj } K (
), :

S=

N
X
i=1

min |ai bj |

1jK

S .
S .


N K . N
{ai }.


S 108 .
24


, , 15-25 2013

Akai

1 N 5 000
1KN
0 ai 400 000

stdin

stdout

5 3
1 5 7 10 14

5.0

{bi } {1, 7, 14}.

G. Grouping
,
K bj . ,
.
bj .
[i, j]
b .
: d[i][j]
i j . :

d[i][j] = min d[p 1][j 1] + cost(p, i)


1pi

cost(p, i) , p i.
O(N 3 ). p[i][j] p,
d[i][j]. :
p[i 1][j] p[i][j] p[i][j + 1]
, p[i][j] , :
N X
K
X

(p[i][j + 1] p[i 1][j]) =

i=1 j=1

N X
K
X

p[i][j] +

i=1 j=2

K
X

p[N ][j]

j=2


, , 15-25 2013

N
1
X

N
1 X
K
X

p[i][j] =

i=1 j=1

p[i][1].

i=1

25

Akai

p[i][j] 1 i,
N K . O(N K).

H. Hidden triangles
:
:
:
:

stdin
stdout
4
512

N 1 N .
, .
, .
,
.


N . N , . xi1 , yi1 , xi2 , yi2 , xi3 , yi3 .
.
.


.
.

1 N 500
1 000 xij , yij 1 000, 1 i N, 1 j 3

stdin
3
1 0 4 0 0 3
-2 1 5 -2 3 4
-2 2 4 1 2 4

26

stdout
2
2 3


, , 15-25 2013

Akai

H. Hidden triangles
, ,
.
O(N 2 log(N )), O(N 2 ),

O(N log(N )).
O(N 2 log(N )).
,
.
, ,
,
. ,
, ,
. ,
,
, , .
set.
,
.
, .
:

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


, , 15-25 2013

27

Akai

I. Interactive
:
:
:
:

stdin
stdout
1
256

http://codeforces.ru/blog/entry/4037
Alex_KPR: ... - , FFT ,
...
Kunyavsky: .
FFT?
Alex_KPR: , ,

Kunyavsky: : .
,
homo_sapiens:
( )
Kunyavsky: . .
homo_sapiens: ...
, .
N {xi }
{yi }.
FFT, ,
:

P (z) =

N
1
X

xi z i

i=0

Q(z) =

N
1
X

yi z i

i=0

P (z) Q(z).
, , {xi }
, {yi } . N (xi , yi ).
. - .
28


, , 15-25 2013

Akai

, FFT, , , (xi ). (yi ). ,


,
, , .
.
, ,
prewritten code.


N . N
{xi }. N
{yi }.


FFT, 2N 1
. ,
N
.
, , ,
.

1 N 50 000
1 xi 50 000
1 yi 50 000
, 50

stdin
6
4
1
6
4
1

1 2 3 6 5
6 3 4 5 2

stdout
4 25 20 34 54 71 72
58 56 37 10
0 1 2 2 3 4

1 2 3 6 5
6 3 4 5 2


, , 15-25 2013

29

Akai

I. Interactive
. O(N log(N )).

J. Journey
:
:
:
:

stdin
stdout
1
256

, J-Pax
A, B C . , ,
.
. ,
, .


A, B , C .


, 108 .

1 A, B, C 1 000

stdin
1 1 1

stdout
2.2360679774998

J. Journey
, . .
.
.
( ).
30


, , 15-25 2013

Akai

A , B1 , B2 , B3 , B4 , . , B4 . , 1, 2, 3, 4, 6
A B4 . 5.
5 ( ).
A1 , A2 , A3 , A4
A 5.
5 .
,
d , 5
A1 , A2 , A3 , A4 d.

K. Knuth knows
:
:
:
:

stdin
stdout
1
512

N K .
.
G G0 ,
G (u, v) ,
u v G.
, , ?

, , 15-25 2013

31

Akai


N K . K , ai bi , ,
ai bi . , .

1 N 50 000
0 K 50 000
1 ai , bi N

stdin
5
1
2
3
4
1
1

6
2
3
5
5
5
3

stdout
7

K. Knuth knows
(u, v) , ,
u v . .
u S[u]
,
S
u. S[u] = S[vi ] vi , vi
u.
S .
N/32 .
O(N K/32).

32


, , 15-25 2013

Akai

L. Lake
:
:
:
:

stdin
stdout
1
256

.
, - (x, y). ,
. -,
d.
, ,
. (x, y) .
.


x, y , d.

10 000 x, y 10 000
1 d 10 000

stdin
6 3 2

stdout
4

L. Lake
(x, y) T = kd+l, 0 l < d. l = 0,
k . l > 0. , k
. , k + 1 .
k 1 (x, y).
d (x, y).
l, d,
2d + l. , ,


, , 15-25 2013

33

Akai

d. ,
. , k + 1 .
, d, .

M. Match them up
:
:
:
:

stdin
stdout
1
256

.
, .
N , N
K .
M , ,
M .
{(ui , vi )} , ui
, vi .
: u1 , v1 , u2 , v2 , ..., um , vm .
.


N K
. K .
ai bi , ai ,
bi .


m. m ui , vi , ui ,
vi , i- .

1 N 103
0 K 105
34


, , 15-25 2013

Akai

1 ai , bi N

stdin
3
1
1
3
2
2

5
2
3
2
3
1

stdout
3
1 3
2 1
3 2

M. Match them up
. .
, .
,
. ,
. ,
.
2, 3 ..
a. ,
. a , b ,
b , , (a, b). b (a, b),
. , ,
.
a , . .
. , a.
, a , . a, .
. , a.
, a . ,

, , 15-25 2013

35

Akai

, .
, , .
, .

N. Need for sum thing


:
:
:
:

stdin
stdout
1
256

N .
, ,
.. i- 2i 1 .
. ,
.
, .

a31

a21
a32

a11
a22 a23
a33 a34 a35
...

0 9.
Q
. :

ri , ci , ki
i- ki (ri , ci ):

ari ,
ari +1,
...
ari +ki 1,

ci

ari +ki 1,

ci
ci +1

ci

ari +1, ci +1
ari +1, ci +2
...
...
...
ari +ki 1, ci +2ki 3 ari +ki 1,

ci +2ki 2

(ri , ci ).
: (ri + 1, ci ), (ri + 1, ci + 1), (ri + 1, ci + 2), ..
.
, :
A1 = 1
36


, , 15-25 2013

Akai

Ai = (1 234 567 Ai1 + 7 654 321) mod 1 000 000 007, 2 i Q


ri = Ai mod N + 1
ci = Ai mod (2ri 1) + 1
ki = Ai mod (N ri + 1) + 1


N Q
. N . i 2i 1 (
).


,
.

1 N 103
0 Q 5 106

stdin
3 5
1
234
56789

stdout
42

1
234
: 56789
3
: 678, 24.
: 8, 8.
: 6, 6.
: 1, 1.
: 3, 3.

N. Need for sum thing


(i, j) : sl(i, j), sr(i, j)
s(i, j).

, , 15-25 2013

37

Akai

sl(i, j) , sr(i, j)
, s(i, j) .
, i, j, k , :
ans = sl(i, j) sl(i + k, j) + sr(i + 1, j + 2) sr(i + k, j + 2k) + s(i + k, j +
2k 1) s(i + k, j).

O. Open air
:
:
:
:

stdin
stdout
1
256

,
0 N (N ). {a, b} . ,
(N + 1)(N + 2)

.
2
38


, , 15-25 2013

Akai

N + 1 ,
, ,
. , .


N .


N + 1 N + 2 : a1 , b1 , a2 , b2 , . . . , ak , bk ,
N +2
k =
, ai , bi 2
. .
.
, .

2 N 100, N

stdin
2

stdout
1 2 0 1
1 1 0 2
0 0 2 2

O. Open air
N (N + 2)
.
2
.
. :
i j pi pj .
vi i, vj j .
pi - si , pj - sj .
. pi - new_si = si vi + vj ,
pj - new_sj = sj vj + vi .
|new_si S| + |new_sj S| < |si S| + |sj S|,
, i j .
S =


, , 15-25 2013

39

Akai

. ,
.
,
.
.

P. Pseudo automaton
:
:
:
:

stdin
stdout
1
256

S . , S ( ). .
.
1.

abacaba


S , .


N K
. K , ai , bi
ci , ai bi ci .
. ,
.
40


, , 15-25 2013

Akai

1 |S| 5 000
1 ai , bi N
a ci z

stdin
abacaba

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

10
2 a
3 b
5 c
3 b
5 c
4 a
5 c
6 a
7 b
8 a

P. Pseudo automaton
N S . ,
1 N . N + 1 26N : i i + 1 a z,
1 i N . ,
S .

Q. Quiz
:
:
:
:

stdin
stdout
1
256

.
.
: N N ,
1 1. ,
1 N 2 1.
. , ,

, , 15-25 2013

41

Akai


. .
.
:

1
2
... N 1 N
N +1
N +2
. . . 2N 1 2N
..
..
.
.
2
2
2
N N + 1 N N + 2 ... N 1
.
:

1
5
9
13

2
6
10
14

3 4
7 8
11 12
15

, ( , ), . ,
.


N . N N
. 1 N 2 1 , 0
. 0 N 2 1 .


, No.
Yes, ( ):
L , ,
.
R , , .
U , , .
D , , .
2 500 000.
, .
42


, , 15-25 2013

Akai

2 N 50
0 aij N 2 1

stdin
2
0
2
2
2
3

3
1

stdout
Yes
DRULDR
No

1
0

Q. Quiz
.
N 2 .
i 1 i- j 1 .
t = (i 1)N + j (i, j).
j < N , t j ,
i, . j = N
i , i
. t j 1 i,
t 1. i
, .
N 2 , . j , (N 1)N +j
(N 1, j), (N 2)N + j (N 1, j + 1).
,
. .
, ,
N 2 1 (N, N 1),
(N, N ). , ,
.
O(N ) ,
O(N 3 ) .


, , 15-25 2013

43

Akai

R. Reduction
:
:
:
:

stdin
stdout
1
256

S M , N ,
L.
S , M ,
S . S , , . ,
S . , .


S . N
. N ,
M . .


, S .

1 |S| 100 ; 1 N 100 ; 1 |Mi | 100 ; 1 L |S|

stdin
abacabada
4
aba
aca
ada
abb

stdout
3

R. Reduction
, ,
|S|
. ,
.
L
44


, , 15-25 2013

(17.02.2013 .)

...
, - ,
. 20052010 . Saratov SU #1, ACM ICPC.
.
:

I
2005 .;
1 ACM ICPC,
( ) 2008 .;
ACM ICPC 2009, ACM ICPC 2010;
Google Code Jam 2008 2011, VK Cup 2012, Challenge 24
2012;
2 Russian Code Cup 2012;
12 ..
, ,
III-.

.

G = (V, E),
(u, v) E c(u, v) 0, . (u, v)
/ E
c(u, v) = 0. : s t.
G f : V V R, :

, , 15-25 2013

45

1. ,

f (u, v) c(u, v), u, v V .

2. : f (u, v) = f (v, u) u, v V .
P
3. :
f (u, v) = 0 u V , s t.
vV

G , (
) ( ), .

X
|f | =
f (s, v).
vV

G
.

u v G, f , : cf (u, v) = c(u, v) f (u, v).
Gf = (V, Ef ), Ef = {(u, v) V V : cf (u, v) > 0}, G, f .
Gf cf .
1. f G f 0
Gf . f + f 0 G |f + f 0 | = |f | + |f 0 |.
13
f + f 0.
w s t Gf

cf (w) = min{cf (u, v) : (u, v) w}.


p

cf (p), (u, v) w,
fw (u, v) = cf (p), (v, u) w,

0, .
|fw | = cf (w) > 0, G:
f := f + fw .
46


, , 15-25 2013

2. f ,
.
:
w,
f , , .
F f : |f | < |F |. , f 0 = F f
Gf |f 0 | > 0. , .
-
begin
f := 0
while w Gf
do
f := f + fw
end
end
n = |V |, m = |E|. O(|f |m). -
-,
, O(nm2 ).
. 100200
. O(n3 ).

G s t,
(u, v) E c(u, v) p(u, v). f
X
p(f ) =
p(u, v)f (u, v).
u,vV
f (u,v)>0

f
, .
(u, v) G (v, u) c(v, u) = 0 p(v, u) = p(u, v). ,
(
), . pf (u, v) = p(u, v).

, , 15-25 2013

47

. , (u, v) (v, u),


u v
.
, .
3. f G, f 0
Gf . p(f + f 0 ) = p(f ) + p(f 0 ).
4. f (.. ) ,
Gf ().
, w, fw .
|fw | = 0 p(fw ) = c(fw ) ( ) < 0. 1 3, |f + fw | = |f | p(f + fw ) < p(f ), ..
f .

F f ,
. ,
() Gf .
begin
(
-)
while w
Gf do
f := f + fw
end
end
-
O(nm). O(nm2 CP ),
C = max c(u, v), P = max p(u, v). ,
O(nm2 log n).

-. f G w s t
. , 0 cf (w) f + fw .
48


, , 15-25 2013

begin
f := 0
while Gf do
w := s t
f := f + fw
end
end

, d(n, m). O(d(n, m)|f |). - d(n, m) = O(nm), O(n2 ),
O(m log n) O(n log n + m), .
, ,
, .
p(u, v) 0 , . ( )
.
v V (v),
. (u, v) p(u, v) + (u) (v). ,
u v
(u) (v), .
,
.
,
. (v) s v +,
v . , (v) > , .

(u) + p(u, v) (v),


.
-
, .
while d(v) : (v) := (v) + d(v).

, , 15-25 2013

49


1. , , , .
: (2005).
2. Ravindra Ahuja, Thomas Magnanti, James Orlin. Network flows (1993).


A.
:
:
:
:

stdin
stdout
2
64

n , 1 n.
1 , n . i j i < j ,
j i. .


n (2 n 100)
.

stdin

stdout

A.
, ,
. i, 1 i
i n min(i 1, n i).
1 n.

n1+

n1
X

min(i 1, n i).

i=2

50


, , 15-25 2013

.
, , . , i n/2 , i > n/2 .
i n/2 i > n/2.
1 n .

B. 1
:
:
:
:

stdin
stdout
2
64

n m . 1 , n .
. 1, ..
1, .
, , . , ,
1.


n m (2 n 1000, 0 m 10000).
m x, y , p x y
p (1 x, y n, 1000 p 1000).
.



1. 1, N O.

stdin
6
1
1
2
1
3
3
4

7
2
1
3
3
4
1
6

stdout
6

-3
8
1
1
-2
2
10


, , 15-25 2013

51

B. 1
, , .
- (
O(nm)).

C.
:
:
:
:

stdin
stdout
2
64


.
, n (2 n 100) . s, t
c , , , . c , :
, 1 000.
, .
,
0, .

52


, , 15-25 2013

stdin
4
1
1
1
2
2
3
0

stdout
25

4
2
3
3
4
4

5
20
10
5
10
20

C.
-.
:
1. .
.
2. . ,
,
.

D.
:
:
:
:

stdin
stdout
2
64

, ACM
2004 . ,
. , ,
.
- n . , ,
, , 15-25 2013

53

, 1.
, m .
, (
), , , .
, ( )

.
, , . , ,
.

, ,
.
,
.


n m
, . (2 n 100, 1 m 2000).
n 1 2, 3 . . . , n ( 1000).
m .
, ,

( , 1000).


,
.

54


, , 15-25 2013

stdin
4 4
80 50 130
1 2 80 50
2 4 40 90
3 1 40 60
3 4 30 50

stdout
3000

80 ( 50 , 30 , 2400
), 30 ( 3
4, 110 , 20 , 600 ).
,
.

D.
. 1, .
( ) , , . . .
-,
. , ,
, . , . ,
-, .

E.
:
:
:
:

stdin
stdout
2
64

k . , n ,

, , 15-25 2013

55

. , 1, n .
, , 1 n. ,
n, , . ,
( ).
, ,
.


n, m k ,
(2 n 200, 1 m 2000, 1 k 100). m
,
,
( 106 ).
, .


,
1. ( 5
), , , .
k , , .
, , ,
.

stdin
5
1
1
1
2
2
3
3
5
56

8
2
3
4
5
3
5
4
4

2
1
1
3
5
1
1
1
1

stdout
3.00000
3 1 5 6
3 2 7 8


, , 15-25 2013

E.
k 1
n, .
, . .
-, .
k ,
k . -, O(nmk).

F.
:
:
:
:

stdin
stdout
2
64

n. n
, , :
1. 1
;
2. , , .
.


n (1 n 200).
n n .
1000.


.
n . i-
, i-
. ,
1 n.


, , 15-25 2013

57

stdin
5
7 10 5 3 7
10 6 6 10 9
7 7 7 5 9
5 1 4 7 7
5 10 6 5 6

stdout
23
4 3 1 2 5

F.
. ,
, . 1
,
. .
1
0 .
n .
.
O(n3 ). .

G.
:
:
:
:

stdin
stdout
2
64

,
.

, .
. , ,
, .
58


, , 15-25 2013

. , , ,
,
.
.


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

.
. , (Xi , Yi ) (Pj , Qj )
Dij = |Xi Pj | + |Yi Qj | + 1 .


,
. N (1 N 100) M (1 M 100),
. N ( 1 N ), M (
1 M ).
N .
Xi , Yi Bi , , Xi , Yi (1000 Xi , Yi 1000) , Bi
(1 Bi 1000) .
M . Pj , Qj Cj , ,
Pj , Qj (1000 Pj , Qj 1000) , Cj
(1 Cj 1000) .

, , 15-25 2013

59

N .
. i- M Eij ,
. Eij (0 Eij 10000) ,
i- j - .
, , , .


,
OPTIMAL.
SUBOPTIMAL, N
( ) , .
,
.

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

60

stdout
SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1

OPTIMAL


, , 15-25 2013

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

H.
:
:
:
:

stdin
stdout
2
64


. ,
,
. ,
,
. ,
.
.
1 N . 1 N .
.


N M (2 N 400),
M
, .
M : X , Y L
(1 X, Y N ; 1 L 10000), X Y ,
.


.
. , No solution
( ).

, , 15-25 2013

61

stdin
6
1
3
3
1
4
4
5
4

8
2
2
4
3
2
5
6
6

stdout
1 3 4 5 6
1 2 4 6

1
1
1
2
2
1
1
2

H.
E k = 2.
,
, .

I.
:
:
:
:

stdin
stdout
2c
64 M

. , . , - ,
. .


n (1 n 1000).

(231 xi , yi 231 ). ( , , ).
.


, .
62


, , 15-25 2013

stdin
8
0 0 1 0
2 1 3 0
3 0 4 0

stdout
2

1 0 2 1
2 -1 3 0
0 0 3 0

1 0 2 -1
1 0 4 0
0 0 4 0

I.
, . v v1 v2 .
(v1 , v2 ). (u, v) : (u2 , v1 ) (v2 , u1 ).
1. ,
, , , .
.

J.
:
:
:
:

stdin
stdout
2
64

, .
1 x
y , 1 x
y.
1 n .

n,
m
(2 n 150; 0 m 2000), n ,
m . m
(x, y ) c
x, y , c (1 x, y n; 1 c 1000).
t (1 t 500), t


, , 15-25 2013

63

. t .
"1 x y " "2 x y " .
.

.


t + 1 . .
t .

stdin
4
1
2
3
1
2
2
1
2

5
2
3
4
3
4

15
5
15
5
5

stdout
15
16
15

2 3
1 3

J.
-. :
. , (u, v)
,
. u 1, v
1. u , v
1
. , 1. ,
u v .

64


, , 15-25 2013

K.
:
:
:
:

stdin
stdout
2
64

G. ci ,
li . ,
li fi ci .
0.
, ,
(
).
.


(1 n 200; 0 m 400),
n , m .
m f romi , toi , li , ci , f romi
, toi ,
li , ci
(1 f romi , toi n; 0 li ci 105 ). .
.
a b, b a
(.. ).


, NO.
YES. m .
m- , i-
.


, , 15-25 2013

65

stdin
4
1
2
3
4
1
4
4
1
2
3
4
1
4

6
2
3
4
1
3
2
6
2
3
4
1
3
2

stdout
NO

1
1
1
1
1
1

2
2
2
2
2
2

1
1
1
1
1
1

3
3
3
3
3
3

YES
1
2
3
2
1
1

K.
. li ri ci .
: li .
. ( -). ? ,
( , ),
li . ,
, li fi ci .
, .

L.
:
:
:
:

stdin
stdout
2
64

n , m . -
66


, , 15-25 2013

.
, .
, , 1,
, , n.
Max Traffic
, , . Max
Traffic , ,
,
.
, .
.
, , , . k c,
c/k .


n (2 n 100)
m (1 m 400). m
: , , .
107 .
, . . , ,
..
.


, .
.
1 , .


, , 15-25 2013

67

stdin
6
1
1
2
2
3
3
5
4
4
1
1
2
2
3

8
2
3
4
5
4
5
6
6
5
2
3
3
4
4

stdout
4
3 4 5 6

3
3
2
2
2
2
3
3

3
1 2 3

2
2
1
2
2

L.
G = (V, E) C E ,
, t s E\C . .
. . [1]. ( ) .
, .
, w.
. k c1 , c2 ,
. . . , ck .
k
1X
ci w.
k i=1
:
k
X

(ci w) 0.

i=1

,
c(u, v) w. ( )
68


, , 15-25 2013

. w . ,

c(u, v) w w.
. ,
( ). ,
.
-.

M.
:
:
:
:

stdin
stdout
2
64

, .
.
.
, n m
.
xi , yi xi yi . i- wi .
Snow
White. .
A,
B - .
, A B .
A
B , , , .

1 . ,
, .
, .
Snow White . , A ,

. , .

, , 15-25 2013

69

, Snow White ,
.
A B , : ,
, .


n, m, A, B (2 n 100;
0 m 5000; 1 A, B n; A 6= B ), n
, m . m
xi , yi , wi , ti (1 xi , yi n; xi 6= yi ; 0 wi 100; 0 ti 1),
xi , yi , wi , ti (0 , , 1 ).
.


p . p
. , A, B , .
, , , 0.

stdin
4
1
2
2
1
3
2
1
3
1
3
1

70

7
2
1
4
3
4
3
4
3
3
2
2

1 4
3 1
100 0
1 0
1 0
4 0
2 1
2 0
1 2
2 0
3 0
1 0

stdout
6
1
1
1
1
1
1

3
4
4
2
2
2

4
3 4
3 4

3
1 3 2
1 3 2
1 2


, , 15-25 2013

M.
, wi . , V . :
S T ,
B A .
(u, v) V : (u, v) (S, v) (u, T ) c(u, v).
. (S, v) (u, T ), ,
, . ,
V . .
, B
A . , . ,
F1 , F2 .
F1 F F2 . F2
,
B A.

V ( ).
, V ,
, .
, ( ,
)
.
. , u v ,
u v . , B
A , . , . ,
B A .
: A B
B A.


, , 15-25 2013

71

(18.02.2013 .)

...
, .
:

ICL-2011
ACM ICPC
2011 (5 ).
Google Code Jam 2011 onsite
(20 ).
Russian Code Cup 2011 onsite
(12 ).
Challenge 24 onsite
_NiN_ (8 ).
ACM ICPC 2012 (18 ).
Codeforces: 2391.
TopCoder: 2407.

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

72


, , 15-25 2013

STL-:

;
;
( );
.
:

;
.
- .

,
++ - ;
-- - ;
* - , .
- .
STL .
() ( ).
Random Access Iterator
Random Access Iterator - ,
N . += -=.
Random Access .
1. .
1 #i n c l u d e <algorithm>

STL- - ,
.
, -
(, )
:
1 bool compare_func(T a, T b)


, , 15-25 2013

73

T - , .
true, (
), . false - . ,
false.
() .
count
1 count(it1, it2, value);

value ,
it1, it2. - int.
:
1
2
3
4

a[5]
cout
cout
cout

= {1, 2, 8,
<< count(a,
<< count(a,
<< count(a,

2, 2};
a + 5, 2); // 3
a + 5, 8); // 1
a + 4, 2); // 2

find
1 find(it1, it2, value);

, , value , it1, it2. .


:
1
2
3
4

a[5] = {6, 2, 8, 1, 3};


find(a, a + 5, 2); // \&a[1]
find(a, a + 5, 4); // \&a[5]
find(a, a + 5, 1) - a; // = 3

min_element/max_element
1 min_element(it1, it2);
2 max_element(it1, it2);

, /
, , it1, it2. - .
:
1
2
3
4
5
6
7

a[5] = {6, 2, 8, 1, 3};


min_element(a, a + 5); // &a[3]
max_element(a, a + 5); // &a[2]
min_element(a, a + 5) - a; // = 3
max_element(a, a + 5) - a; // = 2
*min_element(a, a + 5); // = 1
*max_element(a, a + 5); // = 8

binary_search
74


, , 15-25 2013


1 binary_search(it1, it2, value);

, value , it1, it2. . - true, , false - .


:
1 a[5] = {1, 2, 8, 17, 239};
2 cout << binary_search(a, a + 5, 17); // true
3 cout << binary_search(a, a + 5, 19); // false

lower_bound/upper_bound
1 lower_bound(it1, it2, value);
2 upper_bound(it1, it2, value);

, /
[it1, it2), value, .
- .
:
1
2
3
4
5

a[5] = {1, 2, 2,
lower_bound(a, a
upper_bound(a, a
lower_bound(a, a
upper_bound(a, a

6, 8};
+ 5, 1);
+ 5, 1);
+ 5, 2);
+ 5, 2);

//
//
//
//

&a[0]
&a[1]
&a[1]
&a[3]

sort
1 sort(it1, it2);

it1, it2.
:
1 a[5] = {6, 2, 8, 1, 3};
2 sort(a, a + 5); // a[5] = {1, 2, 3, 6, 8}

stable_sort
sort, .
replace
1 replace(it1, it2, value1, value2);

value1 value2 , it1, it2.


:
1 a[5] = {1, 2, 2, 6, 8};
2 replace(a, a + 5, 2, 4); // a[5] = {1, 4, 4, 6, 8}


, , 15-25 2013

75

reverse
1 reverse(it1, it2);

it1 it2 .
:
1 a[5] = {6, 2, 8, 1, 3};
2 sort(a, a + 5); // a[5] = {1, 2, 3, 6, 8}

stable_sort
sort, .
replace
1 replace(it1, it2, value1, value2);

value1 value2 , it1, it2.


:
1 a[5] = {1, 2, 2, 6, 8};
2 replace(a, a + 5, 2, 4); // a[5] = {1, 4, 4, 6, 8}

reverse
1 reverse(it1, it2);

it1 it2 .
:
1 a[5] = {6, 2, 8, 1, 3};
2 reverse(a, a + 5); // a[5] = {3, 1, 8, 2, 6}

merge
1 merge(it11, it12, it21, it22, out_it)

, it11,
it12 ( ) it21, it22 ( ), , out_it. - , .
:
1
2
3
4

a[5] = {1, 2, 2, 6, 8};


b[3] = {2, 3, 9};
*c;
merge(a, a + 5, b, b + 3, c); // &c[8], c[8] = {1, 2, 2, 2, 3, 6,
8, 9}

76


, , 15-25 2013

unique
1 unique(it1, it2);

, it1 it2, . , .
:
1 a[5] = {1, 2, 2, 6, 6};
2 unique(a, a + 5); // &a[3], a[3] = {1, 2, 6}

random_shuffle
1 random_shuffle(it1, it2)

,
it1 it2.
:
1 a[5] = {6, 5, 1, 4, 3};
2 random_shuffle(a, a + 5); // a[5] = {1, 5, 6, 3, 4};

next_permutation
1 next_permutation(it1, it2)

, it1 it2 , . true,


, false - (
).
:
1
2
3
4

a[5] = {6, 1, 1, 5,
next_permutation(a,
a[5] = {6, 5, 3, 1,
next_permutation(a,

3};
a + 5); // true, a[5] = {6, 1, 3, 1, 5}
1};
a + 5); // false, a[5] = {6, 5, 3, 1, 1}

prev_permutation
next_permutation, .
2. pair.
( ).
:
1 pair<type1, type2> var;


, , 15-25 2013

77

2 : var.first - var.second -
.
:

( );
;
: ,
- ;
make_pair, .
:
1
2
3
4
5
6

i n t x, y;
double w;
...
pair<i n t , i n t > coordinates = pair<i n t , i n t > (x, y);
pair<i n t , i n t > coordinates = make_pair(x, y);
pair<double , pair<i n t , i n t > > point = make_pair(w, make_pair(x, y));

3. .


- , .. ,
.
:

size() - ( );
empty() - , ;
clear() - ;
( );
;
.
vector
,
( ). , ,
78


, , 15-25 2013

. , , .
1 #i n c l u d e <vector>

push_back(value) - value ;
pop_back() - ;
capacity() - ;
resize(size) - (size);
reserve(cap) -
( ). capacity().
:

begin() - , ;
end() - , .
:
1
2
3
4
5

vector<i n t > v;
v.push_back(100); // - 100
v.push_back(500); // - 100 500
*v.begin(); // 100
v.size(); // 2

:
1
2
3
4
5
6
7

vector<i n t > v;
...
f o r (i n t i = 0; i < v.size(); i++) {
i n t x = v[i];
...
}

:
1 f o r (vector<i n t >::iterator it = v.begin(); it != v.end(); it++) {
2
i n t x = *it;
3
...
4
5 }

.

, , 15-25 2013

79


1
2
3
4

vector<long long > v;


...
sort(v.begin(), v.end());
i n t c = count(v.begin(), v.end(), 100ll);


1. N M .
.
1
2
3
4
5
6
7
8
9
10

vector<i n t > v[MAXN];


...
cin >> n;
f o r (i n t i = 0; i < M; i++) {
i n t x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}

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

vector<i n t > ans;


...
ans.push_back(value);
...
reverse(all(ans));
f o r (i n t i = 0; i < ans.size(); i++)
cout << ans[i] << endl;

3. . K
.
1 i n t f(i n t x) {
2
...
3
vector<i n t > tmp;
4
...
5
tmp.push_back(h[y]);
6
...
7
sort(tmp.begin(), tmp.end());
8
i n t sum = 0;
9
f o r (i n t i = 0; i < min(k, tmp.size()); i++)
10
sum += tmp[i];
11
r e t u r n sum;
12 }

4. .
.
80


, , 15-25 2013


1
2
3
4
5
6
7
8

vector<i n t > v;
...
v.push_back(x);
...
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
...

queue
, , .
1 #i n c l u d e <queue>

front() - ;
pop() - .
push(value) - value .

1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

queue<pair<i n t , i n t > > q;


void process(pair<i n t , i n t > p) {
i n t x = p.first;
i n t y = p.second;
...
}
...
i n t main() {
...
i n t x, y;
q.push(mp(x, y));
while (!q.empty()) {
process(q.front());
q.pop();
}
...
}


, , 15-25 2013

81

priority_queue
, , () .
1 #i n c l u d e <queue>

top() - () ;
pop() - ;
push(value) - value .

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

82

vector<pair<i n t , i n t > > v[MAXN];


i n t ans[MAXN];
priority_queue<pair<i n t , i n t > > q;
void process(pair<i n t , i n t > p) {
i n t d = -p.first;
i n t x = p.second;
i f (ans[x] != d)
return;
f o r (i n t i = 0; i < v[i].size(); i++) {
i n t y = v[x][i].first;
i n t nd = d + v[x][i].second;
i f (ans[y] > nd) {
q.push(make_pair(-nd, y));
ans[y] = nd;
}
}
}
i n t main() {
...
i n t x;
q.push(mp(0, x));
while (!q.empty()) {
process(q.top());
q.pop();
}


, , 15-25 2013

set
, . ,
, O(log(N )), N - .
1 #i n c l u d e <set>

insert(value) - value ;
erase(value) - value , , ;
erase(it) - ,
it;
find(value) - value ,
, end();
lower_bound(value) - lower_bound begin(),
end(), O(log(N ));
upper_bound(value) - upper_bound begin(),
end(), O(log(N )).
:

begin() - , ;
end() - , .

1. set online- . N , , ;
2. set .
, ;
3. set . - . N .
;
4. set . .

, , 15-25 2013

83

1. .
2. .
3. .
4. set.
5. lower_bound (lower_bound(s.begin(), s.end(), x)).
multiset
set, .
: erase(value) value .
erase(it) .

1. . N
M , .

1. C++,
http://www.cplusplus.com/reference/

STL

2. topcoder.com
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary2


A. DOMA 2: Last Hit
:
:
:
:

stdin
stdout
1
256

- Defence of Mysterious Accepted


2. , ,
84


, , 15-25 2013

, . , .
(
). , ,
, .
, , .
, , .
!
, , ,
, T1 ,
T2 . , T , ,
T + T1 . . , ,
. ,
, ( 0).
0
, T2 .
, ,
(. 1).
H .
H1 , H2 .
, .
! ,
.


2 T1 T2
(1 T1 , T2 100000) . 3
H1 , H2 H (1 H1 , H2 , H 100000) ,
.


,
, .
( T1

, , 15-25 2013

85

). ,
.
-1.

stdin
1
1
1
1
1
1

1
1 1
1
1 2
2
4 10

stdout
-1
1
0
2
2 3

A. DOMA 2: Last Hit


, , . H 100000, 99999
. - K2
, . (K2 1) T2 , D2 K2 .
H 0 = H D2 K2 . , , H 0 0.
T2 1 , .. T = K2 T2 1
( ).
, , K1 = H 0 /D1 .
T 0 = (K1 1) T1 . T 0 T ,
. , ,
T , T T1 , T 2 T1 .. K2 ,
-1.

B. Last Effect 3: Danger


:
:
:
:

stdin
stdout
1
256

Last Effect 3. ,
, .
. , - ,
86


, , 15-25 2013

3 . , .
.
.
, ,
. 3 ,
,
.
.


3 .
3 Xi , Yi
(100000 Xi , Yi 100000) Ri (1 Ri 100000).
. .


106 . ,
. , .

stdin
0 0 1
0 4 1
8 0 1

stdout
4.0000000 2.0000000

B. Last Effect 3: Danger


D2 R2 , D -
, R - .
(X1 , Y1 , R1 ), (X2 , Y2 , R2 ).
(X, Y ), . (X X1 )2 +(Y Y1 )2 +R12 = (X X2 )2 +(Y Y2 )2 +R22 . ,
A X + B Y + C = 0, .. ( ).

, , 15-25 2013

87

(X2 , Y2 , R2 ), (X3 , Y3 , R3 )
. ,
( ).

C. Fineage: Training
:
:
:
:

stdin
stdout
1
256

Fineage.
80- (, , 99-
). , 80-
. , . ,
X1 , X2 .
. N , . ,
,
N 1 .
,
, ,
. Fineage M , ,
0 M 1. ,
? ,
T > 0, i
Ei = Ei+t (mod M ), Ei ,
i .


2 X1 X2 ,

(0 X1 , X2 109 ). N
(1 N 109 ).
M Fineage (1 M 109 ).


88


, , 15-25 2013

stdin
1 2
2
3

stdout
2

C. Fineage: Training
X1 X2 M.
X1 = X2 , , .
T, , T
N . T = N K . N
X = X1 (N 1) + X2 . T K X .
, K X M. K = M/gcd(M, X), K N -
.

D. DOMA 2: Inventory
:
:
:
:

stdin
stdout
1
256

- Defence of Mysterious Accepted


2. , , , . .
, , . .
, BattleFury Claymore
Broadsword Perseverance. Perseverance, , RingOfHealth VoidStone. (, ), ,
, .
, . , ,
,
, .

, , 15-25 2013

89


N (1 N 100)
. N ,
. ,
. ,
:
<item> = <part1> + <part2> + ... + <partK>
<item> , <part1>, ..., <partK> - , <item>.
2 10. = +
.
, 50 , . .
. ,
( ). ,
,
( ).
M (1 M 100) . M
( ). .
.


,
(, -
), 1. ,
2. , 0.
, -1.

90


, , 15-25 2013

stdin
6
BattleFury =
Claymore
Broadsword
Perseverance
RingOfHealth
VoidStone
1
BattleFury
1
Perseverance
6
BattleFury =
Claymore
Broadsword
Perseverance
RingOfHealth
VoidStone
1
RingOfHealth
1
Perseverance
6
BattleFury =
Claymore
Broadsword
Perseverance
RingOfHealth
VoidStone
2
Claymore
Broadsword
2
RingOfHealth
VoidStone
6
BattleFury =
Claymore
Broadsword
Perseverance
RingOfHealth
VoidStone
1
BattleFury
4
RingOfHealth
VoidStone
Claymore
Broadsword

stdout
1

Claymore + Broadsword + Perseverance


= RingOfHealth + VoidStone

2
Claymore + Broadsword + Perseverance
= RingOfHealth + VoidStone

-1
Claymore + Broadsword + Perseverance
= RingOfHealth + VoidStone

0
Claymore + Broadsword + Perseverance
= RingOfHealth + VoidStone


, , 15-25 2013

91

D. DOMA 2: Inventory
,
. ,
. ,
- .
. ,
. . .
10100 ,
.

E. South Mark: 3.50


:
:
:
:

stdin
stdout
1
256
CHEFS DAD: Ooh, it mustve been about
seven, eight years ago. Me and the little lady
was out on this boat, you see, all alone at night,
when all of a sudden this huge creature, this
giant crustacean from the paleolithic era, comes
out of the water.
CHEFS MOM: We was so scared, Lord have
mercy, I jumped up in the boat and I said
"Thomas, what on earth is that creature?!"
CHEFS DAD: It stood above us looking down
with these big red eyes,CHEFS MOM: Oh, it was so scary!
CHEFS DAD: -and I yelled. I said, "What do
you want from us, monster?!"And the monster
bent down and said, "...Uh I need about
tree-fifty."
KYLE: Whats tree-fifty?
CHEFS DAD: Three dollars and fifty cents.
CHEFS MOM: Tree-fifty.

92


, , 15-25 2013

... South Mark.


, - 3 50 . ,
, 3.50, ?
- ? ,
3.50 , N ? ,
, N ,
, N ,
.


N ,
- (1 N 109 ).
M (1 M 30).
M Ni
(1 Ni 109 ) Ki
(1 Ki 109 ). , i > 1
Ni Ni1 Ni > Ni1 .


N , .

stdin
350
3
10 10
50 8
100 4

stdout
2

E. South Mark: 3.50


,
N . ,
M ,
, M .
. - K . ,
.

, , 15-25 2013

93

X ,
Y .
T = min(N 0 /X, Y /K) , N 0 - ,
. Y /K ,
( Y K ),
Y %K , T = (Y /K) + 1,
D = (KY %K)X - ,
(, D ). N 0
T X .
, D > 0 N 0 > 0,
K , K .

F. HOLM 2: The Great Battle


:
:
:
:

stdin
stdout
1
256

Heroes of Light and Magic 2. .


() .
: (Hi )
, (Li Ri ).
Li Ri , ,
.
, .
. ( Li Ri ). , ,
.
. . ,
,
. ,
, , .
. -
, ,
, 94


, , 15-25 2013

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


N (2 N 10) , .
N .
namei 20,
. 3 : Hi (1 Hi 300),
Li , Ri (1 Li Ri 50).
.
M (1 M 1000) . M
:
namei deal D damage to namej
namei , namej ,
D (1 D 50000). ,
. ,
,
, (. 2).
( ).


. .
:
namei K

, , 15-25 2013

95

namei , K .
.
5 .
1 1000 .
,
,
. , . , .

stdin
3
Vampires 30 5 7
Minotaurs 35 5 10
Liches 25 8 10
5
Vampires deal 7 damage to Minotaurs
Minotaurs deal 9 damage to Vampires
Vampires deal 7 damage to Minotaurs
Liches deal 16 damage to Minotaurs
Vampires deal 6 damage to Minotaurs
2
Titans 300 20 30
Sprites 2 1 2
2
Sprites deal 1 damage to Titans
Titans deal 9000 damage to Sprites

stdout
2
Liches 2
Vampires 1
1
Minotaurs 1

1
Titans 300
1
Sprites 1

, , Heroes of Might
and Magic II.

F. HOLM 2: The Great Battle


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


, , 15-25 2013

, , -
.
.
3 :
1. .
2. (, , ,
).S
3. .
. ,
L R (, L = 1, R = 1000). , , .
DR, .
, , [L0 , R0 ]
(
/ , ).
, X = DR/Hi .
L1 = L0 + X , R1 = R0 + X
[L1, R1] [L, R].
2 3, , [L, R].
[L, R] .

G. Failout Few Vegas: Slow Save


:
:
:
:

stdin
stdout
1
256

Failout Few Vegas. , ,


. ,
. ,
S . , ...
. , -,
( , ).
3 N . ,
N .
, ,

, , 15-25 2013

97

, (
). ,
S . "" M S : S ,
M . "" M
S . ,
, .


S ,
. S 2000 .
.
, .
2000.
.



S , .

stdin
a
aa
aa
aa
aa
abc
cab
bca

stdout
2

G. Failout Few Vegas: Slow Save


- S ,
Pi . . d[x][y][pref ] ,
, x ,
x y (x 1)
98


, , 15-25 2013

, S pref . , i .
prefi .
, S x
( Ki ). , prefi x ( prefi ).
prefi 0, , , Ki = 0. , x S prefi , P [prefi ], P [P [prefi ]] ,
0. P Pi , P [i], P [P [i]], ... S O(N 2 ), N S . Ki prefi
i, d[x][y][pref ], :
d[x][y][pref ] = max(d[x+1][i][prefi ]+Ki ). , d[n][i][pref ] = 0,
d[0][1][0].

H. Carmarandom TDC2013: New Trace


:
:
:
:

stdin
stdout
1
256

Carmarandom Total Destruction Contest 2013. , , ,


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


N
(10 N 100).

, , 15-25 2013

99


, Yes ( ). N + 1
, .
, N 1 ( ),
. . .
0.5.
0 180 , .
10000 .
, No (
).

stdin
11

stdout
No

H. Carmarandom TDC2013: New Trace


N - ,
1 .
, , N . N = 6:
-2 -1
-1 2
3 3
0 0
-3 3
1 2
2 -1
,
N .

100


, , 15-25 2013

I. X-Dom: Railway
:
:
:
:

stdin
stdout
3
256

X-Dom: The Distance Unknown. .


.
. ,
N
. . :
, , .
, , , ,
, . , .
,
.


N (2 N 75000)
. N Xi Yi , 100000,
.
OX.
Y = 0 . ,
OX. ,
OX.


106
.


, , 15-25 2013

101

stdin
3
0 1
-2 5
2 5

stdout
14.000000

1: (1, 3), (0, 5),


(1, 3), (7, 0), (0, 0) (7, 0) .
2: 1,
cin . cin
, Time Limit
Exceeded.

I. X-Dom: Railway
, , 2 . , , - . P
OX OP
: P . P OX .
P OX
OP . OP
P , P
OX .
OP ( P = (+, 0). P 0 OX , OP 0
OP .
OX . O(N ), O(log(M axX))
. ,
.

102


, , 15-25 2013

J. MindCraft: Heliport
:
:
:
:

stdin
stdout
3
256

MindCraft. N M ,
.
, ,
.
(
), .. 1. ,
H W .
, H W ,
.
. ,
.


N M (1 N, M 500)
, .
H W (1 H 20; 1 W 200; H N, W M )
. N M
.
100000.


,
.

stdin
2
1
1
4
2
1
1
4

3
2
2
5
3
2
6
2

stdout
1

3
6
2
3
5


, , 15-25 2013

103


( ) .

, 1 2 2.
1,
cin .
cin , Time Limit Exceeded.

J. MindCraft: Heliport
, . ,
. X - . , ,
Y , X .
, . : K1 -
X , Y S1 - K2 -
X , Y S2 - .
T , ,
T = S1 K1 Y + K2 Y S2 ().
, K1 , S1 , K2 , S2 . , , M S1
M S2 , , 1
(Size(M S2 ) <= Size(M S1 ) <= Size(M S2 ) + 1) M S1 M S2 . X
M S1 . K1 M S1 , S1 - M S1 .
M S2 .
S1 M S2 S1 S2 .
- .
(). H
X H (, M S1 , M S2 ).
X O(log(H W )). , O(H log(H W )).
O(W log(H W )). 104


, , 15-25 2013

.. ,
O(N M ) / O(N ) .
O(N M H log(H W ) + N W log(H W )) = O(N M H log(H W )).

K. Double Life: Amplifiers


:
:
:
:

stdin
stdout
3
256

Double Life. . -. , , , N
. Ai , ,
-. Ai ,
Ai . Ai 0,
, 0
. Ai . |Ai | , ,
. , ()
( ,
). ,
. , ,
.
. K ,
.
.
Ai 1 ( Ai , ).
, .
, . ,

.


N
(1 N 50000).
, , 15-25 2013

105

Ai (100000 Ai 100000).
K (0 K 109 ).


, . ,
- (. 2). ( ) 1.

stdin
3
1 0 1
2
3
10 10 -5
3
3
0 0 0
2

stdout
2

-200

K. Double Life: Amplifiers


, N
K , .
, .
, . ,
0 , 1 ,
( ,
-0). : K > 0 1 (, K
1). 4 .
1. .
(http://en.wikipedia.org/wiki/Karamata%27s_inequality). , . - ,
. , - . , A = a1 , ..., an B = b1 , ..., bn ,
log(a1 ) + ... + log(an ) <= log(b1 ) + ... + log(bn ). ,
106


, , 15-25 2013

K , , , K .
2. . X 0.
K < X , 0. K >= X ,
X
1 (K X ). ,
1.
3. , M , M <= K . ,
, ,
. , .
2.
4. , M , M > K .
, .
, ,
K ( K ). , 1,
( ).
. . , . . K ,
,
, . , O(N )
. . f , [L, R) . : C = (L + R)/2,
X1 = f (L, C), X2 = f (C, R). X = (X1 X2 )
, X .
f (0, N ). ,
O(N log 2 (N )).


, , 15-25 2013

107

(19.02.2013 .)

...
,
-
( 01.02.01
),
,
,


( 11.193.01)
01.02.01,

, II-III ,
,
, ,
, .

1-
(, 2000);
1- ACM-
(,
2001);
3 ;
.. DonNU United,
7- (2008), 4- (2009) 7- (2010) ACM SEERC,
2011 8- ( ) ACM ICPC.

108


, , 15-25 2013

SCH_Donetsk Erudit, 1- XII .


.. (2013)

.



(x1 , x2 , . . . , xn ), ( )
c1 x1 + . . . cn xn ,
(1)

ai1 x1 + ai2 x2 + . . . ain xn ?bi ,

i = 1, m

(2)

? =, , aij , bi , cj
.
(1) , , (2), , ,
(1).
1. .
, ,
. ,
, .
, ,
.
2. K
. ,
, .
, . n (2),
, .
,

, , 15-25 2013

109


. 
m
, n .
, .
( ) .
.

, (1),
(2) , bi .
, , .. xj 0.
. ,
.
1. . , cj cj . ,
, , .
2. . bi ,
1 ( ), .
3. . xj
xj bj ,
xj = x0j + bj , x0j 0.
xj bj , xj = x0j + bj x0j 0.
, xj ,
xj = x0j x00j ,
x0j 0, x00j 0.
4. .
:

ai1 x1 + ai2 x2 + . . . ain xn bi .


xn+1 :

ai1 x1 + ai2 x2 + . . . ain xn + xn+1 = bi ,


xn+1
0. ,
110


, , 15-25 2013

xn+1 1.
. .
.
3. , P1 , P2 , . . . , Pk , Pi - (2),
x1 P1 + . . . + xk Pk = P0 ,
X = (x1 , . . . , xk ) .
4. x = (x1 , . . . , xn ) ,
Pi , xi ,
.
, m .
-
, i
xg[i] , 1,
. Pg[i] Rn ,
-.

bi i , g[i] = j,
xj =
(3)
0 j 6= g[i] i.
3 .

{Pg[i] }.
P
zj =
cg[i] aij .
1im

5. j zj cj , (3)
.
6. j zj > cj ,
(3) .
= min bi /aij . ,
i:aij >0

i aij ,
. ,
Pg[i] Pj ( j
aij ), j - , xj
. , (3) , (zj cj ) ,
.

, , 15-25 2013

111

j , zj > cj , j
. j ,
zj cj . ,
j j (zj cj ),
.
,
.
7. , ,
6,
.
!
n

.
m
, . - . ,
- 2m + n. ,
O(mn(m + n)) .

(2)
m, , w, .
(1)
(2)
, zj zj w + zj .
,
w .
, ,
, , .

.

, .
, .
. , ,
,
, . .
112


, , 15-25 2013

,
. m , n.
m n.
,
aij ( ) i- ,
j - . , , ,
. , .


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

1
7
2
5
3

2
2
2
3
2

3
5
3
4
1

4
1
4
4
6

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

, , 15-25 2013

113

3. , , 3 , 2 .
3 ( ), -
, ( ),
.
, : 1 ,
, maxi minj aij , , , minj maxi aij .
,
. ,
(
).
, maxi minj aij < minj maxi aij .
, - . ,
,
. ,
.
P

(p1 , p2 , . . . , pm ), pi 0 pi = 1. pi
i- .
p,
q ,
P
V (p, q) =
ai jpi qj . ,
, .
. .
p q ,
p q V (p, q ) V (p , q ) V (p , q).
. V . ,
,
,
, V :
P
P aij pi V, j = 1, n,
pi = 1,
(4)
pi 0,
i = 1, m.
114


, , 15-25 2013

, ,
V , . ,
V . , f (p, V ) = V ,
(4).

-. .
, ( , ,
, ), p0i = pi /V

P 0
P pi =0 1/V min,
aij pi 1,
j = 1, n,
0
pi 0,
i = 1, m.
, , ( ), .
,
, , , , . ,
,
, , .


A. High
:
:
:
:

stdin
stdout
1
256

p. Zp = {0, 1, . . . , p 1}
p.
p.

f (x) = xn + an1 xn1 + . . . a1 x + a0 ,



, , 15-25 2013

115

n , x , ai Zp .
, , x2 + x + 1 Z2 . ,
f (x) = p(x) q(x), p(x), q(x) - ,
f (x).
Z2 .
n Zp . ,
m.

p, n, m , p .
1 p, n, m 109 .


p, n, m.


n Zp m.

stdin
2 2 10
3 4 100

stdout
1
18

B. Junior
:
:
:
:

stdin
stdout
1
256

p. Zp = {0, 1, . . . , p 1}
p.
p.

f (x) = xn + an1 xn1 + . . . a1 x + a0 ,


n , x , ai Zp .
116


, , 15-25 2013

, , x2 + x + 1 Z2 . ,
f (x) = p(x) q(x), p(x), q(x) - ,
f (x).
Z2 .
n Zp .

p, n , p .
1 p, n 109 ; 1 pn < 1018 .


p, n.


n Zp .

stdin

stdout

2 2
3 4

1
18

B.
f (x).
,
f (x) = p1 (x)p2 (x). - ,

, :

f (x) = p1 (x)p2 (x) . . . pk (x).


,
.
, n
( R(n)) :
X
R(n) =
C(I(1), i1 )C(I(2), i2 ) . . . C(I(n 1), in1 ), ()
i1 +2i2 +...+(n1)in1 =n


, , 15-25 2013

117

I(n) = pn R(n)
n, (n, k) n k ,

!
n+k1
C(n, k) =
.
k
(*) n , , ( ) .
, (*) .
R . R(n, k)
,
k . R(n, 0) = 0 I(n) = pn R(n, n 1). ,
k .
:
dn/ke

R(n, k) =

C(I(k), i)R(n, k 1).

i=0

R(i, k) 1 i n, 0 k i1
O(n2 log n). ,
Junior, p
pn 1018 , n 60.
High .
1. f (x) n
Zp . Zp ,
, f () = 0. F = Zp ()
c0 + c1 + c2 2 + . . . cn1 n1 , ci Zp
( n ( )
f () = 0). :
a) F f (x) Zp ,
f (x) n
2

, p , p , . . . , p

n1

()

) f (x)|xp x.
118


, , 15-25 2013

. a) ai Zp ,
api = ai . s N

ps

f ( ) =

n
X

i ps

ai ( ) =

i=0

n
X

(ai i )p = f ()p = 0.

i=0

, (a + b)p = ap + bp
( p, p ).
, , (**) f (x).
s
t
, . , p = p , 0 s < t n1.
s+r
s
r
s
r = t s , p p = (p )p = 0,
r

p = ,

0 < r < n.

, pn F r
xp x, r < n.
, (**) .
) F pn ,
n
xp x = 0. ,
n
f (x) G(x) = xp x.
G(x) f (x) F .
Zp , f (x)
G(x) Zp .
2. Zp () Zp g(x)
m (m|n), Zp
h(x), deg h(x) 6 |n.
. g(x) Zp
m
m, m
1 g(x)|xp x. m|n. pm 1|pn 1
n
m
n
n
xp 1 1|xp 1 1. , xp x|xp x g(x)|xp x.
n
xp x Zp (), g(x)
.
g(x) m Zp (),
[Zp () : Zp ] = m. ,
Zp () Zp () Zp , m|n.
3. n N
X
pn =
dI(d).
d|n

. f (x) Zp
deg f (x) = n. 1 Zp (),

, , 15-25 2013

119

f (x), f (x) n
.
2 g(x) Zp
d, d|n, Zp () . 1 d g(x).

Zp (),
, P
. Zp ()
dI(d) :
d|n

pn

dI(d).

d|n

, Zp () n
xp x, , r(x). 2 deg r(x)|n :
X
pn
dI(d).
d|n

.
4. F (n) f (n) ,
:
X
f (d) = F (n),
d|n

n :
X
n
(d)F ( ) = f (n).
d
d|n

(n) ,

1,
(n) = (1)k

:
n = 1,
1 = . . . = k = 1,
i > 1,

k n
, i .
120


, , 15-25 2013

(. .,
., . , c.161).
3 4 f (n) = nI(n)
F (n) = pn ,
n Zp :

I(n) =

n
1X
d|n(d)p d .
n


d n, .

n. 9
O( n). l ( , n 10
l 9). ,
, 2l
.
O(log n)
n
d
p
.
, n,
mn, m.
mn 1018 , ,
O(log mn) .
,
l O( n + 2 log n(log m + log n)).

C. High
:
:
:
:

stdin
stdout
1
256

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

, , 15-25 2013

121

,
50. .


, ( ).


, , 108 .

stdin
001
110
00
10

stdout
0.50000000
0.25000000

D. Junior
:
:
:
:

stdin
stdout
1
256

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

,
10. .

122


, , 15-25 2013


, ( ).


, , 108 .

stdin
001
110
00
10

stdout
0.50000000
0.25000000

D.
,
l. ,
l , . , , l
, l
. : p00...0
, l , p11...1
l , pa1 a2 ...al ,
al , al1 ..
:

1, a1 a2 . . . al ,
pa1 a2 ...al = 0, a1 a2 . . . al ,

1 (pa ...a 0 + pa ...a 1 ) .


2
l
2 2 l
2l 2l . , .
( ).
, l

, , 15-25 2013

123

1 X
pa1 a2 ...al .
P = l
2
ai {0,1}

,
.
, , ,
l = max(l1 , l2 ) .
a1 a2 . . . al ( )
a1 a2 . . . al (
) (
).
O(23l ). , ,
. ,
( ), .
, Junior ,
.
High . ,
,
. l1 l2 , l1 + l2 + 1.
, 101 .
O(l2 ),
O(l3 ). O(l),
( -). ,
, . 0
( ), 1 ,
, parent[x] x (
, next[x][c] , x c, last[x] x, suf f [x]
, .. (
124


, , 15-25 2013

),
x, pr[x][c] x
c, .. , ( x + c). pr[1][0] = pr[1][1] = 0,
p[0] = 1. (
), :

suf f [x] = pr[suf f [parent[x]]][last[x]],



next[x][c],
next[x][c] 6= 1,
pr[x][c] =
pr[suf f [x]][c] .
:

1
ppr[x][0] + ppr[x][1]
px =
2
x, . , . , p0 .
( ,
(.. ), O(l2 ).
- ,
.
:
p = Ap,
. k
p = Ap, , k p = A2 p.
k
k , A2
, , .
, p0 .
O(kl3 ).

E. High
:
:
:
:

stdin
stdout
2
256

, N , .
, , 15-25 2013

125

,
C .
,
.
M . , , ,
.

N , C , M .
1 N 1018 , 1 C 100, 0 M C(C + 1)/2.


N , C , M . M ,
.



109 + 7.

stdin
2
1
2
3
1

3 2
2
3
2 1
2

stdout
5

F. Junior
:
:
:
:

stdin
stdout
1
256

, N , . ,
C .
,
.
126


, , 15-25 2013

M . , , ,
.

N , C , M .
1 N 10000, 1 C 100, 0 M C(C + 1)/2.


N , C , M . M ,
.



109 + 7.

stdin
2
1
2
3
1

stdout

3 2
2
3
2 1
2

F.
A ,
.. Aij 1, i j , 0, .
. f (n, c) n,
c.
C
P
f (N, c). , f (1, c) = 1. ,
c=1

f , :

f (n, c) =

C
X

Acd f (n 1, d).

()

d=1


, , 15-25 2013

127

, N C ,
O(C) ,
O(N C 2 ).

Junior, High.
f (n, c) n -
fn . (*) :

fn = Afn1 ,
, , :

fN = AN 1 f1 .
,
O(log N C 3 ), ,
O(C 3 ).

G. - High
:
:
:
:

stdin
stdout
1
256

N .
K . , ,
-.

-.

N , K .
1 N 100000, 0 K N/2. 109
.


N K . N , .

128


, , 15-25 2013



,
- K .

stdin
5
0
3
2

stdout

2
3 4 7 9
1
7 5

13 3
5 2

H. - Junior
:
:
:
:

stdin
stdout
1
256

N .
K . , ,
-.

-.

N , K .
1 N 10000, 0 K N/2. 109
.


N K . N , .



,
- K .


, , 15-25 2013

129

stdin
5
0
3
2

2
3 4 7 9
1
7 5

stdout
13 3
5 2

H. -
, k .

. .
. k - k k .
- ,

P
P.
N k+1iN xi 1ik xi . ,
.
k - ,
,
i i1 i+1.
, k -, i
j (j < i 1). i 1
, , (i, j) (i, i 1)
. i 1 (
j 0 ), (i, j) (i 1, j 0 ) (i, i 1)
(j, j 0 ). . ( j 0 > i
(i, j 0 ) (i1, j 0 ),
. j 0 6= i + 1,
,
i i + 1 ).
, ,
(i, i + 1). n 1. k
, ,
. , (i, i + 1)
(i 1, i) (i + 1, i + 2).

130


, , 15-25 2013

Junior, , f (n, k) k -
1, 2, . . . , n.

f (n, 0) = 0,
f (n, k) = min(f (n 1, k),
f (n 2, k 1) + (xn xn1 )). k - O(N k).
High, . (i, i + 1), (i 1, i) (i + 1, i + 2)
.
, , (i, i + 1)
(i 1, i) (i + 1, i + 2),
.
,
(i 1, i + 2) wi+1,i+2 + wi1,i wi,i+1 ,
, , .
,
.
, ( ) , c 1 . , ,
, O(log N ).
. , ,
. .
, ( ) , (
).
O(log N ), O(k log N ).

I. High
:
:
:
:

stdin
stdout
1
256

.

, , 15-25 2013

131

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

N , hi .
1 N 105 , 0 hi 1000.


N . N + 1
hi , , i-
(hN +1 , ,
).


, , . N + 1
.
106 . ,
, . ,
N - , ..
132


, , 15-25 2013

stdin
2
0 6 3
3
2 3 4 2

stdout
4.000000
0.333333 0.666667
0.000000
2.800000
0.400000 0.400000
0.200000 0.000000

J. Junior
:
:
:
:

stdin
stdout
1
256

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

N , hi .

, , 15-25 2013

133

1 N 5000, 0 hi 1000.


N . N + 1
hi , , i-
(hN +1 , ,
).


, , . N + 1
.
106 . ,
, . ,
N - , ..

stdin
2
0 6 3
3
2 3 4 2

stdout
4.000000
0.333333 0.666667
0.000000
2.800000
0.400000 0.400000
0.200000 0.000000

J.
,
( 2n ).
.
Gn , ,
, (
), n.
.
k , ,
hk .
, Gk+1 .
134


, , 15-25 2013

GN +1 , , , , VN +1
hN +1 . Gk , k N 1, Vk
pk .
V1 ,
p1 , (1 p1 )p2 , (1 p1 )(1 p2 )p3 ..
(1 p1 )(1 p2 ) . . . (1 pN ).
,
, Gk ( ).
Gk , 2 (N k + 1),
.
f (p) = min (hi p + Vi+1 (1 p)), p [0, 1].
kiN

hi p + Vi+1 (1 p).
Gk+1 Gk ,
, .
,
O(l), l . O(N 2 ) , Junior.
High,
(set), ( ).
,
, , .
N N , N
,

. ,
O(N log N ).


, , 15-25 2013

135

K. High
:
:
:
:

stdin
stdout
1
256

a b. x ,
x = x1 + x2 + . . . + xn ( ) xi , a
b. , [A, B]
.

a, b , 10000.
A, B , 1018 .
A B.


a, b, A, B .


a b [A, B].

stdin
4 5 7 12
6 10 20 30

stdout
4
6

L. Junior
:
:
:
:

stdin
stdout
1
256

a b. x , x = x1 + x2 + . . . + xn
( ) xi ,
a b. ,
[A, B] .
136


, , 15-25 2013

a, b, A, B .
0 a, b 10000, 0 A B 107 .


a, b, A, B .


a b [A, B].

stdin
4 5 7 12
6 10 20 30

stdout
4
6

L.
a b , xa+yb,
x, y 0. , ,
d = gcd(a, b), xa+yb a
b. , a b , d,
A B , dA/de bB/dc . a
b . (
Junior). ,
ab ( ab a b) .
O(ab) , [0, ab).

[A, B].
High, a b , . , a b ,
, . a b ,
. a = b = 0,
0.


, , 15-25 2013

137

M. High
:
:
:
:

stdin
stdout
1
256

a b. , ,
, . , p
a b , i
i + p(i) = j 2 j . a b
.

a, b .
0 a 100, 0 b 100000, a b.


a b.


b a + 1 , p(i) i a
b, p .
a b , 1.

stdin
1 9
3 5

stdout
8 2 6 5 4 3 9 1 7
-1

N. Junior
:
:
:
:

stdin
stdout
1
256

a b. , ,
138


, , 15-25 2013

, . , p
a b , i
i + p(i) = j 2 j . a b
.

a, b .
0 a b 20.


a b.


b a + 1 , p(i) i a
b, p .
a b , 1.

stdin
1 9
3 5

stdout
8 2 6 5 4 3 9 1 7
-1

N.
, ,
a b, ,
. , .

2

O(b b) (
b ). ,
Junior. ,

.
High . b0 , b0 + 1 + b
. [a, b0 ], , b0 + 1 + j b j ,


, , 15-25 2013

139

[a, b]. , a 100 b 200,


. , ( )
, b 200, 500.
,
b.

O. High
:
:
:
:

stdin
stdout
1
256

n D = {0, 1, . . . , b 1} a0 , a1 , . . . , al1 ,
n D
(.. b0 , b1 , . . . , bn1 D k 0 l 1 , bj = a(k+j) mod l
j = 0, n 1. .

n, b .
1 n 1000, 1 b 10, bn 107 .


n b.


n b- ( ).

stdin
2 3
3 2

140

stdout
001102122
00010111


, , 15-25 2013

P. Junior
:
:
:
:

stdin
stdout
1
256

n D = {0, 1, . . . , b 1} a0 , a1 , . . . , al1 ,
n D
(.. b0 , b1 , . . . , bn1 D k 0 l 1 , bj = a(k+j) mod l
j = 0, n 1). .

n, b .
1 n 1000, 1 b 10, bn 104 .


n b.


n b- ( ).

stdin
2 3
3 2

stdout
001102122
00010111

P.
, n D,
(x1 , . . . xn ) (y1 , . . . yn )
, yi+1 = xi i = 1, n 1. n + 1: (x1 , . . . xn , yn ) = (x1 , y1 , . . . yn ).
, , . ,
. .

, , 15-25 2013

141

,
n + 1. , ,
.
,
, .
,
n 1 .

. Junior
, High
, ,
. O(bn ).

Q. High
:
:
:
:

stdin
stdout
1
256

. ( n1 n2 ).
, . ,
, , . ,
, .
, .
, 1 , 0. , 0.5 . N .

A. Aij 1, i j , 0 . ,
.

1 n1 , n2 , N 8.
Aij + Aji = 1 i 6= j , Aii = 0.

142


, , 15-25 2013


N . N
N , A. n1
n1 , .
.


108 .

stdin
3
0
0
0
2
1
3
0
0
1
3
3
3
0
0
1
3
3

1
0
0
3
1

stdout
0.00000000

1
1
0
2
0.50000000

1
0
0
1
1

0
1
0
2 3
2 3
0.66666667

1
0
0
1
2

0
1
0
2 3
2 3

R. Junior
:
:
:
:

stdin
stdout
1
256

. ( n1 n2 ).

, , 15-25 2013

143

, . ,
, , . ,
, .
, .
, 1 , 0. , 0.5 . N .
A.
Aij 1, i j , 0 . ,
(
).

1 n1 , n2 , N 8.
Aij + Aji = 1 i 6= j , Aii = 0.


N . N
N , A. n1
n1 , .
.


108 .

144


, , 15-25 2013

stdin
3
0
0
0
2
1
3
0
0
1
3
3
3
0
0
1
3
3

1
0
0
3
1

stdout
0.00000000

1
1
0
2
0.66666667

1
0
0
1
1

0
1
0
2 3
2 3
0.66666667

1
0
0
1
2

0
1
0
2 3
2 3

R.
. S1 ( ) , , S2 ,
. , .
V (S1 , S2 ) .

. (S10 , S20 ), Si0 = Si ,
i- , Si0 = Si \ j , i- j .
, S1 S2 ,
, , . Junior
,
, . High
, ,

, , 15-25 2013

145

-.
V (, ) = 0.5, V (, S) = 0, V (S, ) = 1
S . V (S10 , S20 ), Si0 i-
.
O(2n1 +n2 N 3 ).

S. High
:
:
:
:

stdin
stdout
1
256

N , N .

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

N , 1 N 100000.


N . N ,
(0 , 1 ).


N , ,
.
108 .

146


, , 15-25 2013

stdin

stdout
1.00000000 0.5000000
0.50000000
0.25000000 0.75000000
0.33333333 0.33333333

3
0 1 0
4
1 0 1 0

T. Junior
:
:
:
:

stdin
stdout
1
256

N , N .

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

N , 1 N 15.


N . N ,
(0 , 1 ).


N , ,
.
108 .


, , 15-25 2013

147

stdin
3
0 1 0
4
1 0 1 0

stdout
1.00000000 0.5000000
0.50000000
0.25000000 0.75000000
0.33333333 0.33333333

T.
, Junior. p(i, a1 a2 . . . an )
, i- ,
n ,
, a1 = 0; , a1 = 1;
, , a1 = 2 (
).
:

a1 = 0,
1,
p(1, a1 a2 . . . an ) = 1/n, a1 = 1,
0,
a1 = 2.
:
p(i, a1 a2 . . . an ) =

p(i 1, a2 . . . an ),

1
1, a2 . . . an ) +
nk (p(i
P
1
p(i 1, a02 . . . a0n )),
nk (

p(i 1, a02 . . . a0n 0)),

a1 = 0,
a1 = 1,
a1 = 2.

k aj 2, a02 . . . a0n , 2 .
O(n2 3n ), ,
3n a1 a2 . . . an .
High, ,
a1 ?1 : (n i + 1)
P
p(i, a1 a2 . . . an ) =
ak
ni+1
ak
1k<i

2. . , , O(n).

148


, , 15-25 2013

(20.02.2013 .)

...
,
, 5 , .
TopCoder: 2309
Codeforces: 2275
C 2012 Codeforces.
VK Cup 2012.

, , 5 , .
TopCoder: 1851
Codeforces: 2145
- VK Cup 2012
Russian Code Cup 2012.
:


2010 . 2013 6 .
ICL (. )
2010 . 2010 - 5 , 2011
- 3 , 2012 - 2 .
NEERC 2010 . 2011 7 .

, , 15-25 2013

149

Best Of 2011 snarknews.info Saratov SU #1 " 2011".

.
:
http://www.cs.princeton.edu/%7Esssix/papers/dto-journal.pdf.


A.
:
:
:
:

stdin
stdout
2
256

, n .
. m
.
1 n. ,
x y , (x, y).
(vi , ui ). :

(vi , ui ) .
,
-,
(vi , ui ).
, +.
.


n m (1 n 104 ; 1
m 3 105 ). m . i-
vi , ui (1 vi , ui n)
(vi , ui ).
150


, , 15-25 2013

, .
, . .


m
.

stdin
3
1
1
1
2
3
3
1
1
1
1
1
4
1
1
1
2
4
3

7
2
2
1
3
1
1
3
3
1
1
1
6
2
3
4
3
1
4

stdout
+
+
+
+
+
+
+
+
+

A.
online O(|V | |E|).
, ,
. ,
,
, . ,
. ,

, , 15-25 2013

151

. ,
(v, u), ,
, ,
v .

B.
:
:
:
:

stdin
stdout
1
256

, n . . .
m .
1 n.
v cv .
, cv = v . ,
x y , (x, y).
(vi , ui ). :

(vi , ui ) .

. p1 , p2 , , pn , (v, u) xv < xu , pxv = v , pxu = u.
n
P
.
pi (106 + 3)( i 1)
1000000009 (109 + 9).

i=1

. v cv = j ,
pj = v .
.


n m (1 n 103 ; 1 m
50000). m . i-
152


, , 15-25 2013

vi , ui (1 vi , ui n; vi 6= ui )
(vi , ui ).
, . , .



1000000009 (109 + 9).

stdin
3
3
2
3
5
5
5
5
5

3
1
1
2
4
1
2
3
4

stdout
8991020
8991020
7991018
350074640
276308329
265326315
264326313

, n , n , 1
n.
x1 , x2 , . . . , xp y1 , y2 , . . . , yq , p < q x1 = y1 , x2 = y2 , . . . , xp = yp ,
r (r < p, r < q), x1 = y1 , x2 = y2 , . . . , xr =
yr xr+1 < yr+1 .

:

2, 3, 1;
2, 3, 1;
3, 2, 1.


, , 15-25 2013

153

B.
online O(|V | |E|). , ,
. ,
,
( O(1)) - . :
1. x[i] = 1000003i , p[i] , .
2. res =

i=n
P

x[i]i1 p[i]

i=1

3. j p,
res. , p[j] = val. , res: res =
res (x[j 1] p[j]); p[j] = val; res = res + (x[j 1] p[j]).

C.
:
:
:
:

stdin
stdout
1
256

, n ,
pj wj .
, n 1
( ,
, ), .
.
.
P

w j Cj .

. - ,
.

154


, , 15-25 2013


n (1 n 50000)
. n pj (1 pj 1000)
. n wj (1 wj
1000) .
n 1 vi ui (1 vi , ui
n) , ui vi . ,
.



. n t1 , t2 , , tn . ti ,
i- .

stdin
3
1
1
1
1
4
3
2
2
3
2
7
1
7
1
1
2
2
3
3

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


, , 15-25 2013

stdout
49
0 3 1

64
7 0 4 6

210
0 1 3 6 10 15 21

155

C.
. i ti
wi . . ,
. P
. ,
wi Ti , Ti
i- .
. , . . n = 2, ,
(1, 2), t1 w2 , t2 w1 . , ,
t2
t1
w1 < w2 , (1, 2),
(2, 1). , n
: wt11 < wt22 , , . ,
wtii .
. . wtii , ,
. : , , , ,
, .
, pv , v .
, .
tpv + tv , wpv + wv . .
t w . , , ,
.
, .
. wtii ,
set (TreeSet). , , , set ,
, set.

156


, , 15-25 2013

D.
:
:
:
:

stdin
stdout
1
256

. n , 1 n.
.
, x tx . .
a 1- b, a
b.
a k - (k > 1) b,
b 1-, a
(k 1)- 1- b.
.
,
( x- ,
x, x > 0).
a k - b,
b k - a.
.
m vi , ki . vi ,
ki ki -
vi .
p1 , p2 , , pr (p1 < p2 < < pr ) (tp1 (tp2 ( tpr ))).
. x y 30- .
.



n (1 n 105 ) . n
. i-
ti ri (0 ti 109 , 0 ri n), ti
i, ri
i 0 i .

, , 15-25 2013

157

m (1 m
10 ) . m
. i- vi , ki
(1 vi , ki n).
, .
5


m , , . ,
.

stdin
6
1
1
0
3
4
5
4
1
1
2
3

0
1
1
2
2
2

stdout
1073741822
1073741823
1073741823
0

1
2
1
1

30-
30- , 30
. 10 132 =
1073741813.
:

0 0 = 1;
0 1 = 1;
1 0 = 0;
1 1 = 1.

158


, , 15-25 2013

D.

O(n n log n). ,


.
.
1. dep(v)
.
2. dep(v),
.
3. v, k . ,
,
dep(v) + k .
4. . .
5.
dep(v) + k , -.
6. ,
(
O(1), -)
O(n n).

E.
:
:
:
:

stdin
stdout
1
256

n . Rd .
d 1 ,
(0, 0, . . . , 0). , .


n (1 n 5000), d (1 d
10).

, , 15-25 2013

159

n d+1 . d
, xj,1 , xj,2 , . . . , xj,d .
yj (|yj | = 1). yj , .
100.
6 .


d n1 , n2 , . . . , nd .
. d
P
sign( xj,i ni ) yj . ,
i=1

.
, |

d
P

xj,i ni |

i=1

104 |n|, |n| .

stdin
2 1
1 -1
-1 1
4 3
-99.749 12.71 -61.33 1
61.7 17.00 -4.0 -1
-29.94 79.192 64.56 1
49.320 -65.178 71.788 -1

stdout
-1.000000000
-0.891651465 0.415648308
-0.179427280

E.
. d- n .
, .
,
, . , .
. r. , , . ,
, 1. : r ,
hr, xi i . :
r0 = 0, xj : hr, xj i 0 ri+1 = ri + xj
160


, , 15-25 2013


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

read(n)
f o r (i = 1; i <= n; i++)
read(x[i], sign[i])
x[i] *= sign[i]
r = 0
run = 1
while (run)
run = 0
f o r (i = 1; i <= n; i++)
i f (r * x[i] <= 0)
r += x[i]
output(r)

.
, , . , u. , , |u| = 1.
. hrk , ui. i : hxi , ui > 0, minhxi , ui ,
i

hrk , ui = hrk1 , ui + hxj , ui hrk1 , ui + , hrk , ui k .


-.
|rk |2 = |rk |2 |u|2 (hrk , ui)2 = (k )2 .
|rk |2 = |rk1 + xj |2 = |rk1 |2 + 2hrk1 , xj i + |xj |2 .
hrk1 , xj i 0, |rk |2 |rk1 |2 + |xj |2 . max |xi |2 ,
i

|rk |2 k . : (k )2 |rk |2 k .
, xi , |xi | = 1, = 1,
= cos t, t [0..) .
(k )2 |rk |2 k
, (k )2 = k , k 2 .


, , 15-25 2013

161

(21.02.2013 .)

. C

( ) , , . ,

http://ru.wikipedia.org/wiki/_.

,
, .
, . ,
, . ,
(
) .
.
1 s t r u c t node{
2
i n t l, r, par; //s[l..r-1] ,

3
map<char , i n t > next;
4
node(i n t l, i n t r, i n t par) : l(l), r(r), par(par){
5
}
6 };


, .
.
1 s t r u c t position{
2
i n t V, L;
3
position(i n t V, i n t L) : V(V), L(L) {}
4 };


.
1 i n t leng(i n t v); // .
1 void split_edge(position pos); // .

162


, , 15-25 2013


1 void add_edge_to_parent(i n t l, i n t r, i n t parent); //
.
1 position read_char(position pos, char c); //
.

:
1 i n t leng(i n t v){
2
r e t u r n t[v].r - t[v].l;
3 }
1 int
2
3
4
5 }
1 int
2
3
4
5
6
7
8
9
10
11 }

add_edge_to_parent(i n t l, i n t r, i n t parent){
i n t nidx = szt++;
t[nidx] = node(l, r, parent);
r e t u r n (t[parent].next[s[l]] = nidx);

split_edge(position pos){
i n t v = pos.V, up = pos.L, down = leng(v) - up;
i f (up == 0) r e t u r n v;
i f (down == 0) r e t u r n t[v].par; // if
i n t mid = add_edge_to_parent(t[v].l, t[v].l + down, t[v].par);
t[v].l += down, t[v].par = mid;
t[mid].next[s[t[v].l]] = v;
r e t u r n mid;

1 pt read_char(position pos, char c){


2
i n t v = pos.V, up = pos.L;
3
i f (up > 0)
4
r e t u r n s[t[v].r] == c ? position(v, up - 1) : position(-1, -1);
5
else{
6
i n t nextv = t[v].next.count(c) ? t[v].next[c] : -1;
7
r e t u r n nextv != -1 ? position(nextv, leng(nextv) - 1) : position(-1,
-1);
8
}
9 }


s, t O(|s|2 ).
,
. .
1 void make_tree(){
2
node root(-1, -1, -1);
3
t[szt++] = root;
4
5
f o r (i n t i = 0; i < szs; ++i){
6
position pos(0, 0);


, , 15-25 2013

163


7
8
9
10
11
12
13
14
15
16
17
18
19
20 }

f o r (i n t j = i; j < szs; j++){


position npos = read_char(pos, s[j]);
i f (npos.V != -1)
pos = npos;
else {
i n t mid = split_edge(pos);
add_edge_to_parent(j, szs, mid);
break;
}
}
}

, . ,
1000000, .

.
, (v, u) ,
u u. (v, u) , link(v) = u. , v
dep(v).
1. ()
. .
2. .
,
L(L > 1) . , ,
x(|x| > L) y(|y| > L) , L. ,
link(v) x1 (|x1 | = |x| 1), y1 (|y1 | = |y| 1)
, L 1
link(v), . ,
link(v), . link(v)
.
3. v dep(link(v)) dep(v) 1.
164


, , 15-25 2013

v .
x1 , x2 , , xdep(v) (xdep(v) = v).
link(x1 ), link(x2 ), , link(xdep(v) ) , . , .
.
? w
, w . ,
( k ) ( 2),
k dep(k) dep(w).
.
1 position fast_go_down(i n t v, i n t l, i n t r){
2
i f (l == r) r e t u r n position(v, 0);
3
while(t r u e ){
4
v = t[v].next[s[l]];
5
i f (leng(v) >= r - l)
6
r e t u r n position(v, leng(v) - r + l);
7
l += leng(v);
8
}
9
throw;
10 }
1 i n t link(i n t v){
2
i f (t[v].link == -1)
3
t[v].link = split_edge(fast_go_down(link(t[v].par), t[v].l + i n t (t[v
].par == 0), t[v].r)); // .
4
r e t u r n t[v].link;
5 }

, link split_edge.
.
.

, . s = s0 s1 s2 s|s|1 ,
s0 , s0 s1 , , s0 s1 s|s|1 .
.
( ), , .
.
:

, , 15-25 2013

165


1 position add_char_to_tree(position pos, i n t i){
2
while(t r u e ){
3
position npos = read_char(pos, s[i]);
4
i f (npos.V != -1) r e t u r n npos;
5
6
i n t mid = split_edge(pos);
7
8
add_edge_to_parent(i, szs, mid);
9
10
pos = position(link(mid), 0);
11
12
i f (mid == 0)
13
r e t u r n pos;
14
}
15
throw;
16 }
1 void make_tree(){
2
node root(-1, -1, -1); root.link = 0;
3
t[szt++] = root;
4
5
position pos(0, 0);
6
f o r (i n t i = 0; i < szs; ++i){
7
pos = add_char_to_tree(pos, i);
8
}
9 }


A. -
:
:
:
:

stdin
stdout
1
256

k k k , .
, .
k 3
( ), :

2 ;
2 .


k (1 k 100)
.
166


, , 15-25 2013


, -1.
. k
k k . k k k
. k - . ,
.
w, b.
, .
.

stdin
1
2

stdout
-1
bb
ww
bb
ww

A. -
1. , - , ,
.
2. .
3. , .
4. ,
,
.
, .
k .
5. .
:


, , 15-25 2013

167

wwwwwwww
wbbbbbbw
wbwwwwbw
wbwbbwbw
wbwbbwbw
wbwwwwbw
wbbbbbbw
wwwwwwww
6. n n,
n n n.
7. , -
(.. , - ).
.

B. -
:
:
:
:

stdin
stdout
1
256

n. y .
,
(1) (2).
,
2n n
. ,
.
, .


n (1 n 50) .


,
n , 2n .
.
, . , .
168


, , 15-25 2013

stdin
1
2

stdout
2
12

B. -
1. , . .
n = 1 .
2. n a.
3. n + 1. n n + 1
n. n , n + 1: 10n + a 2 10n + a.
b c .
4. b c - n + 1, bmod2n+1 = cmod2n+1 = 0.
(b c)mod2n+1 = 0. b c = 10n = 2n 5n .
2n+1 , ,
.
5. n,
. n = 1 .
n. n
, n + 1.

C.
:
:
:
:

stdin
stdout
1
256

n m . .
,
.


, , 15-25 2013

169

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



n m (1 n m 105 ) .
n m .


, .

stdin
2 2
ab
ba
1 9
aaabbbccc
4 4
xyzx
yzxx
zxxy
xxyz
1 1
a
170

stdout
2

2
5


, , 15-25 2013

C.
1. , -
, , ,
.
2. , ,
- , . , ,
, ,
.
3. ,
.
4. , ,
,
.
5. ,
. , , x
(x 2)/2 ,
.
6. x 2 (x
2)/2 + 1, , 1,
.

D. -
:
:
:
:

stdin
stdout
1
256

-,
n , , (v, u)(v 6=
u) v u .
,
(
).

, , 15-25 2013

171


n (3 n 1000)
.


-1, ,
.
n , n ,
, a . ,
1 n. av,u = 0,
v u, av,u = 1,
.
, :

av,u + au,v = 1 v, u (1 v, u n; v 6= u);


av,v = 0 v (1 v n).

stdin
3

stdout
0 1 0
0 0 1
1 0 0
-1

D. -
1. n 3.
n + 2 .
2. : s, t.
3. t s.
4. v : s v v t.
5. s 1.
s t 2.

172


, , 15-25 2013

6. t 1.
s 2.
7. t s 1. t
2.
8. n = 3,
n 3,
.
9. n = 4, ,
.
10. n = 6. , , ,
n 6.

E. -
:
:
:
:

stdin
stdout
1
256

. -.
: a, b, c.
a, b, c.

a: a , b c
. , b c
.
b: b , a c
. , a c
.
c: c , a b
. , a b
.
, (, )
a, b c .
, ,
, ,

, , 15-25 2013

173

, . ,
.


: a, b, c (0
a, b, c 109 , 1 a + b + c 109 ) ,
.



, .
, -1.
, , a, b,
c, ], [ . 1000 .
c answer:
character ::= a | b | c;
sequence ::= character | character sequence;
block ::= character | number [ sequence ];
answer ::= block | block answer | << >>;
number (1 number < 1010 ) ,
. , , , .

. character , number [ sequence ] number sequence. .

stdin
1 1 0
2 0 0
0 0 1
5 4 5

174

stdout
1
c
-1
0
13
4[abc]b


, , 15-25 2013

E. -
1.
.
2. , 001.
3. , ,

(.. ),
.
4. , .
5. , 1,
, ,
.
6. 001,
, .
7. Ec 00a(a 6= 1),
, .
8. 01a, ,
10a 1,
01a 1. a
010.
9. Ec aa + 1b, ,
a 01b + a.
10. abc.
a 2bc : a 1b 1c + 1,
a 1 1b 1 + 1c + 1 1, a 2bc.
11. .
a, a + b. , b .
12. , a + b 2, b1
2
aa + 1c, .

, , 15-25 2013

175

F.
:
:
:
:

stdin
stdout
1
256

n, k .
[f rom, to] idx,
[lef t, right] (f rom to, lef t right).
1 int
2
3
4
5
6
7
8
9
10
11
12
13
14 }

get(i n t idx, i n t left, i n t right, i n t from, i n t to) {


i n t ans = 1;
i f (left == from && right == to)
r e t u r n ans;
i n t mid = (left + right) / 2;
i f (from <= mid)
ans += get(2 * idx + 1, left, mid, from, min(mid, to));
i f (to >= mid + 1)
ans += get(2 * idx + 2, mid + 1, right, max(from, mid + 1), to);
r e t u r n ans;

,
. , .
[f rom, to] (0 f rom to n 1),
get(0, 0, n 1, f rom, to) k .
1000000009 (109 + 9).


: n (1 n
10 ), k (0 k 1018 ).
18


1000000009 (109 + 9).

stdin
6 5
10 6
4 6

176

stdout
4
9
0


, , 15-25 2013

F.
1. , get(idx, l, r, from, to)
, .
2. O(log n),
k 10 log n, 0.
3.
z[L][V ][K], V , K , , L
, .
4. V l r.
5. L = 0, [f rom, to],
l f rom to r.
6. L = 1, [l, to],
l to r.
7. L = 2, [f rom, r],
l f rom r.
8. (L, V, K) :
9. k < 1, 0, V .
10. k = 1,
[l, r]. V .
11. mid =

l+r
2 .

12. L = 1, [l, to].


13. to mid,
,
z[1][2 V + 1][K 1].
14. mid < to,
,
2 V + 1, z[1][2 V +
2][K 2].

, , 15-25 2013

177

15. L = 2 .
16. L = 0, [f rom, to].
17. to mid,
, z[0][2V +1][K 1].
18. mid < f rom,
, z[0][2V +2][K 1].
19. , , . lk , lk 1 k 2
z[2][2 V + 1][lk] z[1][2 V + 2][k 1 lk].
20. , ,
- .
, .
21. O(log n). .
22. O(log n) .
n.
23. i x. x ,
x1
i + 1 x2 , x1
2 2 + 1.
24. i : x x + 1. x ,
x+1
i + 1 x2 x2 + 1, : x+1
2 2 1.
25. O(log n) ,
O(log n).
26. O(k cdot log n)
O(k). O(k 2 log n).
27. k 10 log n, O(log3 n).
28. O(log2 n). .

178


, , 15-25 2013

G.
:
:
:
:

stdin
stdout
5
512

p q , n , m : l1 , r1 , l2 , r2 (l1 r1 ; l2 r2 ).
1 n, [l1 , r1 ] ( ),
[l2 , r2 ] ( ).
, n , n , 1
n.
v (1 v n) g1 , g2 , , gn
i, gi = v .


n (1 n 106 ) .
n , : p1 , p2 , ..., pn (1 pi n)
.
q1 , q2 , , qn .
m (1 m 2 105 )
.
m . i- : a, b, c, d (1
a, b, c, d n). l1 , r1 , l2 , r2 a, b, c, d
:
1. x. , 0,
.
2. f (z) = ((z 1 + x) mod n) + 1.
3. l1 = min(f (a), f (b)), r1
min(f (c), f (d)), r2 = max(f (c), f (d)).

max(f (a), f (b)), l2



.

, , 15-25 2013

179

stdin
3
3
3
1
1
4
4
2
3
1
1
1

stdout
1

1 2
2 1
2 3 3
3 2 1
3 4 1

1
1
2

2 3 4
3 2 1
4 2 3

G.
1. , . p1 , p2 , ..., pn .
2. (l1 , r1 , l2 , r2 )
x, l1 x r1 x
[l2 , r2 ].
3. (l1 , r1 , R)
x, l1 x r1 x
[1, R].
4. (l1 , r1 , l2 , r2 ) (l1 , r1 , r2 )
(l1 , r1 , l2 1).
5. , .
.
6. 1 n, . .
7. . i pi .
(l1 , r1 , i)
[l1 , r1 ] .
180


, , 15-25 2013

8. ,
,
.

H. -
:
:
:
:

stdin
stdout
5
256

-.
n m. .
i j (xi,j , yi,j ) (1 i n, 1 j m).
- , ,
k . t (1 t k) -
- ((t 1) mod n) + 1.
, .
, -, k .
-
? .


n, m, k (1 n, m 105 , 1
k 109 , nm 105 ) -.
n 2m :
i xi,1 , yi,1 , xi,2 , yi,2 , , xi,m , yi,m (|xi,j |, |yi,j |
109 ).


, -.


, , 15-25 2013

181

stdin
2 2 3
1 0 -1 0
0 1 0 -1
2 1 10
1 1
-1 -1
2 3 1000000000
1001320 -1000 123 1233
2233 -1232
-12300 -10 -1233 -15533
2233 -123

stdout
5

50

251779971234500000000000000000

H. -
1. , .
i ,
.
2. . n m + 1 .
(0, 0). , .
3. .
.
4. ,
, , .

182


, , 15-25 2013

I. -
:
:
:
:

stdin
stdout
1
256

1 n,
1 n .
, .
a1 , a2 ,
ak k .
.
, .
. , . ,
n.
, . ,
, , . , .


n k (1 n
10 , 1 k 106 ) .

a1 , a2 , , ak (1 ai < n, ai < ai+1 ) .
, n .
9

stdin
5
1
5
2

2
3
2
3


, , 15-25 2013

stdout
1
0

183

I. -
1. n 1 ,
, .
2. , n 1,
.
3. n n 1 ,
, .
4. ,
.
5. , ,
i , i- .
6. . - 1.
1 , i-.
7.
, , -
, ,
, .
8. , a1 , a2 , ..., am (ai
ai+1 ).
, ai ai+1 .
9. d1 , d2 , ..., dm , d1 == a1 , di = ai
ai1 . .
, x ,
x .
10. . . .

184


, , 15-25 2013

J.
:
:
:
:

stdin
stdout
1
256

s. s
. , .


s (1 |s| 105 ),
$, .


0 n1 ,

. ASCII-
.
n .
n 1 , ,
.
v : p, lf, rg , p
(0 p < n, p 6= v) . ,
p v , s[lf...rg 1] (0 lf < rg |s|).


, , 15-25 2013

185

stdin
aaa$

b$

ababa$

stdout
7
0 3
0 0
2 3
2 1
4 3
4 2
3
0 1
0 0
10
0 5
0 0
2 5
2 1
4 5
4 3
0 1
7 5
7 3

4
1
4
2
4
4
2
2
6
1
6
3
6
6
3
6
6

s[l...r] (0 l r < |s|) s = s0 s1 . . . s|s|1 ( |s|


s) sl sl+1 . . . sr .

J.
,
.

K.
:
:
:
:

stdin
stdout
1
256

s t. , 186


, , 15-25 2013

s t. ,
.


s (1 |s| 105 ),
$, .
t (1 |t| 105 ),
#, .


0 n1 ,

. ASCII-
.
n .
n 1 , ,
.
v : p, w, lf, rg ,
p (0 p < n, p 6= v) , w (0 w
1) . w = 0,
, p v , s[lf...rg 1] (0 lf <
rg |s|). w = 1, , p v ,
t[lf...rg 1] (0 lf < rg |t|).


, , 15-25 2013

187

stdin
ab$
ac#

aba$
baab#

stdout
8
0 1 2 3
0 0 2 3
0 0 0 1
3 0 1 3
3 1 1 3
0 0 1 3
0 1 1 3
14
0 1 4 5
0 0 3 4
0 0 0 1
3 0 3 4
3 1 2 5
3 0 1 2
6 1 4 5
6 0 2 4
0 0 1 2
9 1 4 5
9 0 2 3
11 0 3 4
11 1 2 5

s[l...r] (0 l r < |s|) s = s0 s1 . . . s|s|1 ( |s|


s) sl sl+1 . . . sr .

K.
1. s + t.
2. t.
3. s,
s t.
4. ,
, , $.
188


, , 15-25 2013

L.
:
:
:
:

stdin
stdout
1.5
256

s. q .
:
1. s. + c, c ,
s.
2. s.
? l, l s,
. , s
( |s| l + 1, ).
, +,
, - .


q (1 q 2 106 )
.
q , . , . ,
. , c
a, b, c, d, e.
, l
s.


.
.


, , 15-25 2013

189

stdin
13
+ a
? 1
+ a
? 1
? 2
+ a
? 1
? 2
? 3
+ b
? 3
? 1
? 2
8
+ a
+ b
+ a
+ b
+ a
? 3
? 2
? 4

stdout
+
+
+
+
+
+

L.
online . ,
.
. ,
.

190


, , 15-25 2013

M.
:
:
:
:

stdin
stdout
1
256

s, t k .
, s t . k -
.


s t (1 |s|, |t| 105 ).
k (1 k 1018 ).
.


-1, .

stdin
aaa
abaa
3
aabbbabba
abbabbab
1
baab
bbaababb
3
bbbbaaba
abbabbabba
2


, , 15-25 2013

stdout
-1

aab

ab

191

M.
1. s + $ + t + #.
2. $,
s.
3. #
$, t.
4. , s, t.
5. , , .
6. k -
.

N.
:
:
:
:

stdin
stdout
1
256

. a b, a b, b
a.
s.
s, k , , s
, , .
.


s (1 |s| 105 ) .
k (1 k min(100, |s|))
.
.
192


, , 15-25 2013



. , -1.

stdin
aba
1
aaa
1
abacaba
3

stdout
-1
3
11

N.
1. s.
2. - s.
3. ,
.
4. z[V ][K], V
, K ,
V .
5. V , ,
V
V . k 1

V .
6. V .
7. . z2[V ][U ][K], V U , K
, V .

, , 15-25 2013

193

8. z2 U K , ,
U , 1 K z[U ][U K] + z2[V ][Y ][K U K], e
Y U V .

194


, , 15-25 2013

(22.02.2013 .)
K
...
, 28
1985 .
11
1992 - 1993., No. 84 1993 - 1998 ., 33 .
1998 2002 .
2007 - , 2005 - 2007 . . .
2007-2009 .
Google -.
C 2012 .
:

Google CodeJam 2010;


x13 2006;
2012 8 TopCoder 3
Codeforces.

s
, :
1. s.
.
2. t0 .

, , 15-25 2013

195

3.
. s ,
- s.
t0 ,
, s.
, aba (* ):

,
.
t s. s, t.
endP os(t). s endP os (
t0 endP os
[1 . . . lengths(s) 1). .
u w s ( u w). , u
w, endP os(v) endP os(w) ,
endP os(w) endP os(u).
- w - .
, , ,
w,
. ,
t0 , ,
v t0 ,
, v .
. ,
s[0 . . . i]
s[0 . . . i + 1].
, .
196


, , 15-25 2013

t0 0, null
. last,
.
c :

cur , last + 1.
last ,
c.
c cur.
- p,
c q , 2 :
q p + 1, q
cur
.
clone c , p + 1,
,
q , , q .
q cur clone,
p ,
c q ,
clone.

c, cur t0 .
last cur.
. s
c. (p, q) , p
q . .
cur ,
s + c (endP os(s + c) = length(s), , ,
). w
s, w + c
s. w s
w + c s. ,
w c ( w + c , , ), endP os w + c
length(s).
endP os(w + c) , , endP os(q)

, , 15-25 2013

197

( q , ,
w c). clone.
, . ,
(
2). , . ,
, ,
.
(p, q)
t0 p q ,
, t0
- .
,
. , 2n1,
- 3n 3, n s.
,
( ,
). ,
last
(q ) .
length(s) .

1 p u b l i c c l a s s SuffixAutomaton {
2
p u b l i c f i n a l i n t [] length;
3
p u b l i c f i n a l i n t [] link;
4
p u b l i c f i n a l i n t [] first;
5
p u b l i c f i n a l i n t [] next;
6
p u b l i c f i n a l i n t [] to;
7
p u b l i c f i n a l i n t [] l a b e l ;
8
p u b l i c i n t size;
9
p u b l i c i n t last;
10
11
p u b l i c SuffixAutomaton(CharSequence s) {
12
i n t count = s.size();
13
length = new i n t [2 * count];
14
link = new i n t [2 * count];
15
first = new i n t [2 * count];
16
next = new i n t [4 * count];
17
l a b e l = new i n t [4 * count];
18
to = new i n t [4 * count];
19
Arrays.fill(first, -1);
20
link[0] = -1;
21
size = 1;
22
i n t edgeSize = 0;
23
last = 0;
24
f o r (i n t i = 0; i < s.length(); i++) {
25
i n t c = s.charAt(i);

198


, , 15-25 2013

K
26
i n t current = size++;
27
length[current] = length[last] + 1;
28
f o r (i n t previous = last; ; previous = link[previous]) {
29
i f (previous == -1) {
30
link[current] = 0;
31
break;
32
}
33
i n t index = findEdge(previous, c);
34
i f (index != -1) {
35
i n t curLink = to[index];
36
i f (length[previous] + 1 == length[curLink])
37
link[current] = curLink;
38
else {
39
i n t clone = size++;
40
length[clone] = length[previous] + 1;
41
link[clone] = link[curLink];
42
i n t linkEdge = first[curLink];
43
while (linkEdge != -1) {
44
next[edgeSize] = first[clone];
45
first[clone] = edgeSize;
46
l a b e l [edgeSize] = l a b e l [linkEdge];
47
to[edgeSize++] = to[linkEdge];
48
linkEdge = next[linkEdge];
49
}
50
f o r (; previous != -1; previous = link[previous]) {
51
i n t edge = findEdge(previous, c);
52
i f (edge == -1 || to[edge] != curLink)
53
break;
54
to[edge] = clone;
55
}
56
link[current] = link[curLink] = clone;
57
}
58
break;
59
}
60
next[edgeSize] = first[previous];
61
first[previous] = edgeSize;
62
l a b e l [edgeSize] = c;
63
to[edgeSize++] = current;
64
}
65
last = current;
66
}
67
}
68
69
p u b l i c i n t findEdge(i n t vertex, i n t l a b e l ) {
70
i n t edge = first[vertex];
71
while (edge != -1) {
72
i f (t h i s .l a b e l [edge] == l a b e l )
73
r e t u r n edge;
74
edge = next[edge];
75
}
76
r e t u r n -1;
77
}
78 }


, , 15-25 2013

199


s. ,
s
.
1 + ,
.

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


A. Chords
:
:
:
:

stdin
stdout
2s
256 Mb

Consider a circle. There are 2N different points marked on it and numbered


with integer numbers in range from 1 to N so that each number from the
interval has two points corresponding to it.
Points with the same numbers are connected by segments. Thus weve got
N chords. Futhermore, the chords are numbered as well: the chord number
200


, , 15-25 2013

i connects the two different points with number i. Apparently some of the
chords may intersect. Now it would be nice to find out for each single chord
the number of chords it intersects.


In the first line of the input file there is a single integer number N (1
N 105 ). In the next line there are 2N integers in range from 1 to N
the numbers assigned to the points in the order of traversal. Each number is
written exactly twice. All the numbers in the line are separated with spaces.


The output file is supposed to contain exactly N lines: on the i-th line
write the number of chords that i-th chord intersects.

stdin
5
1 2 3 1 4 2 5 5 3 4

stdout
2
3
3
2
0

A. Chords
, .
,
, .
, , . ,
, . , ,
.

B. Cyclic suffixes
:
:
:
:

stdin
stdout
2s
256 Mb

Consider a string S = s1 s2 s3 . . . sn1 sn over an alphabet . A cyclic



, , 15-25 2013

201

expansion of order m over a string S is the string s1 s2 s3 . . . sn1 sn s1 s2 . . .


of m characters; that is, we concatenate instances of S until the resulting
string has sufficient length, and then take the first m characters as the result.
Let cyclic string S be the infinite cyclic expansion over a string S .
Take a look at the suffixes of a cyclic string S. Obviously, there are no
more than |S| different ones among them: the (n + 1)-th one is equal to the
first (the cyclic string itself), the (n + 2)-th is equal to the second, and so on.
However, there may be even less different suffixes. For example, if S = abab,
the first suffixes of S are:

S1
S2
S3
S4

=
=
=
=

ababababab . . .
bababababa . . .
ababababab . . .
bababababa . . .

etc. Here, only two different suffixes can be found, while |S| = 4.
Let us order the first |S| suffixes of S lexicographically. If two suffixes are
equal, the one with the lower index comes first. We are now interested in the
following problem: what is the position of our original string S after sorting?
The following example shows the ordering for S = cabcab.

(1)
(2)
(3)
(4)
(5)
(6)

S2
S5
S3
S6
S1
S4

=
=
=
=
=
=

abcabcabca . . .
abcabcabca . . .
bcabcabcab . . .
bcabcabcab . . .
cabcabcabc . . .
cabcabcabc . . .

Here, the position of S = S1 is 5.


Given a string S of length n, find the number of S in the specified ordering.


On the first line of the input file, a string S (1 6 |S| 6 1 000 000) is given.
The input consists of lowercase Latin letters only.


Output the only number on a line by itself the number of S in the
specified ordering of its first |S| suffixes.
202


, , 15-25 2013

stdin
abracadabra
cabcab

stdout
3
5

C. A Coloring Game
:
:
:
:

stdin
stdout
2s
256 Mb

Two players play a graph coloring game. They make moves in turn, first
player moves first. Initially they take some undirected graph. At each move,
a player can color an uncolored vertex with either white or black color (each
player can use any color, possibly different at different turns). Its not allowed
to color two adjacent vertices with the same color. A player that cant move
loses.
After playing this game for some time, they decided to study it. For a
start, theyve decided to study very simple kind of graph a chain. A chain
consists of N vertices, v1 , v2 , . . . , vN , and N 1 edges, connecting v1 with
v2 , v2 with v3 , . . . , vN 1 with vN .
Given a position in this game, and assuming both players play optimally,
who will win?


The first line of the input file contains the integer N , 1 N 100 000.
The second line of the input file describes the current position. It contains
N digits without spaces. ith digit describes the color of vertex vi : 0
uncolored, 1 black, 2 white. No two vertices of the same color are
adjacent.


On the only line of the output file, print FIRST (without quotes) if the
player moving first in that position wins the game, and SECOND (without
quotes) otherwise.


, , 15-25 2013

203

stdin

stdout

5
00100
4
1020

SECOND
FIRST

C. A Coloring Game
x0 . . . 0x x0 . . . 0y ( x, y
0 ). , 1,
0 . (0
) .
0 xor 0 1 xor 1, 1.
0 xor 1 1 xor 0.
,
0. , ,
,
, 2.

D. Hippopotamus
:
:
:
:

stdin
stdout
2s
256

You think that your roof looks unpretty. So you opt for a new one,
consisting of n consecutive long narrow boards. You have two types of boards:
wooden ones and iron ones, giving you an amazing total of 2n possible roofs.
But the safety should not be left aside. Having considered the weight and
the cruising speed of a falling hippopotamus, you decide to have at least k
iron boards among every m consecutive boards.
How many possibilities do you have?


The input file contains three integers, n, m and k , separated by spaces
and/or line breaks. 1 n 60, 1 m 15, 0 k m n.


Output the number of possibilities.
204


, , 15-25 2013

stdin
10 2 1
5 5 2
3 2 2

stdout
144
26
1

D. Hippopotamus
(-
, m 1 ).
, .
, 0
1, 0
0.

E. False RSA
:
:
:
:

stdin
stdout
2s
256 Mb

Roman, Serge and Andrew has decided to upgrade the famous RSA
encryption algorithm. They think that the limitation that modulo n used in
RSA must be a product of two distinct primes is redundant. Instead they plan
to use n which is the product of k -th powers of two distinct primes: n = pk q k .
However, Nick pointed out that besides all other mathematical problems,
the scheme may be easier to crack. That is it is difficult to factorize n
which is a product of two distinct primes, because it has exactly one nontrivial factorization. On the other hand, in case n = pk q k there can be more
different non-trivial factorizations. For example, 100 = 22 52 has eight nontrivial factorizations: 100 = 250, 100 = 2225, 100 = 2255, 100 = 2510,
100 = 4 25, 100 = 4 5 5, 100 = 5 20 and 100 = 10 10.
Now Roman, Serge and Andrew wonder given n = pk q k , how many
different non-trivial factorizations of n are there?


The input file contains one integer number n (6 n 1018 , it is guaranteed
that n = pk q k for different primes p and q and integer k > 0).


, , 15-25 2013

205


Output one integer number the number of different non-trivial
factorizations of n.

stdin
6
100

stdout
1
8

E. False RSA
,
p k . k .9
k
, p <p
q , p < (n), 10 .
k > 1, p < (109 ),
p. , k = 1.
, k log6 1018 < 24. , (a = p, b = q , c = p
, d = q ),
pa q b , p
, c, p c, q ,
d. .

F. Perspective
:
:
:
:

stdin
stdout
2s
256 Mb

Breaking news! A Russian billionaire has bought a yet undisclosed NBA


team. Hes planning to invest huge effort and money into making that team
the best. And in fact hes been very specific about the expected result: the
first place.
Being his advisor, you need to determine whether its possible for your
team to finish first in its division or not.
More formally, the NBA regular season is organized as follows: all teams
play some games, in each game one team wins and one team loses. Teams
are grouped into divisions, some games are between the teams in the same
division, and some are between the teams in different divisions.

206


, , 15-25 2013

Given the current score and the total number of remaining games for each
team of your division, and the number of remaining games between each pair
of teams in your division, determine if its possible for your team to score at
least as much wins as any other team in your division.


The first line of the input file contains N (2 N 20) the number
of teams in your division. They are numbered from 1 to N , your team has
number 1.
The second line of the input file contains N integers w1 , w2 , . . . , wN ,
where wi is the total number of games that ith team has won to the moment.
The third line of the input file contains N integers r1 , r2 , . . . , rN , where ri
is the total number of remaining games for the ith team (including the games
inside the division).
The next N lines contain N integers each. The j th integer in the ith line of
those contains aij the number of games remaining
Pbetween teams i and j .
It is always true that aij = aji and aii = 0, for all i j aij ri .
All the numbers in the input file are non-negative and dont exceed 10 000.


On the only line of output, print YES (without quotes) if its possible for
the team 1 to score at least as much wins as any other team of its division,
and NO (without quotes) otherwise.

stdin
3
1
1
0
0
0
3
1
1
0
0
0

stdout
YES

2
1
0
0
0

2
1
0
0
0
NO

2
1
0
0
1

2
1
0
1
0


, , 15-25 2013

207

F. Perspective
, .
, . , , ,
, . ,
,
, ,
, , . ,
, .

G. Circular Railway
:
:
:
:

stdin
stdout
2s
256 Mb

There are L stations along a circular railway, numbered 1 through L.


Trains travel in both directions, and take 1 minute to get from a station to
the neighbouring one (i.e., between 1st and 2nd, between 2nd and 3rd, . . . ,
between (L 1)-th and L-th and between L-th and 1-st).
There are n employees houses along the railway, and n offices, each house
or office located near a railway station. You are to establish a one-to-one
correspondence between houses and offices in such a way that total travel
time (sum of travel times of each employee) is minimized.


The first line of the input file contains two integer numbers, n and L
(1 n 50000, 2 L 109 ). The second line contains n locations of the
employees houses, and the third line contains n locations of the offices. Each
location is an integer number between 1 and L. Some houses or offices or
both can be located at the same railway station.


Output the minimal total travel time followed by the description of the oneto-one correspondence. The description should be represented by n numbers
208


, , 15-25 2013

(one for each employee, ordered as in the input), denoting the 1-based index
of the office assigned to the corresponding employee.

stdin
3 15
1 2 10
11 12 13
4 12
2 5 8 11
6 9 12 3

stdout
9
2 3 1
4
4 1 2 3

G. Circular Railway
:
. , ,
,
, 2. ,
ai , i
i+1 ( , , an , n 1),
a1 ( ai+1 ai = , i + 1 ).
, a1 ,
ai . ,
ai , ai . ai a1 ,
.

H. SETI
:
:
:
:

stdin
stdout
2 seconds
256 s

Amateur astronomers Tom and Bob try to find radio broadcasts of


extraterrestrial civilizations in the air. Recently they received some strange
signal and represented it as a word consisting of small letters of the English
alphabet. Now they wish to decode the signal. But they do not know what to
start with.

, , 15-25 2013

209

They think that the extraterrestrial message consists of words, but they
cannot identify them. Tom and Bob call a subword of the message a potential
word if it has at least two non-overlapping occurrences in the message.
For example, if the message is abacabacaba, abac is a potential word,
but acaba is not because two of its occurrences overlap.
Given a message m help Tom and Bob to find the number of potential
words in it.


Input file contains one string that consists of small letters of the English
alphabet. The length of the message doesnt exceed 10 000.


Output one integer number the number of potential words in a message.

stdin
abacabacaba

stdout
15

H. SETI
. ,

endP os.
,
.
.
.

I. 2-3 Trees
:
:
:
:

stdin
stdout
2s
256 Mb

2-3 tree is an elegant data structure invented by John Hopcroft. It is


designed to implement the same functionality as the binary search tree. 2-3
tree is an ordered rooted tree with the following properties:

the root and each internal vertex have either 2 or 3 children;


210


, , 15-25 2013

the distance from the root to any leaf of the tree is the same.
The only exception is the tree that contains exactly one vertex in this
case the root of the tree is the only vertex, and it is simultaneously a leaf,
i.e. has no children. The main idea of the described properties is that the tree
with l leaves has the height O(log l).
Given the number of leaves l there can be several valid 2-3 trees that have
l leaves. For example, the picture below shows the two possible 2-3 trees with
exactly 6 leaves.

Given l find the number of different 2-3 trees that have l leaves. Since this
number can be quite large, output it modulo r.


Input file contains two integer numbers: l and r (1 l 5 000, 1 r
10 ).
9


, , 15-25 2013

211


Output one number the number of different 2-3 trees with exactly l
leaves modulo r.

stdin
6 1000000000
7 1000000000

stdout
2
3

I. 2-3 Trees
k l
p , 2 3 . 2-3 v l
p v p
2-3 p .

212


, , 15-25 2013

(23.02.2013 .)

...
, .
. Google ( 2008).
(, 2009) (, 2009-2010).
ACM ICPC, NEERC ACM ICPC. .
.
.
Google .
:

(ACM ICPC Tokyo 2007);


TopCoder Open, TopCoder Collegiate Open, Global
Google Code Jam, Google Code Jam Europe;
(4- , ) ACM-ICPC World Finals 2009, ,
.


, , 15-25 2013

213

.

:
;
;
, .
, , ,

, .
( , ):
( ,
);
.
,
;
, .
,
. ,
:
;
;
.
, . , P (W ),
:
P (A) P (A + e)
P (A) P (B)=>P (A + e) P (B + e)
A B , e .
, .
,
.

s t, . ,
214


, , 15-25 2013

. ,
:
, . d[v] s v e[v]
v t.
s t
,
u v . w
max(d[u], e[v]), w+max(d[u], e[v]).
.
.
-
. D[k][v] s v k . D[k + 1]
D[k]. , , N 1 ,
N . ,
.
E[k][v] s v , k .
-
. N 1
W [v]. W [v] k . ,
W [v] D[k][v].
.
-
N W , ,
.
,
. , , , N 1 ,
.


, , 15-25 2013

215


A.
:
:
:
:

stdin
stdout
1
256

N M .
.
.
.
1,
N .
,
. ,
. ,
.
. , , ,
. ,
,
.
, , ,

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

, .
, , .
,
, , .
216


, , 15-25 2013


N , M K
(2 N 2 000, 1 M 100 000, 1 K 100 000). M
: Bi , Ei ,
i- , Ti ,
(1 Ti 10 000).
K
, : Pi , ,
Vi , (1 Vi 10 000).
1 N .

.


,
, NO.

, YES.

.
, .
.

.


, , 15-25 2013

217

stdin
4
1
1
2
3
2
2
2
3
4
4
1
2
2
1
2
2
3

4
3
2
3
4
1
2
1
4
1
3
2
3
4
2
1
2
5

stdout

5
6
2
2
1

YES
2
1 4

4
2
1
3

NO

A.
. (
).
, ,
. L,
R,
[L T, R], T
. , . , ,
( ,
).

218


, , 15-25 2013

B.
:
:
:
:

stdin
stdout
1
256

, , . .

, .
. , , . -
, . K .
,
, . , ,
K , ,
. 1,
N . ,
.
, , ,
1 ( ,
, ). , :
, , , , . .
, . ,

, .


N , M K (1 N 100, 0 K M 20000). M
: a b , , t
( , 1000).


, , 15-25 2013

219


,
. 1, .

stdin
5
1
2
1
3
4
1
1
1
2
2
3

4
2
4
4
5
6
2
3
4
3
4
4

2
1
1
1
1
2
1
1
2
1
1
1

stdout
-1

B.

. D[V ]
N .
N 0. V , V , .
, V .
U K + 1 ( ), D[V ] + W (V - ,
W - U V ). ,
K . D[U ]
K D[V ] + 2 W D[V ] + W K + 1 .
, U , K + 1,
.
O(M logM ). N ,
O(N M logM ). -

220


, , 15-25 2013

D[V ] + 2 W D[V ] + W ,
O(M logM logK).

C.
:
:
:
:

stdin
stdout
1
256


,
. , .
:

1
.

2 .
.
, (, A , B C ).
, (
).
(, ;

). , , ,
.


N (1
N 40). N X1 , Y 1, X2 , Y2 , T ,
,

(,
1 2 , ,
). ,
200, ,
107 .

, , 15-25 2013

221


,
, , ,
.
2- . ,
.

stdin

stdout
0 0
1.00

1
0 0 1 1 1
5
0
1
0
0
2

0
0
0
0
2

0
0
1
1
1

1
1
0
1
1

0 0
3.25

1
10
1
1
1

3
1 1 1 2 10
1 2 2 2 10
1 1 2 2 50
16
0 0 0 1 1
-1 0 0 0 1
2 1 1 0 1
0 0 1 0 1

2 2
35.00

-2
0
2
0

1 3 1 1
3 1 3 1
0 1 1 1
1 1 1 1

-2
1
2
-1

1 1 0 1
3 2 2 1
0 1 0 1
1 1 2 1

-2
2
2
0

1 1 2 1
2 2 1 1
1 1 1 1
3 1 2 1

0 0
4.50

C.
, , . .
,
, .
,
. ,
,
. , ,
.

, . ,
, .
222


, , 15-25 2013

,
.
. ,

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

,
,
.
. , . ,
1 ,
.
. ,
. , , .
, ,
. , , :
1 f o r k:=1 t o N do


, , 15-25 2013

223


2
3
4

f o r i:=1 t o N do
f o r j:=1 t o N do
i f a[i,j]$<$a[i,k]+a[k,j] then a[i,j]:=a[i,k]+a[k,j];

.
A[i,j] ( A[i,j] , i j, ,
,
, 109 ). Ck [i,j] (k=0,. . . ,N). Ck [i,j] i
j, 1 k. ,
1 k (
- ),
k+1 N. . ,
, .
.
C0 [i,j] , . , C0 [i,j]
A[i,j].
CN [i,j] ,
, .
Ck1 [i,j],
Ck [i,j] : Ck [i,j]:=min (Ck1 [i,j],
Ck1 [i,k]+Ck1 [k,j]). , i j,
1 k. :
k. 1 k-1.
Ck1 [i,j].
k.
: i
k,
1 k-1 (
Ck1 [i,k]), k j
,
1 k (Ck1 [k,j]).
, Ck [i,j].
C0 , C1 , C2 ..
CN
224


, , 15-25 2013

.
, Ck Ck1 , N
, , ,
, . ,
(, ).
.

, ,
. - ,
, ,
, .
.
L
T1 T2. T1 = T2+L T2 = T1+L,
, T1 T2 ,
. , T1 T2
L (, ).
T1 T2 L. ,
,
- . ,
,
. !
, , L/2. ,
.
T1 T2, .. , 0,
T.
T + (L-T)/2. . L
1 . T
, T ., ,
L-T 2 /, .. (L-T)/2. T = 0 L/2, T = L L, .

.


, , 15-25 2013

225

.
.
. ,
, . ,
. ,
, .
, 2.

.
, . ,
X/4, X . , 0.0, 0.25, 0.5 0.75,
.
:
1
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

Program Matches;
Const
TaskID=f;
InFile=TaskID+.in;
OutFile=TaskID+.out;
Const
MaxN=42; { N }
MaxG=2*MaxN+1; { }
Infinity=MaxLongInt; { "" }
Var
N:I n t e g e r ;
Match:Array[1..MaxN]Of Record
X1,Y1,X2,Y2:I n t e g e r ;
Time:LongInt;
End;

{
{
{

{
{

}
}
}
}
}

NG:I n t e g e r ;
Vertex:Array[1..MaxG]Of Record
X,Y:I n t e g e r ;
End;
Edge,Distance:Array[1..MaxG,1..MaxG]Of LongInt;

{
}
{
}
{ }
{
}
{
}

Res:Extended;
{ }
ResX,ResY:I n t e g e r ; { }
Procedure Load;
Var
I:I n t e g e r ;

226


, , 15-25 2013


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

Begin
Assign(Input,InFile);
ReSet(Input);
Read(N);
For I:=1 To N Do
With Match[I] Do
Read(X1,Y1,X2,Y2,Time);
Close(Input);
End;

Function GetVertex(VX,VY:I n t e g e r ):I n t e g e r ;


{ , .
}
Var
I:I n t e g e r ;
Begin
For I:=1 To NG Do
With Vertex[I] Do
I f (X=VX) And (Y=VY) Then Begin
GetVertex:=I;
Exit;
End;
Inc(NG); { }
With Vertex[NG] Do Begin
X:=VX;
Y:=VY;
For I:=1 To NG-1 Do Begin
Edge[I,NG]:=Infinity;
Edge[NG,I]:=Infinity;
End;
Edge[NG,NG]:=0;
End;
GetVertex:=NG;
End;
Procedure AddEdge(X1,Y1,X2,Y2:I n t e g e r ; Time:Longint);
{ , }
Var
A,B:I n t e g e r ;
Begin
A:=GetVertex(X1,Y1);
B:=GetVertex(X2,Y2);
Edge[A,B]:=Time;
Edge[B,A]:=Time;
End;
Procedure BuildGraph; { }
Var
I:I n t e g e r ;
Begin
NG:=0;


, , 15-25 2013

227


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

For I:=1 To N Do
With Match[I] Do Begin
AddEdge(X1*2,Y1*2,X1+X2,Y1+Y2,Time);
AddEdge(X1+X2,Y1+Y2,X2*2,Y2*2,Time);
End;
End;
Procedure FindShortestPaths;
Var
K,I,J:I n t e g e r ;
Begin
Distance:=Edge;
For K:=1 To NG Do
For I:=1 To NG Do I f Distance[I,K]<Infinity Then
For J:=1 To NG Do I f Distance[K,J]<Infinity Then
I f Distance[I,K]+Distance[K,J]<Distance[I,J] Then
Distance[I,J]:=Distance[I,K]+Distance[K,J];
End;

Function BurnAt(At:I n t e g e r ):Extended;


{ , At }
Var
I,J:I n t e g e r ;
Cur,ThisEdge:Extended;
Begin
Cur:=0;
For I:=1 To NG Do I f Distance[At,I]>Cur Then Cur:=Distance[At,I];
For I:=1 To NG Do
For J:=I+1 To NG Do I f Edge[I,J]<Infinity Then Begin
I f (Distance[At,I]<Distance[At,J]+Edge[I,J]) And
(Distance[At,J]<Distance[At,I]+Edge[I,J]) Then Begin
I f Distance[At,I]<Distance[At,J] Then
ThisEdge:=Distance[At,J]+(Edge[I,J]-(Distance[At,J]-Distance[At,I
]))/2
Else
ThisEdge:=Distance[At,I]+(Edge[I,J]-(Distance[At,I]-Distance[At,J
]))/2;
I f ThisEdge>Cur Then Cur:=ThisEdge;
End;
End;
BurnAt:=Cur;
End;

123
124
125
126
127
128
129 Procedure Solve;
130 Var
131
I:I n t e g e r ;
132
Cur:Extended;
133 Begin
134
Res:=Infinity;
135
For I:=1 To NG Do
136
With Vertex[I] Do
137
I f Not Odd(X) And Not Odd(Y) Then Begin

228


, , 15-25 2013


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

Cur:=BurnAt(I);
I f Cur<Res Then Begin
Res:=Cur;
ResX:=X Div 2;
ResY:=Y Div 2;
End;
End;
End;
Procedure Save;
Begin
Assign(Output,OutFile);
ReWrite(Output);
WriteLn(ResX, ,ResY);
WriteLn(Res/2:0:2);
Close(Output);
End;
Begin
Load;
BuildGraph;
FindShortestPaths;
Solve;
Save;
End.

D.
:
:
:
:

stdin
stdout
1
256


. ,
, :
, , , ,
.
, . , .
,
,
, .

, , 15-25 2013

229

,
. . ,
. ,
,
.


N , (1 N 3000). N
Ai , Bi ,
(0 < Ai < Bi < 106 ).


,
.
, .
, .

stdin
5
1 10
10 12
1 10
1 10
23 24

stdout
5
5 10 12 23 24

D.
-
. , .
:
,
. :

230


, , 15-25 2013

:
1 const
2
MaxN=3000;
3
Infinity=MaxLongInt; { }
4
5 type
6
TSegment = record
7
left, right : longint;
8
end;
9
10 var
11
n : longint;
{ }
12
segment : array [1..MaxN] Of TSegment; { }
13
point : array [1..MaxN*2] Of longint;
{, }

,
.
(N 3000) , ,
.
. . : [1,6], [3,7],
[12,14], [14,16].

[1,6] . ,
5 6. ?
, .
! ,
, . ,
, :
,
.


[3,7]. , , 1 2,

, , 15-25 2013

231


, . 6 ,
5 . :
1 PrevLast := right-1;
2 Last := right;
3 { right .}

[3,7]
, ,
.
1 i f left <= PrevLast then < >;

[12, 14]. ,
lef t > last.
.

[14, 16]. , Last = lef t.


16.
14.
1
2

PrevLast := Last;
Last := right;

, . , ,
,
. , :
1 procedure solve;
2 var
3
Last, PrevLast : longint; { }
4
i : longint;
5 begin
6
res := 0; { }
7
Last := -Infinity;
8
PrevLast := -Infinity;
9
f o r i := 1 t o N do
10
with segment[i] do
11
i f last < left then
{ }
12
begin

232


, , 15-25 2013


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 end;

inc(res);
point[res] := right-1;
inc(res);
point[res] := right;
PrevLast := right-1;
Last := right;
end
else
i f PrevLast < left then
{ }
begin
inc(res);
point[res] := right;
PrevLast := Last;
Last := right;
end;

E.
:
:
:
:

stdin
stdout
1c
256

, . , ,
,
. 6 : (N), (S), (W), (E), (U)
(D). .
,
.
N, S, W, E, U, D.
M.
,
. 1,
, , , ,
1. 1, . , ,
:


, , 15-25 2013

233


N
N
S
NUSDDUSE
W
UEWWD
E
U
U
D
WED
S(3)
:
1) 1 S;
2) N(2), U(2), S(2), D(2), D(2), U(2),
S(2), E(2).
2, , : SNNUUSNUSDDUSEDWEDDWEDUUSNUSDDUSEE ,
. 34.


N, S, W, E, U D .
100 ( ).
: N, S, W, E, U, D,
, 100.


, . ,
109 .

stdin
N
NUSDDUSE
UEWWD

stdout
34

U
WED
S 3
234


, , 15-25 2013

E.
.
L(C), L .
L(C + 1) R(C).
d[L][C] ,
L(). L ABC...Z , d[L][C +1]
d[A][C] + d[B][C] + ... d[Z][C] + 1.

F. Shortest Path
:
:
:
:

stdin
stdout
1
256

s .
u s u


: n, m s -
,
(2 n 2 000, 1 m 5 000).
m . . -
, 1015 .
. .


n - s u
u, *, -, s
u.


, , 15-25 2013

235

stdin
6
1
2
1
3
5
4
6

7
2
3
3
5
4
3
1

1
10
5
100
7
10
-18
-1

stdout
0
10
*

F. Shortest Path
-, .

G.
:
:
:
:

stdin
stdout
3
256

, . n .
. (
i j ), , cij

i j .
: ,
- ? ,
?


n m
(1 n 300, 0 m n(n1)
2 ), n , m . : m
i, j , cij cji (1 i n, 1 j n; i 6= j; 104 cij
104 , 104 cji 104 ). ,
. .

236


, , 15-25 2013


, , ,
0, .

stdin
4
1
1
2
3

4
2
3
4
4

stdout
4

-10 3
1 -10
-10 -1
0 -3

, a1 ,
a2 , . . . , ak , a1 a2 , a2 a3 , . . . , ak1
ak , ak a1 .
, . k , . ,
.

G.
-.
,
. ,
. : .
- ,
. , .


, , 15-25 2013

237

H.
:
:
:
:

stdin
stdout
1
256

, , N .
D( ). , , .
.


N (1 N 1000).
S1 S2 (1 S1, S2 N ; S1 6= S2)
, .
R (0 < R 109 ) .
D (0 < D 4 109 ). N
G1 T1 G2 T2, :
G1 , (0 G1 90);
T1 : N , S ;
G2 , (0 G2 180);
T2 : E , W .


,
, 1, .

stdin
2 1 2 10000 31416
89 S 13.12 W
89 N 78.8 E
3 1 3 10000 15000
89.9 S 13.12 W
0.001 S 63.12 W
89 N 78.8 E

238

stdout
1

-1


, , 15-25 2013

H.
. , , D. ,
. .
.

I.
:
:
:
:

stdin
stdout
1
256

- . . , , ,
, . , ,
, , ( , ,
).
1 N . 1 ,
, 0 . ,
, E . ,
, .


N (2 N 100) E
(2 E N ). M (2 M 100), . M .
Ki (2 Ki N )
, ,
Ki , ,
( 0 109 ). .

, .

, , 15-25 2013

239


,
.
, 1.

stdin
5
4
2
2
4
3

3
1
2
5
1

stdout
20

5 2 10
10 4 15
0 4 17 3 20 2 35
2 3 40 4 45

I.

.
, . ,
.

J.
:
:
:
:

stdin
stdout
1
256

" - ,
, . .
.
.
( )
, . , ,
.
: ? , .

240


, , 15-25 2013


N , (2 N 30000). N ,
.
( ,
). , , ,
32000.


, .

stdin
13
5 3 1 3 5 2 5 3 1 3 5
2 5
4
1 1 1 1
4
1 2 3 1

stdout
6

1
3

J.
,
: Ai N .
Bi , .

, Bi : Ai ( ).
. Ai N

, , 15-25 2013

241

Bi K. Bi Ai , N
K . , K 1 N . , K N , Ai Bi .
.
. N = 12, K = 2. , Ai .
Ai N/K = 6 ,
Bi .

, Bi .

:
1 j := 1;
2 while (j <= n-k) and (a[j] = a[j+k]) do inc(j, k);

.
(2 6= 3), 2 .

Bi 1 K .
Pascal:
1 res := 0;
2
f o r k := 1 t o n do { }
3
i f n mod k=0 then
4
begin
5
Ok := t r u e ;
6
f o r i := 1 t o k do
7
begin
8
j := i;
9
while (j <= n-k) and (a[j] = a[j+k]) do inc(j, k);

242


, , 15-25 2013


10
11
12
13
14
15
16
17

i f j <= n-k then Ok := f a l s e ;


}

end;
i f Ok then
{ ,
begin
B }
res := k;
break;
end;
end;

.
. N = 12, K = 4. Ai N/K = 3
,
Bi .

,
.
, , a = b b = c ,
a = c.

. Ai ( j ) a[j]
a[j + k].
1
2 res := 0;
3
f o r k := 1 t o n do { }
4
i f n mod k=0 then


, , 15-25 2013

243


begin
j := 1;
while (j <= n-k) and (a[j] = a[j+k]) do inc(j);
i f j > n-k then
begin { , B }
res := k;
break;
end;
end;

5
6
7
8
9
10
11
12
13

, nmodk = 0 . , 30000 50 , 50
Bi , 30000. , N > 106
.
. Ai .


Ai .
Bi (, !)
, Ai ,
- Ai .
. , --. , , . ,
. , . : , .: , 2000.

K.
:
:
:
:

stdin
stdout
1
256

NxN , KxK
S .

244


, , 15-25 2013


N, K, S
(1 N 100; 1 K N , 0 S K 2 ).


. ,
. ,
.

stdin
3 2 1

4 2 2

stdout
0
0
0
1
0
1
0

0
1
0
0
1
0
1

0
0
0
0
1
0
1

1
0
1
0

K.
. , .
( ) - . :
K K , S , N N .
. . K K , S . -
. , .
,
, . S , ,
. S > K 2 , . Pascal,
. gen k, s .
true, ,
false :


, , 15-25 2013

245


1
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

const
MaxK=100;
type
Square = array [1..MaxK, 1..MaxK] o f byte;
{ KxK, }

f u n c t i o n gen(k, s : word;
var
i, j : i n t e g e r ;
CurS : word; {
Begin
CurS:=0;
fillchar(a, sizeof(a),
f o r i := 1 t o k do
f o r j := 1 t o k do
i f CurS < s then
}
begin
a[i, j] := 1;
inc(CurS);
end
end;
i f CurS < s then gen
end;

var a : Square) : boolean;

KxK }

0);

{ }

{ }

:= f a l s e e l s e gen := t r u e ;

. K K N N . , . N = 7, K = 3, S = 4.
gen :

7*7:

246


, , 15-25 2013

,
3x3 , 4. ?
3x3 , .
(1,1) ,
, , , .
(1,2)

,
. ?
, 4 -!
,
.
, .
, . ,
-, ! -,
.

, ,
, !
, N N .


, , 15-25 2013

247

N*N .
:
1 i f gen(k, s, a) then
{ }
2
f o r i := 1 t o n do
{ N*N}
3
begin
{ }
4
f o r j := 1 t o n do
5
w r i t e (a[(i-1) mod k+1,(j-1) mod k+1], );
6
writeln;
7
end;


:
:
:
:

stdin
stdout
1
64

,
. ,
f .
x f (x).
.


. 100000.
x f (x),
x. .


:
.

248


, , 15-25 2013

stdin
1
1
2
1 2
2
2 1

stdout
1
2
1

(. strongly connected),
.
s t ,
s t t s.
.


, , 15-25 2013

249

(23.02.2013 .)

...
,
1989 . . 9-
, 11- - . . , .
:

, .
ACM ICPC (2012 2013 ).
Russian Code Cup 2012.
2011 Codeforces.


A.
:
:
:
:

stdin
stdout
1
256

.
4 4 16 .
, .

250


, , 15-25 2013

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


n (1 n 100).
n , . 4
4 .
, <, >.


,
.
First,
Second. Draw.


, , 15-25 2013

251

stdin
3
<<<<
<<<<
<<<<
<<<<

stdout
Second
First
Second

<<<<
<><<
<<><
<<<<
<<>>
<<>>
>><<
>><<

A.
16- , 0
, 1 . , , (
, 1
0).
. .
.
. .

First Second. Draw
.
O(216 163 + n).

252


, , 15-25 2013

B.
:
:
:
:

stdin
stdout
1c
256

.
, n m .
, . .
,
.
.
.
, .
( ).

( ).
:

, , , .
,
(
).

. (
) , .

.
, .


n m (3 n, m 50).
n m . :

. .

, , 15-25 2013

253

# ().
H (Harry) .
P (Philosophers stone) .
E (Exit) .
B (Blue fog) .
R (Red fog) .
. H, P
E . P
# R.


IMPOSSIBLE .

, :

L (Left) .
R (Right) .
U (Up) .
D (Down) .
G (Get) .
T (Throw) .
.
.

254


, , 15-25 2013

stdin
5 5
#####
#H.P#
#B#R#
#..E#
#####
4 5
#####
#H#.#
#P#E#
#####

stdout
RRGLLTRRDDLLGRR

IMPOSSIBLE

B.
, : ,
. , , .
, , .
.
IMPOSSIBLE.
O(n2 m2 ).

C.
:
:
:
:

stdin
stdout
1
256

n
a1 , a2 , , an .
:
ai ai ai+1 ( i = n an an a1 ),
.

, , 15-25 2013

255

a1 , a2 , , an

a1 a2 , a2 a3 , , an a1 .
,
.


n (2 n 105 ).
n . i- ai
(0 ai 109 ).


, . Never

.

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

stdout
5
Never
0

:
: 3, 6, 7, 2, 4, 1, 0, 5;
1: 5, 1, 5, 6, 5, 1, 5, 6;
2: 4, 4, 3, 3, 4, 4, 3, 3;
3: 0, 7, 0, 7, 0, 7, 0, 7;
4: 7, 7, 7, 7, 7, 7, 7, 7;
5: 0, 0, 0, 0, 0, 0, 0, 0.
256


, , 15-25 2013

C.
, 2k (k 0),
:

a1 a1+2k , a2 a2+2k , , an an+2k .


( , 1 n,
, ).
n = 2k . n ( ) . 0 n.
n = 2m t ( t 2).
2x 2m mod 2m t. : 2xm 1 mod t.
x = m + z(t). x
:

a1 a1+2m , a2 a2+2m , , a2 an+2m .


, ai ai+2m ,
(
) Never. i ai = ai+2m ,
n = 2m .
0 n.
, , ,
0 n. .
O(n log n).

D.
:
:
:
:

stdin
stdout
3
256

.
,
. ,
. .

. .


, , 15-25 2013

257

, .
,
.
. , .
.

, .
, ,
.
( ) .

.
.
,
. . ,
,
.

.


n (1 n 105 ).
n . i- op
x1 y1 x2 y2 . op , add del
. x1 y1 , x2 y2
. ,
(x1 = x2 y1 = y2 ),
(x1 6= x2 y1 6= y2 ).
1 109 .


n . i-
i- .

258


, , 15-25 2013

stdin
7
add
add
add
add
del
add
add

3
2
5
1
5
3
5

1
2
1
4
3
5
2

3
6
5
7
5
5
5

6
2
6
4
5
5
7

stdout
0
1
2
4
3
5
6

D.
. , . , x0 x.
, x-
x0 .
, .
, , . (, , ).
. , ,
.
, , ,
. O(n),
2 , . , set C++,
O(n log n).
2 : , (,
).
,
.
.

, , 15-25 2013

259

(x, y1 ) (x, y2 ) (y1 < y2 ),


(x, y1 ) +1, (x, y2 +1) -1. (x1 , y)(x2 , y) (x1 < x2 )
(x1 , 0) (x2 , y).
, . ,
( )
.
O(n log n).

E.
:
:
:
:

stdin
stdout
1
256

100 1
10 . 4
, .

1 105 .
, 4 .
5


10 10 . 1 105 .
.


4 . i- i- . , , .
.
.

260


, , 15-25 2013

1 2 3
11 12
18 19
21 22
28 29
31 32
38 39
41 42
48 49
51 52
58 59
61 62
68 69
71 72
78 79
81 82
88 89
91 92
98 99

stdin
4 5 6 7 8 9
13 14 15 16
20
23 24 25 26
30
33 34 35 36
40
43 44 45 46
50
53 54 55 56
60
63 64 65 66
70
73 74 75 76
80
83 84 85 86
90
93 94 95 96
100

stdout
10
17
27

1
3
5
2

100
20 30 50
1 2 3 4 90
35 65

37
47
57
67
77
87
97

,
.

E.
, . 100000 . .
, . .

6 .
.
k (k 19-20)
(A1 , B1 ), (A2 , B2 ), , (Ak , Bk ), Ai = (ai1 , ai2 , ), Bi = (bi1 , bi2 , ),


, , 15-25 2013

261

a- b-
P
P , . , j aij = j bij i.
P
j aij . .
.
.
X Y , .
.
X 0 Y 0 . ,
. Ai ,
i X 0 . Ai
, .
Bi , i X 0 .
X 0 Y 0 .

262


, , 15-25 2013

(24.02.2013 .)

...
- 1989 . - 30. 4-6
. 9- .
2012- .

.
2008-2009 .

http://kruzhok.spbgu.ru/09e http://195.19.228.2/%7Esk1/mm/345/
-
.
2012- .
( 2011-) ( 2004-, )
:

IOI 2005 () 2006 () .


TopCoder Open Algorithm Final 2008, 5-th place.
2008 ( ) 1- .
ACM ICPC
2007 .
ACM Burunduchki.
2008 () 2009 () .


, , 15-25 2013

263

: Google Code Jam Topcoder Open.

. 20

1.
1.
.
c1 c2 , p, i, j : c1 [pi , pj ] = c2 [i, j].
2.
.
, (proot1 = root2 ).
1. . O(nlog n).
. , . , . , .
: .
1. .
O(n log n), , O(n). . . :
(1, 2), (1, 3), (1, 4) (1, 2), (1, 3), (3, 4), (3, 5).
k
X
a0 , a1 , . . . , ak =
ai p i
i=0

2
: 1 + 1 p + 1 p = 1 + (1 + 1 p) p. , , :
k
X
13 +
log ai
i=0

C++ long double.


, , ai , . . ,
264


, , 15-25 2013

. :
k
X

a2i + ai pi + 3 mod 264
i=0

2. . O(nlog n).

, . , - ,
, .
, .
3. . O(n).
1 n.
1. , , ,
-, . , -
.
.
:
1 list[1] <--
2 f o r type = 1..n
3
f o r v i n list[type]
4
p = parent[v]
5
children[p].push_back(type)
6
i f children[p].size() = degree[p] then
7
h = hash(children[p])
8
t[p] = get_type(h)
9
list[t[p]].push_back(parent[v])


. , ,
, ( )
( ) .
4. -.
-? 1 n
1 n. .
() ,
, 1 n , .
, n, -


, , 15-25 2013

265

. O(1), , , ,
-.
5. O(n). .
, .
-. , ,
. ,
children[p].push_back(type)
,
pos[p] = go_down(pos[p], type)
, type , ( type).

2. offline

a, q , . Offline . :
?
li ri
,
(li li+1 , ri ri+1 ),
L++ R++.
?:
1
2
3
4
5
6
7
8
9
10
11

map <i n t , i n t > cnt;


i n t answer = 0, L = 0, R = 0;
f o r (i n t i = 0; i < q; i++) {
while (R <= r[i])
i f (cnt[a[R++]]++ == 0)
answer++;
while (L < l[i])
i f (--cnt[a[L++]] == 0)
answer--;
result[i] = answer;
}

O(q log n).


2q t(n), t(n) L++
R++.
266


, , 15-25 2013

li ri

n . i- b lin c. ri
. R , L

, , n.

: R
n n ( n, n), L
q n ( q

n). (q + n) n t(n), t(n)


l--, l++ r++.

3.
,
n m , (1, 1)
(n, m), (0, 1) (1, 0).
.
, O(nm):
1 i n t f[n + 1][m + 1];
2 f[][] <--- 0
3 f o r (i n t i = 1; i <= n; i++)
4
f o r (i n t j = 1; j <= m; j++)
5
f[i][j] += a[i][j]
6
f[i + 1][j] = max(f[i + 1][j], f[i][j])
7
f[i][j + 1] = max(f[i][j + 1], f[i][j])

( ) f[n][m].

, i- f i + 1-
( ). :
1 i n t cc = 0, f[2][m + 1];
2 f[cc] <--- 0
3 f o r (i n t i = 1; i <= n; i++)
4
f[cc ^ 1] <-- 0
5
f o r (i n t j = 1; j <= m; j++)
6
f[cc][j] += a[i][j]
7
f[cc ^ 1][j] = max(f[cc ^ 1][j], f[cc][j])
8
f[cc][j + 1] = max(f[cc][j + 1], f[cc][j])
9
cc ^= 1

( ) f[cc1][m].


, , 15-25 2013

267


, , ( ) ,
p[n+1][m+1], (i, j) , .
.
.

: i=1, i=n/2, i=n. p[m+1],
(1, 1) (n, x) (n/2, p[x]). ,
: (1, 1) (n/2, p[m]) (n, m). , , , (y1 , x1 ) (y2 , x2 )
O((y2 y1 + 1) (x2 x1 + 1)), (1, 1)
(n/2, p[m]) ( ), (n/2, p[m]) (n, m) ( ). : T (n, m) =
nm + T (n/2, x) + T (n/2, m x) = nm + n2 m + T (n/4, x1 ) + T (n/4, x x1 ) +
T (n/4, x2 ) + T (n/4, (m x) x2 ) = = nm + n2 m + n4 m + ... 2nm.
, x-
m. ,
.

: s n ci wi .
, . .
, O(ns):
1 i n t f[n + 1][s + 1];
2 f[][] <--- 0
3 f o r (i n t i = 0; i < n; i++)
4
f o r (i n t j = 0; j <= s; j++)
5
f[i + 1][j] = max(f[i + 1][j], f[i][j])
6
i f (j + w[i] <= s[i])
7
f[i + 1][j + w[i]] = max(f[i + 1][j + w[i]], f[i][j] + c[i])

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

268


, , 15-25 2013

1 , ,
. (n/2, p[x]) , n/2
p[x] , , n/2
p[x], n n/2
s p[x].

4.

,
+ for.
1. . , ti .
.
0 d
.
. ti .
2. . , ti
deadline di . .
0
deadline-.
. di .
3. . , mi , , wi .
, .
.
mi + wP
i . :
P
wi
mj wi + mi
mj = const
j6=i

wi + mi .
4. . , ti
wi . i- Ci ,
wi Ci .
1

(, s)
, . ,
.


, , 15-25 2013

269

. 0.
, .
. wtii . :
( ) ,
, . : w2 t1 > w1 t2 .
5. . n , ,
.
.
bool less(string a, string b) { return a + b < b +
a; }
: . .
. . ,
n = 2 , (1, 2) (2, 1)
. , . (4)
F (1, 2) = w2 t1 , . (5)
F (s, t) = st . F :
bool less(type a, type b) { return F(a, b) < F(b, a); }
, , , .

.
1. . : n ,
i- ai
, bi .
.
, ,
.
. n = 2. F (1, 2) = a1 + max(a2 , b1 ) + b2 .
:
i < j ai + max(aj , bi ) + bj < aj + max(ai , bj ) + bi
, ai + aj + bi + bj = const,
: i < j min(aj , bi ) > min(ai , bj ).
270


, , 15-25 2013

2. . , .
,
.
. .
( )
(ai ) (bi ). ai 0, ai bi .
, ai 0.
(.. bi 0).
ai . ai bi < 0. ,
. i < j min(bi , ai + bj ) > min(bj , aj + bi ). ,
ai + bj < bj aj + bi < bi , i < j ai + bj > aj + bi
ai bi > aj bj .
,
,
deadline-.
1. . . ,
.
. . , , , .
.
- . . Mij , i
, j , Mij

+, .
, wi Mij , .
j : Mnj 6= +.
O(sort) + O(n2 ).
2. . . s.
,
(.. , ),
. , . Bij

, , 15-25 2013

271

, i j ,
1, , .
j : Bnj 6= 1.
O(sort) + O(ns). 1745.
3. deadline-.
deadline di ti ,
. di . : .
= ( di ) + ( Tij ), i
, j ,
Tij .

deadline- ,
O(n log n).
1.
: , , di . :
di
. (..
deadline), -
. , ti < tmax , tmax
.
tmax , set<int>.
2.
: i : di ti ( ,
), ti ( ,
, ). :
ti ,
ti . , .
: ti .
A = {,
}. , j A,
A di , .
O(log n), j , A
di P
fi = di
tj ,
j

mi =
272

min

fj .

, , 15-25 2013

5.

1, 2, . . . n, .
(1, 2), (2, 3) (1, 3), (3, 2) . ,
n nn2 . , , , :
,
. n 1 ,
n. , ,
nn2 .
.

.
n-
n . . fn,d n
d ( ).
1. fn,d :
2dn+1
2a<n
a<n
X
X
X

(x +
fa,d fna,d ) + (fn,d
fa,d1 Fna,d )
d=1

Fn,d =

i<d
P

a=1

a=1

fn,i , x = D(fn/2,d , 2), n ,

i=1

. D(n, k) n


k . D(n, k) = n+k1
k

, .
2.
n d. (d, n) .
d,
n. fn,d , gn,d,x ,
. gn,d,x
n ,
(d, x). fn,d = gn,d,1 gn,d1,1 (
n d + 1

, , 15-25 2013

273

n d, ,
).
3. gn,d,x .
ax<n
X
gn,d0 ,x0 =
gnxa,d,x D(fx,d , a)
0

a=0

(d , x ) (d, x)
(d := d, x := x+1; if (x = maxN) {d++, x := 0})
4. : f1,1 = 1, g1,1,1 = 1.
,
1. ,
.
2. , g d (..
d d, d + 1). ,
.
3. a > 0 fx,d = 0 , D(fx,d , a) = 0, ,
fx,d = 0, a , break a.
4. D-, C -
n k ) .
 (

f +k
f +k1
+k
f +k

D(f, k + 1) = k+1 =
k+1 = D(f, k) fk+1
k
D(fx,d , a) O(1).
, O(n2 ) O(n3 log n)
, ( ax < n,
a x, , O(n log n)).
,

n 232 . ,
unsigned int,
.
32
31
: a1 mod 232 = a(2 )1 mod 232 = a2 1
16
mod 232 = (a2 1 )2 a mod 232 = . . . 10 . ,
?
274


, , 15-25 2013



nk . nk , ( ). 2k a, a , . :
(k1 , a1 )(k2 , a2 ) = (k1 +k2 , a1 a2 ), : (k1 , a1 )/(k2 , a2 ) = (k1 k2 , a1 /a2 ).
nk , .

, fx,d
, fx,d
k
32
2 . : n k
232 , n 232 ? : .
: 264 , ,
232 :-).

6.

1. . k n- . , ,
, n- , , f (i) ,
i- . i,
x(i) = 0, x(i + 1) = 1.
2. . fn,k (n, k). f2,k = 0, fn,k = min max(fi,k1 , fni+1,k ). ,
i=2..n1

O(n k).
3. 1. k log2 n, ,
k = min(k, dlog2 ne).
O(n2 log n).
4. 2. fi,k fi+1,k , fi,k1, fni+1,k, max(fi,k1 , fni+1,k ) (
fi,k1 fni+1,k ). O(n log2 n).
5. 3. pn,k i,
. pn,k pn+1,k
: pn+1,k = pn,k ,
, pn+1,k 1.
O(n log n).

, , 15-25 2013

275

6. . n, k 1018 ,
1
1
, fn,1 = n2, fn,2 n 2 , fn,k n k ,
- ( )
,
.
(Day1-G)
1. . n , k
, n k .
2. . , n
k . O(1)
( k , k2 , ), ,
O(1),
. fi,j ,
, i j . , j = 1. :
fi,j = min (fp,j1 + sp+1,i )
p=1...i

sl,r [l . . . r]. ,
sl,r = 0. O(nk)
O(n2 k).
3. . pn+1,k pn,k . ,
, (n, k). pn,k pn1,k .
: pn,k1 pn,k pn,k+1 .
. , pn,k fn,k ,
.
:
pn,k1 pn,k pn+1,k
pn,k . pn+1,i = n. =
k X
n
X
(pj+1,i pj,i1 + 1) = O(n2 )
i=2 j=1

, pi,j ,
276


, , 15-25 2013

, . .., .
O(n2 ). , k 2
n2 . k = 2, ai = i, , ..
pn,k = n nk . k = 1 O(1),
n
n
P
P
n2
(pj+1,2 pj,1 +1) = ( j+1
k = 2:
2 0+1) 4 .
j=1

j=1

command-post
1. . n ,
3 k ,
,
. , ,
, ,
, .
2. . : l, r, (l . . . r)
k . [i, i, k 1] i.
(i, i) , . :
fl,r,k = max (fl,m,0 + fm,r,k1 )
m=l+1..r1
3

O(n k).

3. 1. k 1 k 1, k/2
k k/2. k O(log k),
O(n3 log k).
4. 2. m pl,r . ,
pl,r1 pl,r pl+1,r . pl,r .
k
P
(pl+1,r pl,r1 ). pi,j ,
l,r

, , O(n2 ).
O(n2 log k).

7.
: n . 1 + 2 2 + 1 .


, , 15-25 2013

277

k
,
fn,k = fnk,k + fn,k1
:
1 f[0] = 1
2 f o r (x = 1; x <= k; x++)
3
f o r (i = 0; i + x <= n; i++)
4
f[i + x] += f[i]

,
fn,k = fnk,k + fnk,k1
:
1 f[0] = 1
2 f o r (x = 1; x <= k; x++)
3
f o r (i = n - x; i >= 0; i--)
4
f[i + x] += f[i]

k
,
fn,k = fnk,k + fn,k1
: 1, , . ,
fn,k = fnk,k + fnk,k1
, ?
, , . ,
90 ( ).
n
n . ,
. 90
.
k k
fk k , Fk k , fk = Fk Fk1 , Fk =

fi

278


, , 15-25 2013

k
k . i- i. k
, n k(k+1)
2

n,

O(n n).

8.
:
1. . mi wi ,
.
.
2. deadline-. deadline di ti .
deadline-.
3. . H . li
hi . (

).
P
li + hj H , .
, ,
.
4. A. n . i- ,
ai . , i-
, bi . ai ,
bi , , . ,
.
deadline-
P
mjP
wi
mi + mj wi + mi
di = wi + mi , ti = mi ( ti > 0, mi > 0)


, , 15-25 2013

279

P hi S , .
li + (S hj ) P
H (i , j )
(S H) + li hj ( )
mi = hi , wi = li + (S H) ( mi > 0, hi > 0)

.
, (bi 0).
ai .
A,
(bi < 0).P
A + bj ai (i , j bj ,

P )
b j A ai
wi = A ai , mi = bi ( mi > 0, bi < 0)

,
deadline- O(n log n)
set<int>, , ,
,
O(n log n).


A. n
:
:
:
:

stdin
stdout
0.5
256

, -!, , .
! .
n , . ,
, i- ai ,
bi . ,

280


, , 15-25 2013

, ,
, .
, , a1 = 1, b1 = 10, a2 = 10, b2 =
20. ,
10 , ,
. ,
10 .


n (1 n 105 ),
a1 , a2 , . . . an , b1 , b2 , . . . bn (1 ai , bi
109 ).


n , . ,
1.

stdin

stdout

2
1 10
10 20
2
10 10
10 10

2 1

-1

A. n
ai + bi .

B.
:
:
:
:

stdin
stdout
0.5
256

-
, . ,

, , 15-25 2013

281

,
.
,
. , ai , bi .
j k i- ,
bj < ai < bk , bk < ai < bj .
, - ,
.
, .
,
, ,
.


m n
, (1 m, n 100 000).
m ai (0
ai 109 ). n bi
(0 bi 109 ).


k , -
. k : di , ei,1 ,
ei,2 , . , .
, bei,1 < adi < bei,2 .
, , , ,
.

stdin
4 6
2 3 4 5
1 3 2 2 5 2

stdout
2
1 1 2
3 4 5

B.
a b .
. , k , ,
282


, , 15-25 2013

i- (bi , bnk+i ),
.

C.
:
:
:
:

stdin
stdout
0.5
256

N -
. ,
. ,
.
H . hi
li . , , , , hi + li .
, , .
. i j1 , j2 , . . . , jk ,
hj1 + hj2 + . . . + hjk + hi + li .
( hj1 + hj2 + . . . +
hjk + hi + li H ), .
.
, ,
.


N , . N
: i- hi li .
H .


K ,
. K > 0, , . ,
. , .


, , 15-25 2013

283


1 (50 ) n 2 000
1 li , hi , H 105
2 (50 ) n 100 000 1 li , hi , H 109

stdin
2
10 4
5 2
20
6
6 7
3 1
8 5
8 5
4 2
10 5
30

stdout
0

4
1 4 2 5

C.
. . 8, 3, 4,
deadline-.

D.
:
:
:
:

stdin
stdout
0.5
256

. . . i-
, ai
( ). i- , bi ( ,
bi < 0).
.

284


, , 15-25 2013


.
n a0 (109 a0 109 ). n ai bi (109 ai , bi 109 ).


m , . m
, .


1 (50 ) 1 n 103
2 (50 ) 1 n 105

stdin
5
1
6
6
2
2

1
3
-5
-4
2
-1

stdout
4
1 4 3 5

D.
. . 8, 4.

E.
:
:
:
:

stdin
stdout
0.5
256

, . , .

.
, . , -

, , 15-25 2013

285

,
.

.
m ,
,
, .


n (1 n) . n
wi ci (1 wi 105 , 1 ci 109 ), wi
i, ci .


1 (50 ) n 1250
2 (50 ) n 100 000

stdin
3
10
20
30
3
11
20
30

stdout
3

11
100
10
2
11
100
10

E.
. . 8, 1.

286


, , 15-25 2013

F.
:
:
:
:

stdin
stdout
1
256

n n 1 . ,
.
.


n (2 n 300 000). n 1
1 n .


n 1 . i- i-
.


1 (50 ) n 3 000.
2 (50 ) n 300 000.

stdin
5
1
2
2
5

2
3
4
1

stdout
13
8
8
9

F.
. dfs-
v s1 [v] v ,
s2 [v] v . dfs-
.
a b.
s1 [a], t1 [b] = n s1 [a],
s2 [a], t2 [b] ,

, , 15-25 2013

287

: t2 [a] = t2 [b] + t1 [b] + s2 [b] (s2 [a] + s1 [a]). : t2 [root] = 0.


(a, b) : s1 [a] t2 [b] + s2 [a] t1 [b] + s1 [a] t1 [b].

G.
:
:
:
:

stdin
stdout
0.5
256

, n . ,
.
.
. , , , p
.


n
p (1 p n 1000). n 1
. ai , bi (1
ai , bi n) .


q
. .
.
. ,
.


1 (50 ) 1 p n 200.
2 (50 ) 1 p n 1000.

288


, , 15-25 2013

stdin

stdout

11 6
1 2
1 3
1 4
2 6
2 7
1 5
2 8
4 9
4 10
4 11

2
3 6

G.
. v f[v,k]
v , v k .
, .
v .
, , :
for (l = 1; l <= left_size; l++)
for (r = 1; r <= right_size; r++)
f[v, l+r] = min(f[v, l+r], f[left, l] + f[right, r])
P

(left_size[v] right_size[v]). v=1..n

, O(n2 ).
http://codeforces.ru/blog/entry/6703#comment-122804

H.
:
:
:
:

stdin
stdout
0.2
256

n ,
,

, , 15-25 2013

289

, . ,
.
. n .
,
232 .


n.


n
232 .

1 (20 )
2 (20 )
3 (20 )
4 (20 )
5 (20 )

n 10.
n 18.
n 28.
n 300.
n 2000.

stdin
3
5
7

stdout
1
3
11

H.
. . 5.

290


, , 15-25 2013

I. K
:
:
:
:

stdin
stdout
1
256

a n q k
[l . . . r] .
: n = 7, a = [6, 1, 5, 2, 4, 3, 1], l = 2, r = 4, k = 2.
[l . . . r] = [1, 5, 2]. = [1, 2, 5]. 2 =
[1, 2].


n (1 n 100 000).
a (n 1 109 ).
q (1 q 100 000).
q li ri ki
1 li ri n, 1 ki min(ri li + 1, 10)


q (ki ) . .
.


1 (50 ) n, q 100 000 li li+1 , ri ri+1 .
2 (50 ) n, q 30 000 li ri .

stdin
7
6
4
1
2
3
5

1 5 2 4 3 1
7
4
5
7

7
2
1
2


, , 15-25 2013

stdout
1 1 2 3 4 5 6
1 2
2
1 3

291

I. K
. . 2.
L++, L--, R++, R--
multiset<int>

O((n + q) n log n + qk)

J.
:
:
:
:

stdin
stdout
3
256

p n .
, (i, j ): i < j , pi > pj .


n.
.
q .
q li , ri (1 li ri n).


q .


1 (50 ) 1 n, q 105
li li+1 , ri ri+1 .
2 (50 ) 1 n, q 20 000 li ri

stdin
5
5
3
1
1
3

292

4 3 1 2

stdout
3
9
2

3
5
5


, , 15-25 2013

J.
. . 2.
, [L..R]
. L++, L--, R++, R--,
O(log n).

K.
:
:
:
:

stdin
stdout
1
512

:
1 i n t l = m, r = n, result = n + 1;
2 while (l <= r) {
3
i n t c = f(l, r);
4
i f (enough(c)){
5
result = c;
6
r = c - 1;
7
} else {
8
l = c + 1;
9
}
10 }

enough() true false. ,


. .. c < c0 false, c
c0 true. enough(x) x.
f ,
.


m n (1 m n 6 500) .


f .


1 (50 ) n 2 000.
2 (50 ) n 6 500.


, , 15-25 2013

293

stdin

stdout

1 1
1 5

1
9

enough(4).
, enough(5), enough(2).
( 4-2-?), enough(3),
enough(1).
: 4+5 = 9, 4+2+3 = 9, 4+2+1 = 7, = 9.

K.
O(n3 ).
fl,r = min (max(fl,m1 , fm+1,r ) + m)
m=l+1...r1

, fl+1,r fl,r fl,r+1 . fl,m1 m, fm+1,r


m. p: fp+1,r fl,p1 . , ,
p, , (fl,m1 + m)
[p . . . r). :
fl,r = min(fl,p1 + p, min (fm+1,r + m))
m=l+1...p1

l. r . pl,r
:
while (p - 1 > l && f[l][p - 2] > f[p][r]) p--;
O(1), ,
, l + 1 p 1, .
O(n2 ).

L.
:
:
:
:

stdin
stdout
1.5
512

.
.
294


, , 15-25 2013

, . , n
. n (
).
, ,
k . , .
, k , ,
. ,
. , , . .


n k (3 k n 1000).
n ( ) . .
.
0 2 . .
(0, 0).


k 1 n , .
. , .
, 0 .


1 (50 ) n 300
2 (50 ) n 1000


, , 15-25 2013

295

stdin

stdout

4 4
1.57
0
3.14
4.71
4 3
1.57
0
3.14
4.71

2 1 3 4

2 1 4

L.
. . 6, command-post.

M.
:
:
:
:

stdin
stdout
2
16

, . .


.
.
1 5000.



. , . ,
.


1 (50 ) 1000.
2 (50 ) 5 000.
296


, , 15-25 2013

stdin
abacabadabacaba
dbdccdbd

stdout
bccb

M.
. . 3.

N.
:
:
:
:

stdin
stdout
0.5
256

n. .

1 (25 )
2 (25 )
3 (25 )
4 (25 )

1 n 10
1 n 104
1 n 105
1 n 3 105

1 ai
1 ai
1 ai
1 ai

10
10
100
3 105


n. n .


109 + 7.

stdin
3
1 1
35
1 2
1 2
1 2
1 2

stdout
5

2
941167856
3
3
3
3

4
4
4
4

5 6 7 8 9 10
5 6 7 8 9 10
5 6 7 8 9 10
5


, , 15-25 2013

297

N.
f[i] ,
i- . prev[i] ,
: a[prev[i]] = a[i]. ,
O(n) .
1 f[0] =
2 f o r (i
3
f[i]
4 result

1
= 1; i <= n; i++)
= f[prev[i]..i-1]
= f[1..n]

O.
:
:
:
:

stdin
stdout
0.5
256

. . :
1 i1 < i2 < . . . < ik a.n 1 j1 < j2 . . . < jk b.n, t : ait = bjt ,
ait < ait+1 k .


n m 1 3 000 . n ,
. m ,
.
, 109 .


1 (50 ) n 400.
2 (50 ) n 3 000.

stdin
6 5
1 2 1 2 1 3
2 1 3 2 1
298

stdout
2


, , 15-25 2013

O.
fi,j , i a, j b.
:
1. (i, j) (i, j + 1) , j b.
2. if b[j] > a[i] : (i, j) (next(i, bj ), j + 1) , (next(i, bj ), j) .
next(i, bj ), j i , bj , a. next,
. 1 ai , bi 2n
1 2n
a.

P. k-
:
:
:
:

stdin
stdout
1
16

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


n s . n
wi , ci , ki , ,
ki , wi ci . 1 wi , ci 109 .


,
.


, , 15-25 2013

299


1 (30 ) n 100 s 104 ki = 1
2 (35 ) n 100 s 104 1 ki 109
3 (35 ) n 300 s 105 1 ki 109

stdin
2 100
2 1 100
7 100 3

stdout
339

3 . 21, 300. 39
, 78,
39. .

P. k-
, ki = 1, O(ns) O(s)
. f0 , f1 .
(wi , ci , ki ) O(s).
wi wi : 0 . . . wi 1.
r f1 [r], f1 [r+wi ], f1 [r+2wi ], . . .
f1 [r + twi ] = min (f0 [r + (t k)wi ] + kci )
j=0..k

,
O(1).

Q.
:
:
:
:

stdin
stdout
0.5
16

N K . ,
N K
.

300


, , 15-25 2013

1 (33 ) 1 N, K 500.
2 (33 ) 1 N, K 10 000.
3 (34 ) 1 N, K 50 000.


N K .


109 + 7.

stdin
10 3

stdout
4

10 : 1 + 2 + 7, 1 + 3 + 6, 1 + 4 + 5, 2 + 3 + 5.

Q.
. . 7.

R.
:
:
:
:

stdin
stdout
0.3 (0.4 Java)
256

- nn. .
.
. , .
, .


n (2 n 1000).
. .
0 106 .

, , 15-25 2013

301


, YES
. NO.


1 (50 ) n 320
2 (50 ) n 1000

stdin
2
1
2
2
1
2

3
2

stdout
YES
0 1
2 1
NO

3
4

R.
.
: . , ,
. : , , 1,
1 1, ,
.
. ,
x, :
1 Check(x)
2

3
sum_col = 0
4
f o r (i = 1; i <= n; i++)
5
sum_col += i-
6
sum_row = 0
7
f o r (j = 1; j <= n; j++)
8
sum_row += min(row[j], i * x)
9
i f sum_row < sum_col
10
return false
11
return true

, , x,
. . i- . ,
302


, , 15-25 2013

.
. y ,
x, y .
y - y 1.
O(n2 log M ). (
)
O(n2 ).


, , 15-25 2013

303

Оценить