Oleksiy PylypenkoOlympiad Years

All-Ukrainian Olympiad in Informatics — Finals

XVI edition, Donetsk, 6–12 April 2003 · April 2003

10th place · 275 points · Diploma IIRank 10275 ptsDiploma II

UPML won the team competition

The finals of the All-Ukrainian Olympiad in Informatics were the last stop of the school season, and my last shot at them as an 11th-grader at UPML. The 134 finalists — the winners of every regional stage in the country, across grades 9 through 11 — sat two five-hour rounds, three problems each, in the computer classrooms of Donetsk National University's campus on 8 and 10 April 2003.

Round one (ВТО, Вишня, Алхімія) went in fits: 50 points on the triangular tiling problem ВТО, 15 on the reaction-scheduling problem Алхімія, and 0 on Вишня — the cherry-tree problem, whose own model solution the jury discovered was wrong mid-competition and had to patch, retesting it only partially. It was a rough round for the whole field, not just me. Round two turned the result around completely: full 100/100 scores on both Ланцюг (Chain) and Казино (Casino), plus 10 points on Система рівнянь, for 275 points overall.

That was good for 10th place among the 11th-graders — a Diploma II, my second in a row at this level, dated 11 April 2003. UPML's team also topped the province-by-province standings that year with the highest average score of any school, closing out my run through the Ukrainian school olympiad system on the highest note it ever reached.

#ContestantGradePoints
1Borys Katargin11385
2Yevhen Palamarchuk11370
3Oleksandr Halkin11340
10Oleksiy Pylypenko11275
Grade-11 ranking excerpt from the jury's final protocol (Додаток №2 до протоколу №4, 11.04.03) — top 3 of the 11th-grade field plus the 10th-place finish, out of 134 finalists total across grades 9–11.Archived contest overview (uoi.kiev.ua, via Wayback Machine)

Problems

Problems from this contest

Alchemy

Алхімія

graphssimulationshortest-path

Statement

There are K kinds of substances and N kinds of alchemical reactions. Each reaction consumes a fixed set of input substances and produces a fixed set of output substances, taking a fixed amount of time to complete. Any substance obtained from a reaction can be extracted in pure form and used freely — there's always enough of any substance for any use, and a substance produced by one reaction can immediately feed another. Reactions may run simultaneously.

Write a program ALCHEMY that, given the substances and reactions, finds the minimum time needed to obtain a target substance.

Input / Output

The first line of input file ALCHEMY.DAT contains four integers: K (3 ≤ K ≤ 250) — the number of substances, N (3 ≤ N ≤ 500) — the number of reactions, M (1 ≤ M < K) — the number of substances available at the start, and the number of the target substance. N blocks follow, each describing one reaction in three lines: the time the reaction takes, then the count and list of input substances, then the count and list of output substances. Substances available from the start are numbered 1..M; all others are numbered M+1..K. The sum of all reaction times does not exceed 2·10⁹.

The single line of output file ALCHEMY.SOL must contain the minimum time in which the target substance can be obtained, or −1 if it cannot be obtained at all.

Constraints

  • 3 ≤ K ≤ 250, 3 ≤ N ≤ 500, 1 ≤ M < K
  • sum of all reaction times ≤ 2·10⁹

Example

Input (ALCHEMY.DAT):
4 3 1 4
8
1 1
1 4
3
1 1
2 2 3
2
2 1 3
1 4

Output (ALCHEMY.SOL):
5

Substance 4 is obtained by running reaction 2 for the first 3 time units, then reaction 3 for 2 more — 5 units of time in total.

Solution idea

This is a discrete-event simulation over an AND/OR hypergraph rather than a plain shortest-path problem, since a reaction only fires once all of its inputs are ready. At each step, every reaction whose inputs are already available gets scheduled, tentatively giving each of its outputs a completion time; the simulation then jumps forward to the earliest such tentative time, "locks in" every substance that becomes ready at that instant, and repeats. This continues until the target substance locks in, no further substance becomes ready, or every reaction has fired — which finds the true minimum completion time because substances are only ever finalized in non-decreasing order of readiness, exactly like Dijkstra's algorithm processing nodes in order of shortest distance.

Casino

Казино

bfsgraphssimulation

Statement

A gaming die sits in the top-left corner of a rectangular N×M board, in a fixed starting orientation: face 1 points forward and face 2 points left. Board cells are squares matching the die's face size. The die can roll onto an adjacent cell — up, down, left, or right — by tipping over one of its edges, but it can never leave the board; some cells are blocked and may never be visited.

Write a program CASINO that finds a path for the die from the top-left corner to the bottom-right corner such that the value on the die's top face, once it arrives, is as large as possible. The die may revisit any cell any number of times.

Input / Output

The first line of input file CASINO.DAT contains two integers N and M (2 ≤ N, M ≤ 50) — the board's height and width. N further lines of M numbers each (0 or 1) describe the board; a 1 marks a cell the die may never visit, a 0 marks a cell it may pass through. The starting cell is always 0.

The first line of output file CASINO.SOL must contain W, the length of the found path, followed by W lines each giving the row and column of the board cell visited at that step. If no path exists, the output file must contain a single line with -1.

Constraints

  • 2 ≤ N, M ≤ 50
  • board cells are 0 (passable) or 1 (blocked); the start cell is always 0

Example

Input (CASINO.DAT):
3 2
0 1
0 0
0 0

Output (CASINO.SOL):
3
2 1
2 2
3 2
Solution idea

The die's full state is its board position plus its orientation, and a die has exactly 24 distinct orientations, so the search space is only N × M × 24 states — small enough for a plain breadth-first search over a graph where each move both shifts position and rotates the die according to fixed roll rules. The search explores states in BFS order and, every time it reaches the bottom-right corner, remembers the best (highest top-face) arrival seen so far, stopping early only once it reaches that corner with the maximum possible face value; since BFS discovers each state at its shortest distance and every state is visited at most once, this finds an optimal path without needing to search the full state space exhaustively.

Chain

Ланцюг

greedysorting

Statement

There are N pieces of chain, the i-th made of Lᵢ links. Any link can be unbent and later bent shut again, joining separate pieces together.

Write a program CHAIN that, given the number of pieces N and their link counts Lᵢ, finds the minimum number of links that must be unbent and rebent to join all the pieces into a single chain. The final chain must have no branches — every link must connect to exactly two others, except the two end links, which connect to only one.

Input / Output

The first line of input file CHAIN.DAT contains an integer N (2 ≤ N ≤ 10 000). The second line contains N integers Lᵢ (1 ≤ Lᵢ ≤ 1 000 000 000).

The single line of output file CHAIN.SOL must contain the minimum number of links that need to be unbent and rebent to join all the pieces into one chain.

Constraints

  • 2 ≤ N ≤ 10 000
  • 1 ≤ Lᵢ ≤ 1 000 000 000

Example

Input (CHAIN.DAT):
3
100 3 4

Output (CHAIN.SOL):
2
Solution idea

This is the classic "minimum links to cut" puzzle: opening every link of a piece turns each of its links into a free connector, capable of joining two other pieces together at once. Sort the pieces by length and greedily sacrifice the smallest ones first, entirely, as long as the connectors they supply are still needed to stitch the remaining pieces (including themselves) into one strand; once the cumulative supply of connectors catches up with the number of pieces left to join, every further piece can be joined using only its own two end links. The count of pieces fully sacrificed this way gives the answer directly.

Cherry Tree

Вишня

treesdpgreedy

Statement

A small but proud mouse has decided to eat every berry on a cherry tree. The tree's branches fork and never grow back together; from the point where a branch ends, either more branches begin, or a certain number of berries grow. Crawling one meter along a branch costs the mouse one unit of its reserve of nutrients (RN); eating a berry restores one unit of RN. If the reserve ever goes negative, the mouse dies. Movement always starts and ends at the base of branch 1, the trunk.

Write a program CHERRY that, given the tree, finds the minimum starting reserve of RN the mouse needs to eat every berry and return to the ground, never letting its reserve go negative along the way.

Input / Output

The first line of input file CHERRY.DAT contains an integer N (1 ≤ N ≤ 100) — the number of branches. N further lines describe the tree, one per branch: the (i+1)-th line gives branch i's length L (1 ≤ L ≤ 30 000), then the number K of branches that continue from its far end, then K branch numbers; if K is zero, a third number gives the count of berries V (0 ≤ V ≤ 30 000) growing at that branch's end instead.

The single line of output file CHERRY.SOL must contain the minimum RN reserve needed.

Constraints

  • 1 ≤ N ≤ 100
  • 1 ≤ L ≤ 30 000, 0 ≤ V ≤ 30 000

Example

Input (CHERRY.DAT):
8
5 3 6 5 7
5 0 10
9 0 1
4 0 19
11 0 50
5 2 2 4
3 2 8 3
4 0 0

Output (CHERRY.SOL):
15
Solution idea

For each subtree, dynamic programming computes two numbers: the minimum reserve needed to sweep that subtree clean of berries and return to its base, and the leftover reserve on return once that minimum is spent. A subtree's own numbers combine from its children's, but the order in which sibling subtrees are visited changes the peak reserve needed partway through, so the children at each branch point are sorted by an exchange-argument comparator (a subtree that "pays for itself" early is visited first) before folding them together. Historically, this was the one problem where the jury's own model solution had a bug discovered only during the round — it had to be patched and retested only partially, which likely explains how many contestants, this run included, scored zero here.

System of Equations

Система рівнянь

graphsgreedyhavel-hakimi

Statement

Consider a system of N equations, where the k-th equation has the form X + Y = bₖ, with X = x₁ₖ + x₂ₖ + … + x_{k-1,k} and Y = x_{k,k+1} + … + x_{k,N}. Each equation's left-hand side therefore has N−1 terms, and every unknown xᵢⱼ appears in exactly two equations of the system.

Write a program SYSTEM that, given b₁, …, b_N, finds one solution of the system, under the condition that every unknown xᵢⱼ may only take the value 0 or 1.

Input / Output

The first line of input file SYSTEM.DAT contains the number of test blocks. Each block starts with a line containing N (3 ≤ N ≤ 50) — the number of equations — followed by a line with N integers bᵢ (0 ≤ bᵢ ≤ 50).

For each test block, output file SYSTEM.SOL must contain one solution of the system: N−1 lines, the k-th containing the N−k found values of x_{k,k+1} … x_{k,N}. If a test block's system has no solution, a single line containing -1 must be written instead.

Constraints

  • 3 ≤ N ≤ 50
  • 0 ≤ bᵢ ≤ 50

Example

Input (SYSTEM.DAT):
2
3
1 2 3
5
3 2 3 2 2

Output (SYSTEM.SOL):
-1
1 1 1 0
0 0 1
1 1
0

The first test block (N=3, b = 1 2 3) has no valid solution. The second (N=5, b = 3 2 3 2 2) is solved by the given assignment of the xᵢⱼ.

Solution idea

Since each unknown xᵢⱼ ∈ {0,1} appears in exactly two equations, the problem is equivalent to building a simple graph on N vertices where vertex k must end up with degree exactly bₖ, and each edge {i,j} corresponds to setting xᵢⱼ = 1. That is the classical degree-sequence realization problem, solved here with a Havel–Hakimi–style greedy: repeatedly take the vertex with the largest remaining required degree and connect it to the next-largest remaining vertices (re-sorting the degree list, kept in a heap, after every step), failing only if some vertex runs out of distinct partners to connect to before its required degree is satisfied.

The Triangular Territory

ВТО

number-theorymathgeometry

Statement

The territory of the Great Triangular Oblast (ВТО) is shaped like a right triangle whose legs measure M and N state length units. The government wants to cover as much of the territory as possible with square 1×1 tiles, packed tightly against each other and against the two legs, with no cutting — a tile counts only if it fits entirely inside the triangle, never crossing the hypotenuse. Tiles are delivered only in full containers of P tiles each, and the government orders exactly as many containers as the project needs.

Write a program TRIANGLE that finds how many tiles are left over in the last container once the paving is done.

Input / Output

The single line of input file TRIANGLE.DAT contains three integers: M, N (2 ≤ M, N ≤ 2 000 000 000) and P (100 ≤ P ≤ 10 000).

The single line of output file TRIANGLE.SOL must contain one integer — the number of unused tiles left over from the last container.

Constraints

  • 2 ≤ M, N ≤ 2 000 000 000
  • 100 ≤ P ≤ 10 000

Example

Input (TRIANGLE.DAT):
4 3 100

Output (TRIANGLE.SOL):
97

Three tiles fit under the hypotenuse of the 4×3 right triangle, so one container of 100 is ordered and 97 tiles come back unused.

Solution idea

The number of whole unit tiles that fit under the hypotenuse of an M×N right triangle is the classic lattice-point-counting sum ∑ floor(i·N/M) for i = 1..M−1 — but with M, N up to two billion, that sum can't be computed term by term. The author's solution reduces it with a Euclidean-style recursion on gcd(M, N), splitting off the shared factor and folding the coprime remainder through a closed-form formula, with every intermediate product carried modulo 2P (to keep a division by two exact without overflowing) before finally reducing modulo P. The last step is just P minus that remainder: the count of tiles missing from the final container.

Evidence

Scans & photographs

Sources

  1. [1]Archived contest overview (uoi.kiev.ua, via Wayback Machine)
  2. [2]Jury protocols, offline archive (ftp.pmg17.vn.ua)