#P2461. [SDOI2008] 递归数列

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

[SDOI2008] 递归数列

Problem Description

A sequence of natural numbers is defined as follows:

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

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

Here b1…kb_{1\dots k} and c1…kc_{1\dots k} are given natural numbers.

Write a program that, given natural numbers m≤nm \le n, computes (∑i=mnai) mod p\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=mnaimod  p\sum_{i=m}^{n}{a_{i}} \mod p.

2
1 1
1 1
2 10 1000003

142

Hint

For 20%20\% of the testdata, n≤106n \le 10^{6}.

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

For 100%100\% of the testdata, 1≤k≤151 \le k \le 15, 1≤m≤n≤10181 \le m \le n \le 10^{18}, 0≤bi,ci≤1090 \le b_{i}, c_{i} \le 10^{9}, p≤108p \le 10^{8}.

Translated by ChatGPT 5