#P5487. 【模板】Berlekamp–Massey 算法
【模板】Berlekamp–Massey 算法
Background
Prerequisite skills: linear recurrence and the algorithm.
Also, please pay attention to optimizing your memory usage. The shortest recurrence is guaranteed to be unique.
The problem setter apologizes for forcing two tasks into one, but this is also a chance to learn the method for linear recurrences. It is guaranteed to pass under the -O2 option.
Problem Description
You are given the first terms of a sequence starting from index .
Find the shortest linear recurrence of sequence modulo , and output modulo .
Input Format
The first line contains two integers , meaning that the first terms of sequence will be given, and you need to compute .
The second line contains integers, which are .
Output Format
On the first line, output the shortest linear recurrence.
On the second line, output the value of .
4 10
1 1 2 3
1 1
89
5 10
3 7 27 95 339
3 2
691707
Hint
For of the testdata, , , and the length of the recurrence is guaranteed to be at most .
Translated by ChatGPT 5