Join The Future
Time limit5sMemory limit64 MB
Given parity constraints on subarray sums and a lower and upper bound for each element, count the arrays that satisfy them and print the lexicographically smallest one.
- Level
Hard8 of 10
- Topics
- Union-find, Greedy, Combinatorics
- Solved
- No attempts yet
Problem
Professor Zhang has an array of integers. He writes down some observations about the array on paper. Each observation is described by three integers , , and , which means that the sum of elements modulo 2 on the interval of the array is equal to .
After that, he tries to recover the array using only these observations. Many arrays may satisfy them, so Professor Zhang limits the lower and upper bound of each integer in the array.
Given the observations, the lower bounds, and the upper bounds, find the number of possible arrays and the lexicographically smallest array.
Input
There are multiple test cases. The first line of input contains an integer , the number of test cases. For each test case:
The first line contains two integers and (, ): the length of the array and the number of observations.
Each of the next lines contains two integers and (): the lower and upper bound of the -th integer.
Each of the next lines contains three integers , , and (, ), denoting the -th observation.
There are at most test cases, and the total input size is at most 30 kibibytes.
Output
For each test case, output the number of possible arrays on the first line, modulo . On the second line, output the lexicographically smallest array. If no array is possible, output "-1" on the second line.