Recurrence and Period
Time limit0.5sMemory limit512 MB
Given a prime p and the recurrence x_n = a*x_{n-1} + b*x_{n-2} mod p, find the lexicographically smallest (S, T) where the sequence becomes periodic with period T from index S.
- Level
Hard8 of 10
- Topics
- Number theory, Matrix, Math
- Solved
- No attempts yet
Problem
Given a prime , the recurrence (), and the initial terms and , the pigeonhole principle guarantees the existence of integers and that satisfy the following condition.
Condition: for every integer , is a multiple of .
Find the pair that satisfies the condition and is smallest in lexicographic order.
Input
The first line contains integers . It is guaranteed that is prime.
Output
Print the pair that satisfies the condition and is smallest in lexicographic order on the first line.