Oleksiy PylypenkoOlympiad Years

NEERC Pilot Junior Contest — Transcaucasian Group

St. Petersburg · Barnaul · Tbilisi, 24 November 2002 · November 2002

1st placeRank 1First Place

also won the open individual Transcaucasia championship (Diploma I)

Team

  • Oleksiy Pylypenko
  • Bogdan Yakovenko
  • Sergiy Zhylyayev
  • Nikolay Voytsekhovskyycoach

Until this point every stage in this history had been a school or national olympiad. NEERC — the ACM ICPC's Northeastern European Regional Contest — was a different kind of institution: a genuine qualifying round of the world's university-level programming championship. In the autumn of 2002 the region piloted a junior version of it for school teams, and UPML's team — Oleksiy Pylypenko, Bogdan Yakovenko and Sergiy Zhylyayev, coached by Nikolay Voytsekhovskyy — was among those admitted, competing under the same rules and the same nine problems (A through I) as the university teams.

The pilot ran simultaneously in three cities — St. Petersburg, Barnaul, and Tbilisi — on 24 November 2002, ICPC rules throughout, with the certificate for it signed by ACM contest director William B. Poucher. The team's own group was the Transcaucasian one, centered on Tbilisi. Out of that group the UPML squad, representing the Kyiv Phys-Math Lyceum, placed first.

The individual championship

The same trip carried a second result. Alongside the team contest, Tbilisi hosted an open individual championship of Transcaucasia in programming, and Oleksiy — then an 11th-grade UPML student — won it, receiving a Diploma I degree from the Georgian State Department of Informatization, chaired by David Tarkhan-Mourari. Two results, one trip: a first-place team certificate and an individual diploma, both from the same November weekend.

#TeamSolvedPenalty
1Moscow SU 191078
2Nizhny Novgorod SU 281515
3Saratov SU 37937
4SPb SU 171139
5Moscow SU 26645
Top 5 of the 2002-2003 NEERC standings (problems A-I, the same set used by the Transcaucasian junior group). The offline archive that survives does not include a separate scoreboard for the junior/subregional group itself — see the certificate evidence for the team's result there.NEERC archive index, 2002-2003 season

Problems

Problems from this contest

Amusing Numbers

mathbig-numbersconstructive

Statement

Consider the integers from 1 to N, ordered lexicographically — the way a dictionary would order them as strings — rather than numerically. For N = 11 the order is 1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9. Let Q(N, K) be the 1-based position of the number K in this ordering; for N = 11, Q(11, 2) = 4.

Given K and M, find the smallest N such that Q(N, K) = M.

Input / Output

Input: two integers K and M (1 ≤ K, M ≤ 10⁹) separated by a space.

Output: the smallest N with Q(N, K) = M, or 0 if no such N exists.

Constraints

  • 1 ≤ K, M ≤ 10⁹
  • The answer N can be far larger than 10⁹ (up to roughly 18 digits), so it does not fit in a 32-bit or even a naive 64-bit running total without care.

Example

Input: 2 4
Output: 11

Input: 2 1
Output: 0

Input: 100000001 1000000000
Output: 100000000888888879

Input: 1000000000 11
Output: 0
Solution idea

Fix the number of digits i that N will have and count, for that many digits, how many numbers of length ≤ i sort lexicographically before K — call that count C. As i grows this prefix-count only increases, so the right i is the first one where the running count reaches M. Within that digit length, the lexicographic rank of K is determined by "how many i-digit strings are less than K's own padded value," which reduces to a digit-DP style walk over K's decimal digits.

Concretely: for each digit length i, build the largest i-digit number that is still lexicographically before K (this is K's decimal representation truncated/adjusted to i digits, with its last kept digit decremented), and add that count to a running total. Once the running total reaches or passes M at digit length i, the answer's own decimal digits can be reconstructed directly from M - C by walking back through the same counting logic and appending digit i on top of K. Because N can have up to ~19 digits, all of this arithmetic (add, subtract, compare) is done on custom big integers represented as digit arrays rather than machine integers.

Bricks

geometryconstructive

Statement

A prisoner wants to dispose of rectangular A × B × C bricks through a D × E rectangular hole in the floor — a bottomless well of the same cross-section. Given the five measurements, decide whether a brick can be pushed through the hole. A brick can be tilted freely in three dimensions before being lowered through.

Bricks illustration

Input / Output

Input: a single line with five numbers A, B, C, D, E, each with at most one digit after the decimal point.

Output: YES if some face/orientation of the brick fits through the hole, NO otherwise.

Constraints

  • 1 ≤ A, B, C, D, E ≤ 10 (inches), at most 1 decimal digit each.

Example

Input: 1.0 2.0 1.5 1.4 1.0
Output: NO

Input: 1.0 2.0 1.5 1.5 1.0
Output: YES
Solution idea

A brick has three distinct pairs of face dimensions: (A,B), (A,C), (B,C). For a rectangular cross-section D × E to let a rectangle of side lengths a × b through (tilting is allowed, not just axis-aligned pushes), the classic test is: the rectangle fits straight if a ≤ D and b ≤ E (in either assignment), and otherwise it may still fit tilted at some angle if a diagonal placement clears both hole dimensions. The jury solution checks all 6 combinations of (face pair, hole-dimension assignment) with an exact "fits(a, b, d, e)" tilted-rectangle-through-rectangle test, expressed entirely in scaled integer arithmetic (multiplying every length by 10 to avoid floating point) to keep the geometric inequality (derived from setting up the rectangle's rotated bounding box and solving for a valid tilt angle) exact. If any of the 6 checks succeeds, the answer is YES.

Cricket Field

geometrybrute-forceconstructive

Statement

A rectangular park of width W and height H, axis-aligned, contains N trees at integer coordinates. No tree may be cut, and none may end up strictly inside the field (they may sit on its boundary). Find the largest axis-aligned square cricket field that fits inside the park without enclosing a tree, and report its southwest corner and side length.

Sample cricket field plan

Input / Output

Input: first line has N, W, H (0 ≤ N ≤ 100, 1 ≤ W, H ≤ 10000); the next N lines give tree coordinates (Xi, Yi), 0 ≤ Xi ≤ W, 0 ≤ Yi ≤ H, all distinct.

Output: P, Q, L — the field's southwest corner and side length. Any optimal placement is accepted.

Constraints

  • 0 ≤ N ≤ 100
  • 1 ≤ W, H ≤ 10000
  • 0 ≤ Xi ≤ W, 0 ≤ Yi ≤ H

Example

Input:
7 10 7
3 2
4 2
7 0
7 3
4 5
2 4
1 7

Output: 4 3 4
Solution idea

The optimal square's southwest corner can always be pinned to either the park's own southwest corner (x = 0 or y = 0) or to the x- or y-coordinate of some tree — an unconstrained corner could always be nudged toward the nearest such coordinate without shrinking the square. This gives at most (N+1) candidate x-values and (N+1) candidate y-values, so it suffices to try every pair (p, q) drawn from {0} ∪ {tree x-coordinates} × {0} ∪ {tree y-coordinates} as a candidate corner (O(N²) pairs). For each candidate corner, the largest square that avoids all trees is bounded above by the distance to the park's far edges (min(W-p, H-q)) and, for every tree strictly northeast of the corner, by max(treeX - p, treeY - q) (the square must stop before enclosing that tree). Taking the minimum of all these bounds gives the largest valid square at that corner; the best over all O(N²) corners is the answer. Overall O(N³), comfortably fast for N ≤ 100.

Decoding Task

stringsbitwiseconstructive

Statement

A stream cipher XORs each message byte with the matching byte of a repeating key: E[i] = K[i] XOR C[i]. You are given two encoded messages that are XOR-encodings of the same underlying key stream applied to the same clear-text, except the second clear-text has one extra space character (byte value 32) inserted at the very start — so it is one byte longer and every later byte is shifted by one position relative to the first message. Recover the first N+1 bytes of the key.

Encoding algorithm illustration

Input / Output

Input: two lines, each a hexadecimal byte string (two hex chars per byte). The first line is N bytes long (2N hex characters), the second is N+1 bytes long (2N+2 hex characters).

Output: a single hexadecimal line with the recovered N+1 key bytes.

Constraints

  • 1 ≤ N ≤ 10000

Example

Input:
05262C5269143F314C2A69651A264B
610728413B63072C52222169720B425E

Output: 41434D2049435043204E454552432732
Solution idea

Because the second message encodes a space followed by exactly the same clear-text as the first message, its first byte's clear-text is known: a space (0x20). That immediately gives the first key byte: K[1] = E2[1] XOR 0x20. From there, message 2's byte i+1 decodes to the same clear-text character as message 1's byte i (since message 2 is message 1 shifted right by one position). So once K[i] is known, decode C[i] = E1[i] XOR K[i], and since C[i] is also the clear-text at position i+1 of message 2, the next key byte follows as K[i+1] = E2[i+1] XOR C[i]. A single left-to-right pass reconstructs all N+1 key bytes in O(N).

Evacuation Plan

graphsshortest-pathflowoptimization

Statement

A city has N municipal buildings (each with a number of workers) and M fallout shelters (each with a capacity). The travel time between a building and a shelter is the Manhattan distance between their grid coordinates plus one minute. An evacuation plan assigns each building's workers across shelters, respecting every shelter's capacity and evacuating everyone. Given a proposed plan, decide whether it minimizes the total travel time summed over all workers; if not, output any valid plan with strictly smaller total time.

Sample city plan picture

Input / Output

Input: N and M (1 ≤ N, M ≤ 100); N lines of building data (Xi, Yi, Bi); M lines of shelter data (Pj, Qj, Cj); then N lines giving the proposed plan Ei,j (workers sent from building i to shelter j). The plan is guaranteed valid (matches worker counts and respects capacities).

Output: OPTIMAL if the plan cannot be improved, or SUBOPTIMAL followed by an improved valid plan in the same N-line format.

Constraints

  • 1 ≤ N, M ≤ 100
  • -1000 ≤ Xi, Yi, Pj, Qj ≤ 1000
  • 1 ≤ Bi, Cj ≤ 1000

Example

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

Output:
SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1
Solution idea

This is a transportation-optimality check, decided via a negative-cycle argument on a graph over shelters. Define a "transfer cost" from shelter j to shelter k as the cheapest possible change in total time from moving one worker's assignment from j to k: pick, among all buildings currently sending workers to j, the one for which dist(building, k) - dist(building, j) is smallest. If that cheapest transfer is negative and shelter j has spare assigned workers while shelter k has spare capacity, the plan is improvable by that single move. More generally, run an all-pairs shortest-path relaxation (Floyd–Warshall) over these pairwise transfer costs to find chains of transfers (j → l → k) whose combined cost is negative and whose endpoints are feasible (a valid start and end of a transfer chain, including loops back to the same shelter to catch 3+ cycles) — any such negative chain proves the plan suboptimal and is applied by walking the recorded path and moving one worker at each hop. If no negative transfer or transfer-chain exists, the plan is already optimal (this is exactly the transportation problem's optimality condition: no improving cycle in the reduced-cost graph).

Folding

dpstringsinterval-dp

Statement

A "folded sequence" is defined recursively: a single letter is folded; a concatenation of two folded sequences is folded; and if S is a folded sequence, X(S) (X an integer > 1) is folded and unfolds to S repeated X times. Given a string of uppercase letters, find the shortest folded sequence (by character count of its written-out form, e.g. 9(A)3(AB)CCD) that unfolds back to it.

Input / Output

Input: a single line of 1 to 100 uppercase letters.

Output: a single line — the shortest folded representation. Any optimal one is accepted if several tie.

Constraints

  • 1 ≤ length ≤ 100

Example

Input: AAAAAAAAAABABABCCD
Output: 9(A)3(AB)CCD

Input: NEERCYESYESYESNEERCYESYESYES
Output: 2(NEERC3(YES))
Solution idea

Classic interval DP. Let cost[i][j] be the minimum folded length of the substring from position i to j. Base case: single characters cost 1. For a longer interval, two kinds of transitions are tried: (1) split the interval at some point k into two folded pieces and add their costs (cost[i][k] + cost[k+1][j]); (2) repeat — if the interval's length is divisible by some period lr, and the substring is literally lr-periodic across the whole interval, fold it as x(period) where x is the repeat count, costing 3 + cost[i][i+lr-1] (for the digits, parens) plus one extra character for each extra decimal digit needed to write x (10 ≤ x needs 2 digits, 100 ≤ x needs 3). Take the minimum over both transition types for each interval, filling the DP table by increasing interval length (O(n²) intervals, O(n) work each for the split, O(n) periods to test for the repeat check — O(n³) overall for n ≤ 100). The actual string is reconstructed by remembering, for each interval, which transition achieved the minimum, and recursively expanding from the top interval.

Ghost Busters

geometry3dbrute-force

Statement

A proton gun sits at the origin and can fire a straight ray anywhere into the octant X, Y, Z ≥ 0. N ghosts are spherical, given by center and radius; a ray kills every ghost it touches (even tangentially). Find the maximum number of ghosts a single ray can kill, and which ones.

Sample ghosts view through ectoplasmic scope

Input / Output

Input: N (0 ≤ N ≤ 100) on the first line, then N lines of Xi, Yi, Zi, Ri (1 ≤ Xi, Yi, Zi ≤ 10000, 1 ≤ Ri ≤ min(Xi, Yi, Zi)).

Output: the maximum count on the first line, then the indices of the killed ghosts (any optimal set) on the second line.

Constraints

  • 0 ≤ N ≤ 100
  • 1 ≤ Ri ≤ min(Xi, Yi, Zi) (so the sphere never contains the origin)

Example

Input:
2
1200 1200 3900 300
160 160 820 60

Output:
2
1 2
Solution idea

Each sphere, seen from the origin, subtends a circular cone of directions that would hit it; a ray "kills" a ghost exactly when its direction lies inside (or on) that ghost's cone. The optimal ray direction, if it is interior to several cones simultaneously, can always be perturbed to lie on the boundary of at least one of those cones without losing any of the others — and generically it can be pushed to the boundary of two cones at once (their intersection curve). So it suffices to test only two families of candidate directions: the exact direction to the center of every ghost (N candidates), and, for every pair of ghosts, the (up to two) directions lying on the intersection of their two viewing cones — derived from the geometry of two cones sharing an apex, by decomposing the candidate direction into a component along the plane spanned by the two ghost-center directions and a normal component (O(N²) pairs, O(1) trig work each). For every candidate direction, count how many spheres the ray actually intersects by comparing the perpendicular distance from each sphere's center to the ray against its radius. Overall O(N²), which comfortably handles N ≤ 100.

Heroes Of Might And Magic

dpsimulationgame

Statement

A hero with HP_H hit points and MP_H mana stands at square 0 of a 1-D battlefield of N+1 squares; a monster pack of N_M monsters with HP_M hit points each starts at square N and, on its turn, always advances min(V, current square - 1) squares toward the hero, striking for K damage (K = monsters still alive) if it reaches square 1. On the hero's turn (spending 1 mana, required), he casts one of: Lightning Bolt (removes L_P hit points from the pack, where P is the pack's current square), Teleport (moves the pack to any square 1..N of the hero's choosing), or Heal (restores dH hit points to the hero, capped at HP_H). The hero loses if his HP drops to 0 or below, or if he runs out of mana while monsters remain. Determine whether the hero can force a win, and if so, output a winning sequence of spells.

Input / Output

Input: first line N, HP_H, MP_H, HP_M, N_M, V, dH (bounds below); second line: N integers L_1..L_N (Lightning Bolt damage by square).

Output: VICTORIOUS followed by one spell per line (L, H, or T <square>) leading to victory, or DEFEATED if no strategy wins.

Constraints

  • 1 ≤ N ≤ 10, 2 ≤ HP_H ≤ 100, 1 ≤ MP_H ≤ 50, 1 ≤ HP_M ≤ 10, 1 ≤ N_M ≤ 10, 1 ≤ V ≤ N, 1 ≤ dH < HP_H, 1 ≤ L_P ≤ 10

Example

Input:
2 3 2 1 2 1 1
1 1

Output:
VICTORIOUS
L
L
Solution idea

State space is small enough for exact DP/memoized recursion: (mana remaining, pack's current total HP, pack's current square) — at most MP_H × (HP_M × N_M) × N states, each ≤ 50 × 100 × 10 ≈ 50000. Define GetHPNeed(mana, packHP, packSquare) as the minimum hero HP required, right now, to still guarantee eventual victory from this state. It tries all three spells recursively: Lightning Bolt reduces packHP by L at the current square (immediate win if that kills the pack); Heal is only useful if the hero can survive next turn's monster hit regardless; Teleport is only ever useful sending the pack beyond V+1 squares away (closer choices are dominated). Each branch accounts for the HP lost if the pack ends its following move on square 1. Memoizing on the three-tuple avoids recomputation, and comparing the memoized minimum HP need against the hero's actual starting HP_H answers VICTORIOUS/DEFEATED; replaying the recorded best spell at each step from the initial state reconstructs the winning move sequence.

Inlay Cutters

geometrygridsimulation

Statement

A rectangular M × N granite plate is cut by K straight cuts, each either horizontal, vertical, or diagonal at 45°, each starting and ending on the plate's border. After all cuts are made, count how many of the resulting pieces are 45° right-triangles (of any size).

Sample city plan picture

Input / Output

Input: first line M, N, K (1 ≤ M, N ≤ 50, 0 ≤ K ≤ 296); K lines of Xi,1, Yi,1, Xi,2, Yi,2 — the endpoints of each cut, all on the plate border, all cuts distinct.

Output: a single integer, the number of triangular pieces produced.

Constraints

  • 1 ≤ M, N ≤ 50
  • 0 ≤ K ≤ 296
  • Every cut is horizontal, vertical, or at exactly 45°, and goes fully across the plate between two border points.

Example

Input:
4 4 4
1 4 1 0
0 4 4 0
0 0 4 4
0 2 4 2

Output: 6
Solution idea

A 45°-right-triangle piece always has its right-angle vertex at a point where one horizontal-or-vertical cut crosses one diagonal cut (or the plate border, treated as extra horizontal/vertical cut segments), with its two legs running along the horizontal/vertical direction and its hypotenuse along a diagonal. The jury solution doubles all coordinates (so diagonal/axis intersections stay integral), adds the four border edges as extra cut segments, and computes every pairwise intersection point between perpendicular-family cuts (horizontal×vertical, horizontal×diagonal, vertical×diagonal, diagonal×diagonal), marking each intersection on a grid. Then, for every horizontal/vertical crossing point, it walks outward in the four axis directions to the nearest other marked intersection to get four candidate leg lengths, and for every diagonal/diagonal crossing point it walks outward along the two diagonal directions similarly — for each of the (up to 4) leg-length combinations at a vertex, it checks whether the implied third side (the hypotenuse or the matching perpendicular leg) is actually an uninterrupted cut segment between those two points before counting it as a real triangular piece. Summing valid triangles over all crossing points gives the answer.

Sum

mathwarm-up

Statement

This was the contest's practice-session warm-up problem, not one of the scored A–I problems. Find the sum of all integers lying between 1 and N inclusive.

Input / Output

Input: a single integer N, |N| ≤ 10000.

Output: a single integer — the sum of all integers between 1 and N inclusive.

Constraints

  • |N| ≤ 10000
  • N may be zero or negative, in which case "between 1 and N inclusive" is read as the inclusive range running from N up to 1.

Example

Input: 2
Output: 3

Input: -3
Output: -5
Solution idea

No jury solution for this practice problem survives in the archive, so the idea here is derived directly from the sample behavior. Reading N = -3 giving -5 confirms the range is meant inclusively in either direction: it is the set of integers from min(1, N) to max(1, N), e.g. for N = -3 that is {-3, -2, -1, 0, 1}, which sums to -5. The general closed form avoids any loop: let a = min(1, N) and b = max(1, N); the sum of the inclusive integer range [a, b] is the arithmetic series total (a + b) * (b - a + 1) / 2. For N = 2: a=1, b=2, sum = (1+2)*2/2 = 3. For N = -3: a=-3, b=1, sum = (-3+1)*5/2 = -5 — matching both samples. O(1) time, no loop needed even though the constraint (|N| ≤ 10000) would have made a direct loop fast enough too.

Evidence

Scans & photographs

Sources

  1. [1]NEERC archive index, 2002-2003 season
  2. [2]Standings, jury solutions and sample tests (offline archive mirror)