luogu#P4723. 【模板】常系数齐次线性递推

【模板】常系数齐次线性递推

Problem Description

Find the nn-th term of a kk-th order homogeneous linear recurrence sequence {ai}\{a_i\}, that is:

an=i=1kfi×ania_n=\sum\limits_{i=1}^{k}f_i \times a_{n-i}

Input Format

The first line contains two integers nn and kk, as described in the statement.

The second line contains kk integers, representing f1 f2  fkf_1 \ f_2 \ \cdots \ f_k.

The third line contains kk integers, representing a0 a1  ak1a_0 \ a_1 \ \cdots \ a_{k-1}.

Output Format

Output one integer, the value of anmod998244353a_n \bmod 998244353.

6 4
3 -1 0 4
-2 3 1 5
73

Hint

Constraints: N=109N = 10^{9}, K=32000K = 32000.

It is guaranteed that all input numbers are integers in the range [109,109][-10^9,10^9].

Translated by ChatGPT 5