luogu#P5141. 列队
列队
Background
This problem is the strengthened version. For the weakened version, please refer to .
Okay, just to scare you a bit.
Problem Description
Some time ago, -xiao- joined the military training for Grade 10 at . As everyone knows, during military training you need to stand in a square formation.
In the team where -xiao- is, there were originally people (weak ones and top ones), but now only a few top ones including -xiao- and some weak ones are left.
Top one : Instructor, I still haven't finished debugging the last problem of this year's . I'll go back first and solve it.
Instructor: OK, leave approved. If you finish debugging in ten minutes, go back and rest first.
Weak one : Instructor, I haven't finished the hard problems in today's task plan. I want to go back and do them.
Instructor: Even if you go back now you still can't debug it. Stand properly. Stop trying to slack off.
-xiao- is a boy who loves learning. Now he finds that there are only two columns left on the playground. Originally, both columns had length , but now they are incomplete. The weak ones stand in the first column, and the top ones stand in the second column. Also, if there is a top one in a row, their presence will make the weak ones in the adjacent position not dare to stand there.
Even so, the remaining top ones are still stronger than the weak ones (obviously).
In , the fighting value of one column is defined as
where is the row index starting from , and indicates whether there is a person in this row.
Now -xiao- already knows the current standing situation of the top-one column. He wants to ask you: how many possible standing ways can the weak ones have?
However, -xiao- thinks this is too easy. He now has queries. Each query gives you a weak-ones fighting value range and a , meaning he wants to know: among the weak-ones standing ways whose fighting value is within , what is the fighting value of the -th largest one. If the total number of standing ways is less than , then output .
Input Format
The first line contains two integers , meaning the queue has rows, and -xiao- has queries.
The second line is a string, representing the standing way of the top ones.
The next lines each contain three numbers , representing the queries of -xiao-.
Output Format
Output lines, each being the answer. -xiao- is very kind and allows you to output the fighting value modulo .
5 5
0 1 0 1 0
0 4 5
0 3 4
0 0 1
0 1 2
4 4 1
POOR AFO!
POOR AFO!
0
0
4
10 5
1 1 0 1 1 0 0 1 0 0
0 56 7
30 126 7
62 116 5
20 100 1
8 108 1
POOR AFO!
POOR AFO!
POOR AFO!
100
100
5 1
0 0 0 0 1
0 999 1
15
Hint
For of the data, .
For of the data, .
The time limit is very loose, so feel free to enjoy it.
Translated by ChatGPT 5