시간 제한메모리 제한제출정답맞힌 사람정답 비율
180 초 (추가 시간 없음) 1024 MB52240.000%

문제

Supervin is teaching N classes, which are numbered from 1 to N. After giving his most recent exam, he noticed that in each of his classes, the test scores of his students form a sequence of consecutive integers. Therefore, Supervin can summarize the scores for the i-th class as two integers Li and Ri. This means that the i-th class has Ri - Li + 1 students, and for each x (Li ≤ x ≤ Ri), there is exactly one student with score x.

Supervin would like to combine the scores from the students from all of his classes and sort the scores in non-increasing order. He has Q questions (numbered from 1 to Q) about this list; for the i-th question, he wants to know what the Ki-th highest score is. (If Ki is greater than the number of students, then the answer for the i-th question is 0.)

Can you help Supervin answer all of his questions? Since there may be many answers, instead of outputting all of them, output proof that you have answered them: the sum of (Si × i) for all 1 ≤ i ≤ Q, where Si is the answer to the i-th question.

입력

The first line of the input gives the number of test cases, TT test cases follow. Each test case contains four lines. The first line contains two integers N and Q as described above. The next three lines each contain six integers in the following format, respectively:

  • X1 X2 A1 B1 C1 M1
  • Y1 Y2 A2 B2 C2 M2
  • Z1 Z2 A3 B3 C3 M3

These values are used to generate LiRi, and Ki as follows:

We define:

  • Xi = (A1 × Xi - 1 + B1 × Xi - 2 + C1) modulo M1, for i = 3 to N.
  • Yi = (A2 × Yi - 1 + B2 × Yi - 2 + C2) modulo M2, for i = 3 to N.
  • Zi = (A3 × Zi - 1 + B3 × Zi - 2 + C3) modulo M3, for i = 3 to Q.

We also define:

  • Li = min(XiYi) + 1, for i = 1 to N.
  • Ri = max(XiYi) + 1, for i = 1 to N.
  • Ki = Zi + 1, for i = 1 to Q.

출력

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the sum of (Si × i) for all 1 ≤ i ≤ Q, where Si is the answer to the i-th question.

제한

  • 1 ≤ T ≤ 100.
  • 1 ≤ N ≤ 4 × 105.
  • 0 ≤ Ai < Mi, for all i.
  • 0 ≤ Bi < Mi, for all i.
  • 0 ≤ Ci < Mi, for all i.
  • 0 ≤ X1 < M1.
  • 0 ≤ X2 < M1.
  • 0 ≤ Y1 < M2.
  • 0 ≤ Y2 < M2.
  • 0 ≤ Z1 < M3.
  • 0 ≤ Z2 < M3.
  • 1 ≤ Mi ≤ 109, for all i.

Test Set 1 (16점)

  • Q = 1.

Test Set 2 (21점)

  • 1 ≤ Q ≤ 105.

예제 입력 1

2
5 1
3 1 4 1 5 9
2 7 1 8 2 9
4 8 15 16 23 42
7 1
2 3 4 5 6 31
1 3 4 5 5 17
2 2 1 3 2 100

예제 출력 1

Case #1: 7
Case #2: 28

In Sample Case #1, the generated arrays XYZ are:

  • X = [3, 1, 3, 0, 8].
  • Y = [2, 7, 7, 2, 6].
  • Z = [4].

Therefore,

  • L = [3, 2, 4, 1, 7].
  • R = [4, 8, 8, 3, 9].
  • K = [5].

The students' scores for each of the classes are [3, 4], [2, 3, 4, 5, 6, 7, 8], [4, 5, 6, 7, 8], [1, 2, 3], and [7, 8, 9]. This means that the students' scores for all classes combined are [3, 4, 2, 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 1, 2, 3, 7, 8, 9]. If we sort them in non-increasing order, they are [9, 8, 8, 8, 7, 7, 7, 6, 6, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 1]. Therefore, the student with the 5th highest score has score 7. Thus, S = [7] and the answer is 7 × 1 = 7.

예제 입력 2

2
5 5
3 1 4 1 5 9
2 7 1 8 2 9
4 8 15 16 23 42
1 2
0 0 0 0 0 1
0 0 0 0 0 1
0 1 0 0 0 2

예제 출력 2

Case #1: 39
Case #2: 1

In Sample Case #1, every parameter is the same as Sample Case #1 except the value of Q. Therefore, the values of L and R, and the students' scores for all classes combined are still the same as Sample Case #1. However, the queries are now K = [5, 9, 40, 23, 12]. The students with the 5th, 9th, and 12th highest scores have scores of 7, 6, and 4, respectively. Since there are only 20 students, the 23rd and 40th students do not exist. Therefore, S = [7, 6, 0, 0, 4] and the answer is 7 × 1 + 6 × 2 + 0 × 3 + 0 × 4 + 4 × 5 = 7 + 12 + 20 = 39.

In Sample Case #2, the generated arrays XYZ are:

  • X = [0].
  • Y = [0].
  • Z = [0, 1].

Therefore,

  • L = [1].
  • R = [1].
  • K = [1, 2].

Therefore, there is only one student, and S = [1, 0], so the answer is 1 × 1 + 0 × 2 = 1.

채점 및 기타 정보

  • 예제는 채점하지 않는다.