Polyn0mial's blog

By Polyn0mial, 3 years ago, In English

I was solving this problem.

My idea is to map bits into linear equations.

Try all possible number of bits set to 1 in the final array (0~30 in this problem).

Take sample input 1 as an example.

Let's say we want $$$(S = 2)$$$ bits set to 1, here's the linear equations:

$$$(1 - A) + (1 - B) + (1 - C) = 2$$$

$$$A + (1 - B) + C = 2$$$

Do some simplifications,

$$$(-1) * A + (-1) * B + (-1) * C = -1$$$

$$$1 * A + (-1) * B + 1 * C = 1$$$

One possible solution for $$$(A, B, C)$$$ is $$$(0, 0, 1)$$$ = $$$1(001)_2$$$.

However gaussian elimination doesn't ensure the solution having integer solution (and also =0 or =1 in this problem).

When there're infinite solutions, is there a way to check if there exist a solution $$$(x_0, x_1, \dots, x_{30}), x_i = 0$$$ or $$$x_i = 1$$$?

My submission
  • Vote: I like it
  • +8
  • Vote: I do not like it