Some Competition Programming Problems as the Beginning of Artificial Intelligence

We consider in this paper some programming competition problems (which are near to some problems of ACM competitions) of the following subjects: we can make their solution using both Prolog and a classical procedure-oriented language. Moreover, the considered problems are selected that their solution in Prolog and in a classical procedure-oriented language are similar - i.e., in other words, they use the same working mechanism, the same approach to constructing recursive functions etc. However, the main goal of this paper is to demonstrate, that the solution of most of these problems by the students is a step to design the real problems of artificial intellegence.


Introduction
We can say, simplifying the matters, that we consider in this paper some interesting competition problems and their solutions; in this sentence, this paper can be considered as a continuation of (Melnikov and Melnikova, 2006).But, unlike that paper, all the problems considered below are connected by the following subject: we can construct their programs both in classical procedure-oriented languages and in Prolog.
However, it is the real simplifying the matters; it is that the main goal of this paper is not the solving problems, even in Prolog.Authors consider this paper (like (Melnikov and Melnikova, 2006)) as a paper of the set of ones, connected with solving similar problems; and the goal of this set of the papers is not to demonstrate some methods of solving competition problems, but to show, that this solving can give the real "transition" to design the real programs.A possible variant of this transition is the considered here "transition into artificial intelligence", i.e., the considered solving can be considered as a step to the real problems of the artificial intelligence.
However, like (Melnikov and Melnikova, 2006), we shall give consideration of the description of the solution of our problems using both classical procedure-oriented languages and Prolog.And we shall try to show not only how we solved the problem, but also why we solved it in such way1 , i.e., how we can guess the need solution.
Much of our problems were considered in the ACM competitions ((2005/2006 ACM International Collegiate Programming Contest) etc.), or, more often, are the subproblems of the problems considered there.And it is important to remark, that the considered problems are selected such as their solutions in Prolog and in a classical procedure-oriented language are almost the same -i.e., in other words, they use the same working mechanism, the same approach to constructing recursive functions etc.
Let us also remark in advance, that we shall not consider the classical problems of the graph theory (like constructing spanning tree, vertex cover etc.), which also coul be called by competition problems and are connected with the subjects considered below.There are two reasons of such thing.First, there are the limitations for the size of our paper.And second, there exist very good descriptions of such problems in a lot of books of applying Prolog for solving problems of artificial intelligence, see, e.g., (Bratko, 2001).

The "classical" Problems of the Representation a Number by a Sum
At first, we consider some classical problems, where the representation of a natural number as a sum of some other numbers is considered.
Problem 1.For the given numbers n and k, we need to compute r(n, k), which is the number of its representations as a sum of exactly k positive integer items; we do not consider the permutations of items by new representations.For instance, and we have no other variants, therefore, therefore r(7, 3) = 4.
Solution of Problem 1 in Pascal.Solving this problem, we need to use the following standard technique (we use it almost always considering the representation of a number as a sum, see (Aho et al., 1979) etc.): we add to the set of arguments of the computed function not only the arguments given in the problem situation, but also maximal possible item2 .We shall not describe this method in details: it was many times described in a lot of books, and, besides, it can be explained following to the next text of the program.Thus, the solution can be found by the function function R(N,K:integer): integer; begin R:=RR(N,K,N); end; where the last argument of the function RR is the above-mentioned maximal possible item.Here, function RR can be realized (e.g., by the internal function for function R) in the following way: Solution of Problem 1 in Prolog.As we said before, we can have "almost the same program" in Pascal and Prolog; i.e., we simple rewrite function RR into Prolog, for the serviceability changing the loop for the recursive call in the following way: r(N,K,Answer) :-Max is N-K+1, r(N,K,Max,Answer).r(N,_,_,0) :-N<0, !. r(N,K,_,0) :-N<K, !. r(_,_,0,0) :-!. r(0,0,_,1) : Probably, we do not need comments.Let us only remark, that comparing to Pascal version, we only add the special computation of the value r if both its main arguments are equal to 0, i.e., r(0,0,_,1).
Thus, we have almost the same program in a procedure-oriented language (Pascal) and in Prolog.But the main thing is that the same situation will be considered in the problem of Section 4, which is much more complicated.For two following problems, we shall consider only the statements: the mechanism of their solution is almost the same as one considered for Problem 1.
Problem 2. For the given number n, we need to compute s(n), which is the number of its representations as a sum of some positive integer items; we do not consider the permutations of items by new representations.For instance, and we have no other variants, therefore s(4) = 5 (we also consider 4 as a sum of the only item).
Problem 3.For the given number n, we need to compute q(n), which is the number of its representations as a sum of some different positive integer items; we do not consider the permutations of items by new representations.For instance, and we have no other variants, therefore q(7) = 5 (we also consider 7 as a sum of the only item).
Thus, we shall not consider the solutions of problems 2 and 3.
We can often reduct a competition problem to one of the considered three problems, including ones, where such reduction is not evident.For instance, let us consider the following problem.
Problem 4. Let us call a 2k-digit number as a good number, if the sum of its first k digits is equal to its sum of the last k digits.For the given number k, we shall to calculate the number of 2k-digit good numbers.
Solution of Problem 4. Let us consider only the algorithm; we will explain, that we can simply realize it both in Pascal and in Prolog, using the programs of Problem 1.
It is evident, that the trivial solution (i.e., the loop, in which we consider all the 2kdigit numbers) is very bad.And we can obtain a good solution using the simply guess, that the variants of the digits in the first and the second parts of the number are independent.Therefore, knowing the numbers of variants of each of such possible sum (from 0 to 9k; this computation is made like Problem 1), we obtain the answer as the sum of squares of all the variants of such sums.
Let us remark, that we have to make some little changes for the program of Problem 1, for the possibility of applying 0 as minimum possible item; however, this change is very simple, and we shall not describe such new program detailed.Thus, using the same notation r(n, k) for the possibility n = 0, we obtain the following answer, which can be programed by a simple loop: Vice versa, the following problem seems to be like to the problems of representation a number as a sum, but its simple solution needs other methods.
Problem 5. We have to store information about all the possible representations of a given set as combining some subsets; and we have to know only the number of elements in each subset.For instance, we have the following representations for the set of 4 elements: (compare Problem 2).We have to store information about the subsets only by one of two the following possibilities: -either by the matrix of N columns, where each string is the description of the subsets in decreasing order of the numbers of their elements; for instance, for 4 elements we obtain the following string for the representation 3 + 1: -or by the array of the lists, where each element of this array is a list containing the next representation; for instance, considered representation 3+1 is represented here as the list containing 2 units; and each unit needs 3 bytes (where 1 byte is the information, and 2 other bytes are the reference to the next unit).There is given a number of elements of the set, which is no more than 256.Which of two possibilities of storing is better?
Solution of Problem 5.The recurrent formulation, which gives the number of representations of N elements using such numbers for little subsets, is very complicated.
Therefore for the counting, we shall use the standard method which was already used before: our recursive function will use some additional arguments. 3Let us describe such new arguments for our solution; we shall reference to the program given below.
First, it is variable Max, i.e., our artificially added limitation for the maximum number of elements in each set. 4 Second, it is variable Deep, i.e., the deep of nesting of the recursive calls.The returned value of function Max is the number of representations, and the common number of units (we need the deep of nesting for its counting) will be counted in global variable Answer, which has to receive value 0 before the main call of function Rec.Thus, function Rec is as follows: As usually, it is better to define described recursive function Rec inside the following procedure, which returns the number of representations and the number of units: procedure Razbivka (N: byte; var Razb,Answer: word); begin Answer:=0; Razb:=Rec(N,N,0); end; We shall not compare need values, i.e., Razb(N)*N and Answer(N)*3: it is simple.
The considered function has the following defect, which is clear for big values N: while working, we call it many times using the same set of arguments. 5We can improve this little defect by the next standard method: to put calculated values into special mini-DB.

An Arithmical Expression of the Given Sequence of Digits
The problem considered in this section has some analogues with ones considered in Section 2.
Problem 7.There is given a 5-digit number.We can insert signs (statements) +, -, *, /, ˆ(the last one means degree) and !(means factorial) between its digits.We can also use round brackets and the sign -(unary minus) before the number.
We shall consider the obtained text as the arithmetic expression and calculate its value; for instance, for the given number 12345, we can obtain expressions -12+34*5=158, 12/3+4/5=1028 etc.We allow the division only if its result is integer.
We have to obtain the text of the following type: 1 = (the concrete arithmetical expression made of the given number, which value is equal to 1) 2 = (the concrete arithmetical expression made of the given number, which value is equal to 2) 3 = (the concrete arithmetical expression made of the given number, which value is equal to 3) ... The number of the answer have to be without a break, and, in other words, the "grade" for the problem is equal to the maximum obtained number.Consider the following example (for the given number 12345): Here, we have the grade 3.
The forming of the list of numbers (and also the transposition of the list of digits into the corresponding number) is made by two the following functions: We do not consider the text of other auxiliary functions.Let us only remark, that Deep is the deep of recursive calls (this variable is used for the possibility of the processing Problem 8. We put the N white balls in a line and now we want to paint some of them in black, so that at least 2 black balls could be found among any M successive balls.We need exactly C[i] milliliters of dye to paint the i-th ball.The task is to find the minimum amount of dye we need to paint the balls.
Solution of Problem 8. Is it a complicated problem?But using all the methods we consider in this paper, we obtain very short solution!And the "final culmination" of our paper is as follows: we really use all the methods considered before.Namely: -we use a procedure-oriented language "in Prolog style"; -we ignore the algorithm which consider all possibilities, and use the procedure approach (which is demonstrated, at first, using special working with argument nMade in the program given below); -for the recursive calls, we use the additional argument nSum; -the current optimal solution is stored in the global variable Sum; in Prolog, it is changed for an additional argument; -we control the deep of recursive calls by the other additional argument nDeep.As before, we can obtain "the same program" both in Pascal and Prolog.(The realisation of the last method is not difficult, but the corresponding comments are very long.We hope to consider this method in a next paper using some different examples, therefore we do not consider here the program in Prolog.)Thus, we shall consider only the program in Pascal.Its global variables are the following ones: The arguments of this function were already briefly commented.The internal variable bFalse defines whether the following ball will be painted.nMinus is the number of the balls for decreasing the number of painted ones using the recursive call; strangely enough, this value is calculated by much complicated sub-algorithm.

Conclusion
We did not consider in this paper some important questions, which are connected with intellectual programming, rather, we have considered them very briefly.At first, it is declarative and procedure approaches to the solution of the problems in Prolog; however, such subject (i.e., both these approaches in competition problems) needs a lot of problems to be considered, and we hope to realize this thing in a nearest future.
It is important to remark, that we do not discuss "the questions of 80's": is it better Prolog than Pascal?than Lisp?Our approach is that all these languages are important for the students.
Thus, authors consider this paper as a paper of the set of ones, connected with solving similar problems.However, as we said before, the goal of this set of the papers is not to demonstrate some methods of solving competition problems.The main goal is to show, that this solving can give the real "transition" to design the real programs.A possible variant of this transition is the considered before "transition into artificial intelligence".And another possible "transition" (seems to be much more important), which will be considered in a next paper, is the "transition" into the real discrete optimization problems (see (Hromkovič, 2004), and also some papers of the authors, (Melnikov, 2005;Melnikov et al., 2006); see also the references, which are in those papers, at first in (Melnikov et al., 2006)).
Authors have the following plans for some next papers.First, it would be a paper about using the classical algorithms and data structures in Olympiad problems, i.e., algorithms and data structures "of Wirth", "of Cormen, Leiserson and Rivest" etc.Second, it would be a paper about some classical hard problems and their simplification as competition problems, and also about some methods of their solution.And the subject of the third paper would be formulated as "Olympiads as a step to PhD".