Polish version    English version  
  History of OI -> IV OI 1996/1997 -> Problems


 News
 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
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
V OI 1997/1998
IV OI 1996/1997
Stage III - results
Stage II - results
Stage I - results
Problems
Regulations
Organization
Hints
Schedule
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
IV Olimpiada Informatyczna 1996/1997

Task: XOR
Author: Tomasz ¦Œmigielski
XOR gates

I stage contest  
Source fileXOR.??? (e.g. PAS,C, CPP)
Executable fileXOR.EXE
Input fileXOR.IN
Output fileXOR.OUT

A XOR gate has two inputs and one output. Its operation can be described by the following table

input 1 input 2 output
0 0 0
0 1 1
1 0 1
1 1 0

A system of XOR gates is called a XOR net if it has n inputs and one output and meets the following conditions:

  1. Each input of the net is connected to at least one input of a gate.
  2. Each input of a gate in the net is connected to one input of the net or one output of another gate.
  3. The output of exactly one gate is connected to output of the net.
  4. Each output of a gate in the net is connected to at least one input of another gate, or - to the output of the net.
  5. There exists a numbering of gates such that for every gate each of its inputs is connected to the input of the net or to the output of a gate with a smaller number.

Example

The net of 6 gates is shown in the figure. It has 5 inputs and 1 output and meets conditions 1-5 so it is a XOR net. Observe that the numbers given in the figure do not agree with the 5-th condition but there exists an appropriate numbering.

Inputs of a net are numbered from 1 to n. An input state of a XOR net can be described by an input word of the length n. Each letter of such a word is a binary digit 0 or 1. We assume that i-th digit of the word is a state of the i-th input of the net. For every input state the net returns 0 or 1 on its output. Each input word is a binary code of a natural number so we can order input words with respect to their values. We will test XOR nets by sending words from a fixed range to its input and counting the number of digits 1 returned on the output of the net.

Task

Write a program that:

  • reads the description of a XOR net from the input file XOR.IN; the descriptions consists of: the number of inputs n, the number of gates m, the number of the gate connected to the output of the net and descriptions of connections;
  • reads from the same file two binary words of length n - lower and upper limit of the range in which we will test the net;
  • computes for how many inputs from the given range the net returns 1 on its output;
  • writes the result to the text file XOR.OUT.
We assume that: 3 < n < 100, 3 < m < 3000 and that the gates of the given net are numbered from 1 to m in an arbitrary order.

Input

In the first line of the text file XOR.IN there are three integers separated by single spaces. They are respectively: the number of inputs n of the given net, the number of gates m and the number of the gate connected to the output of the net. In the following m lines there are descriptions of the connections between gates of the net. In the i-th line, for 1 <= i <= m, there is a description of the connections of two inputs of the i-th gate. Such a description consists of two integers from the range [-n,m] separated by a single space. If an input of the gate is connected to the k-th input of the net then the description of this connection is the negative integer -k, if this input is connected to the output of the j-th gate then it is described by the positive number j. In the following two lines of the text file XOR.IN there are two n-bit words a and b. They represent lower and upper limits of the range in which we test the net. We assume that in the given range there is at most 100000 words.

Output

In the text file XOR.OUT there should be written a single non-negative integer - the number of words from the range a £ s £ b (where £ is the relation consistent with the values of binary words), for which the XOR net returns 1 on its output.

Example

[figure 1]

For the file XOR.IN comprising the description of the net from the figure:

5 6 5
-1 -2
1 3
1 -2
2 -3
4 6
-4 -5
00111
01110
the correct answer is the following file XOR.OUT:
5





Print friendly version