Olympiad in Mathematics - Category Programming

Problem Set of the Second Round

1995/96



Task P-II-1

A triangular network consists of equilateral triangles with unit-length side. Let us modify the coordinate system a little bit: the x-coordinate is oriented the same way as we are used from the cartesian coordinates, but the angle between the x- and y-axes is 60 degrees. For instance, the verteces of the equilateral triangle based in the origin have the coordinates (0, 0), (1, 0), (0, 1). Now assume there is a polygon whose verteces 1) lie in the verteces of this network, 2) are mutually different, and 3) every two neighboring verteces of this polygon have distance of unit-length. The polygon can be described in the program input as follows: the first line contains the number of verteces N, the following N lines contain the coordinates of the polygon verteces in the order along the circumference of the polygon. Your task is to write a program that can compute the area of a polygon in unit triangles.
Note: Try to minimize the space and time complexity of the algorithm. You can assume that the input is correct.


Task P-II-2

N is a prime, for instance 991. An array P with N integer elements (indexed from 0) contains M different positive integers (M < N). Unoccupied elements of the array are initialized with zeros. The elements are stored in the array in such a way that it is possible to use the function search to determine the location of any element X (the function returns the location of X in the array P, if X is present in the array, otherwise it returns value -1):

function search(X:integer):integer;
var i:integer;
begin
  i := (X div 10 + X mod 10) mod N;
  while P[i] > X do
    i := (i + 7) mod N;
  if P[i] = X then search := i
  else vyhladaj := -1;
end;

int search(int X) 
{
  int i;
  i = (X / 10 + X % 10) % N;
  while (P[i] > X) i = (i + 7) % N;
  if (P[i] == X) return(i);
  else return(-1);
}
Task:

  a)   Describe how the elements should be stored in the array P, so that one could use the function search to search for any of them.
  b)   Write the most efficient procedure insert, which can be used to insert an element X to the array P so that the function search can still be used for searching (assume that before inserting of the element X, the array P had this property and did not contain the element X).


Task P-II-3

Consider a plotting device, which can draw various pictures. However, their descriptions must follow a special format - presented as a character string with the following restrictions:

The plotting device will interpret this sequence as follows: The drawing pen keeps its drawing direction, which is towards north before processing the string, and during drawing, according to the parameters, either updates its direction, or moves (draws a line) in its current direction forward or backwards (if the distance is negative). For instance, the string '[4[100 90]]' draws a square with sides of length 100. Note that the numeric parameters are space-separated.

Some older plotting devices have unsynchronized control circuits. Thus it happens from time to time that instead of drawing a line of length P they draw a line of length P + 1. For this reason, the pen often stops at a location, which is different from the final location that could be exepcted from the input sequence. Your task is to construct and prove an algorithm, which for a given input sequence (a character string) determines the maximum distance of the place where the pen terminates from its expected final location.


Task P-II-4

Construct a D0L system (or prove that it is not possible), with the following grow fucntions:

  a)   f(n) = nn
  b)   f(n) = n22n

D0L systems

An alphabet is an arbitrary finite non-empty set. The elements of this set are symbols. The finite sequence of symbols of some alphabet is a word. We will use for the symbols the early letters of the English alphabet (a, b, c, ...) , while for the words we will use the late letters (u, v, w, ...) and the capital letters (B, G, D, ...). The word length is the number of symbols it contains, and it is written as |w|. Chaining of two words v = a1a2...an and u = b1b2...bm produces a word u*v = a1a2...anb1b2...bm. The set of all words, which can be formed from the symbols of alphabet B is written as B*. This set contains also an empty word, i.e. the word of a zero-length represented by @. A rule over the alphabet B is an ordered pair (a, v), where a is from B and v is from B*. The rule is written in a form a -> v. A deterministic Lindermayer system without interactions (D0L system) is an ordered tripple (B, P, w), where

This D0L system produces a sequence of words w1;w2;w3..., which starts with the axiom and follows with words that are obtained from the neighboring preceding words by simulatenously replacing their symbols by words according to the rules. Therefore
  1. w1 = w
  2. if wi = a1a2...an, then wi+1 = u1 * u2 * ... * un, where aj -> uj is in P for j = 1, 2, ... n.
Notice that for each symbol there is exactly one rule and thus exactly one word, which will replace it. The sequence that is produced by the D0L system is therefore deterministic. The grow function of a D0L system is such a function f: N -> N0 (N are all natural numbers, N0 are all natural numbers and zero), which takes the index of the word in the D0L-generated sequence and returns its length, in other words: f(i) = |wi|.

Example

Note: When constructing a D0L system, we prefer systems that contain the least number of rules.