Module: Iterate over all subpatterns of a given mask


Problem

5 /7


Eric and the LED Board

Theory Click to read/hide

It happens that it is necessary to enumerate all bit sequences of a certain length. Or in other words, iterate over all possible options, where for each object one of two possible states is selected.

In such situations, it is possible to implement enumeration using bit masks. The advantage of this approach is that such code works non-recursively and operates on numbers instead of collections or the like, which greatly improves performance.

The general code using bitmasks is given below: intn; // number of oobjects (length of bit sequence) for (int mask = 0; mask < (1 << n); mask++) { // loop through all numbers from 0 to 2^n - 1, where each number corresponds to a bitmask // the current number mask is a bitmask, where the i-th bit specifies the state of the i-th object for (int i = 0; i < n; i++) { // iterate over n bits to understand what state each object has if ((1 << i) & mask) { // check if the i-th bit is equal to one // process the option that the i-th object has the state '1' } else { // case when the i-th bit is zero // process the option that the i-th object of the state '0' } } }
This code runs in O(2^n * f(n)), where f(n) is the time it takes you to process one particular iterable.

Problem

Eric found an LED board in his grandfather's old garage. However, he was surprised that when activated, the diodes were not synchronized with each other. That is, some of them burned, and some did not.
The board itself turned out to be unusual. It is a rectangular grid with n rows and m columns, where each cell contains one diode. Near each row there is a lever that switches all the diodes in this row (burning diodes go out and vice versa). Each column has the same levers (which I use the diodes in the corresponding column).
Eric wondered if it was possible to switch the diodes to the same state by switching levers.

Input:
The first line contains two natural numbers n and m (1 <= n, m <= 7) - the number of rows and columns on the board, respectively.
Then there are n lines with m numbers each - the states of the diodes, where 0 means that the diode is off, and 1 that it is on.

Output:
Print "YES" if it is possible to bring the diodes into one state and "NO" if it is impossible.

Examples:
 
Input Output
2 2
0 1
10
YES
2 2
0 1
0 0
NO

Explanation:
In the first example, you can switch all diodes in the first row, then switch all diodes in the first column. Then all diodes will be off.