#P15093. [UOI 2025 II Stage] Odd Rows

    ID: 17009 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP线段树2025Special Judge构造链表UOI(乌克兰)

[UOI 2025 II Stage] Odd Rows

题目描述

Once, s1mple approached Kostya, a well-known problem solver, and said:

$\textit{If you want to become better, you need constant practice. Here is a problem for training:}$

Given a matrix aa of size n×mn \times m (nn --- number of rows; mm --- number of columns), where each element is either 00 or 11. It is known that each column contains exactly cic_i ones. The elements within each column can be arranged freely. The goal is to maximize the number of rows with an odd number of ones and to find such a matrix.

Kostya silently nodded, sat down at the table, and began to work, knowing that every practice brings him closer to mastery.

Kostya couldn't solve the problem and is asking you to help him solve it.

$\textbf{You can earn partial points if you only find the number, not the matrix. More details below.}$

输入格式

The first line contains two integers nn and mm (1nm106)(1 \le n \cdot m \le 10^6) --- the dimensions of the matrix.

The second line contains mm integers c1,c2,,cmc_1, c_2, \dots, c_m (0cin)(0 \le c_i \le n) --- the number of ones in each column.

输出格式

In the first line, output a single integer tt (0tn0 \leq t \leq n) --- the number of rows in the matrix with an odd sum.

In each of the next nn lines, output mm integers aija_{ij} (0aij10 \leq a_{ij} \leq 1) --- the numbers of the matrix.

8 4
6 1 6 1
6
1 1 1 0 
1 0 1 1 
1 0 1 0 
1 0 1 0 
1 0 0 0 
1 0 0 0 
0 0 1 0 
0 0 1 0 
4 4
3 0 3 0
2
1 0 1 0 
1 0 1 0 
1 0 0 0 
0 0 1 0 
7 3
4 3 2
7
1 1 1 
1 0 0 
1 0 0 
1 0 0 
0 1 0 
0 1 0 
0 0 1 

提示

In the first example, the first and third columns intersect in at least 44 positions, meaning that if there were only these two columns, we would have 44 even rows, but since there are also two columns with 11 one, we can convert two of them to odd, so the optimal answer will be 66 odd rows.

In the second example, we can ignore the second and fourth columns because they have no ones, meaning they will not change the parity of any row, and the first and third intersect in at least 22 rows, meaning at least two rows will be even, so the answer is 22.

In the third example, the answer is 77, because there exists a matrix that has 77 odd rows and satisfies the condition, and there is no matrix that has more than 77.

Scoring

  • (1010 points): n,m5n, m \le 5;
  • (88 points): the number of ones in the matrix does not exceed nn;
  • (2020 points): the number of ones in each column does not exceed n/2n/2;
  • (1414 points): n,m50n, m \le 50;
  • (1414 points): n3000n \le 3\,000;
  • (1414 points): nm3105n \cdot m \le 3\cdot10^5;
  • (2020 points): no additional restrictions.

You can earn half the points for each block if you output the correct tt for each test in the block.

Note that to earn partial points, you need to output the correct tt and

  • either output nothing more; that is, output only tt, but not the matrix;
  • or output the complete matrix consisting of 00s and 11s, which does not have to be correct. For example, one that consists entirely of zeros.

If you output, for example, only a few rows, or too many rows, numbers other than 00 and 11, etc., you will receive 00 points.