Polish version    English version  
  History of OI -> X OI 2002/2003

 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
Stage III - results
Stage II - results
Stage I - results
Stage II
For contestants
Helpful resources
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
X Olympiad in Informatics 2002/2003

Problem: Mastermind II
Author: Zbigniew Czech

We shall consider sequences that meet the following conditions:

  • the length of the sequence is c,
  • the elements of the sequence are digits from the range of 1-9,
  • the elements are not repeated in the sequence.
A single sequence will be called a code.

When two codes are given we estimate their compatibility giving two numbers. The first one (column A in the example) is the sum of those digits that occur in both codes and appear on the same positions in both sequences, whilst the second number (column B) is the sum of those digits that occur in both codes but appear on different positions.

We are given c codes and their compatibility estimations with some unknown code. One should find and present the unknown code. Sample data and a result for c = 3 are presented below.

40 497
010 674
05 941
unknown code  416


Write a program which:
  • reads a description of codes and estimations of their compatibility,
  • finds a code meeting the conditions of the task,
  • writes the result.


Your program should read the description from the standard input. In the first line there is one integer c, 1 <= c <= 9. In the following c lines there are the descriptions of the given codes of digits and the estimations of their compatibility with some unknown code, one per line. In each of the lines there are c+2 non-negative integers separated by single spaces. The first and the second number are the estimation of compatibility of the given code with the unknown one, and the last c numbers are different digits from the range of 1-9 forming the given code.


Your program should write to the standard output. There should be c different digits from the range of 1-9 forming the code to be found, separated by single spaces.

You may assume that for the test data there is at least one solution. If for the input data there exist many satisfying codes, your program should write out only one of them (arbitrary).


For the following input data:
4 0 4 9 7
0 10 6 7 4
0 5 9 4 1
a correct answer is in the following output:
4 1 6

Print friendly version