#P2461. [SDOI2008] 递归数列

    ID: 1467 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2008各省省选递归山东矩阵乘法

[SDOI2008] 递归数列

Description

A sequence of natural numbers is defined as follows:

For iki \le k: ai=bia_{i}= b_{i}.

For i>ki > k: ai=j=1kcj×aija_{i}= \sum_{j=1}^{k}{c_{j} \times a_{i-j}}.

Here b1kb_{1\dots k} and c1kc_{1\dots k} are given natural numbers.

Write a program that, given natural numbers mnm \le n, computes (i=mnai)modp\left( \sum_{i=m}^{n}{a_{i}} \right) \bmod p.

Input Format

The first line contains a natural number kk.

The second line contains kk natural numbers b1,b2,,bkb_{1}, b_{2}, \dots, b_{k}.

The third line contains kk natural numbers c1,c2,,ckc_{1}, c_{2}, \dots, c_{k}.

The fourth line contains three natural numbers m,n,pm, n, p.

Output Format

Output one positive integer on a single line, representing the value of i=mnaimodp\sum_{i=m}^{n}{a_{i}} \mod p.

2
1 1
1 1
2 10 1000003

142

Hint

For 20%20\% of the testdata, n106n \le 10^{6}.

For another 30%30\% of the testdata, k=1k=1.

For 100%100\% of the testdata, 1k151 \le k \le 15, 1mn10181 \le m \le n \le 10^{18}, 0bi,ci1090 \le b_{i}, c_{i} \le 10^{9}, p108p \le 10^{8}.

Translated by ChatGPT 5