Logo
(追記) (追記ここまで)

23698번 - Mysterious Triple Sequence 다국어

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

문제

Jeffery found an amazing sequence of triples $\{(a_k, b_k, c_k)\}_{k = 0}^{\infty}$:

  1. $(a_0, b_0, c_0) = (2, 1, 0)$.
  2. For each non-negative integer $k,ドル $(a_{k + 1}, b_{k + 1}, c_{k + 1}) = (a_k^2 + b_k^2, a_k b_k + b_k c_k, b_k^2 + c_k^2)$.

For example, $(a_1, b_1, c_1) = (5, 2, 1)$ and $(a_2, b_2, c_2) = (29, 12, 5)$.

In modulo some integer $p,ドル some triples would never appear in this sequence, some triples would appear periodically and other triples would appear only once.

Jeffery is wondering if you could help him find out the first appearance of some triples starting from given positions. Could you help him, please?

입력

The first line contains two integers $n$ and $p$ (1ドル \leq n \leq 5000,ドル 1ドル \leq p \leq 2^{30}$) where $n$ indicates the number of questions and $p$ indicates all the following questions are in modulo $p$.

Each of the next $n$ lines contains four integers $x,ドル $y,ドル $z$ and $m$ (0ドル \leq x, y, z < p,ドル 0ドル \leq m \leq 10^{18}$) indicating a question that queries you to find the minimum integer $k$ such that $k \geq m$ and $(a_k, b_k, c_k) \equiv (x, y, z) \pmod{p}$.

출력

For each question, output in one line a single integer, indicating the answer to the question. If there is no such integer $k,ドル output $-1$ instead.

제한

예제 입력 1

5 11
6 1 4 10
4 10 6 3
7 1 5 0
2 1 0 1
2 1 0 0

예제 출력 1

11
4
2
-1
0

예제 입력 2

5 10
5 8 9 5
0 2 6 0
9 2 5 6
5 5 5 7
5 2 1 2

예제 출력 2

5
-1
6
-1
-1

노트

In the first sample, $(a_0, b_0, c_0) \equiv (2, 1, 0),ドル $(a_1, b_1, c_1) \equiv (5, 2, 1),ドル $(a_2, b_2, c_2) \equiv (7, 1, 5),ドル $(a_{2 T + 3}, b_{2 T + 3}, c_{2 T + 3}) \equiv (6, 1, 4),ドル $(a_{2 T + 4}, b_{2 T + 4}, c_{2 T + 4}) \equiv (4, 10, 6)$ $\pmod{11}$ where $T = 0, 1, 2, \ldots$

In the second sample, $(a_0, b_0, c_0) \equiv (2, 1, 0),ドル $(a_1, b_1, c_1) \equiv (5, 2, 1),ドル $(a_{2 T + 2}, b_{2 T + 2}, c_{2 T + 2}) \equiv (9, 2, 5),ドル $(a_{2 T + 3}, b_{2 T + 3}, c_{2 T + 3}) \equiv (5, 8, 9)$ $\pmod{10}$ where $T = 0, 1, 2, \ldots$

출처

Contest > Open Cup > 2018/2019 Season > Stage 15: Grand Prix of China G번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /