QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#13620. 矩形

Statistics

本题中约定 $0^0 = 1$。

Snuke 使用动态规划解决了一道题目。具体来说,她设计了如下递推式: $$ F(i, j) = \begin{cases} 0, & \text{if $i = 0$;} \\ f_i, & \text{if $i > 0$ and $j = 0$;} \\ aF(i - 1, j) + bF(i, j - 1) + c, & \text{if $i > 0$ and $j > 0$.} \end{cases} $$ 其中 $i, j$ 是非负整数,$a, b, c$ 是给定的常数,$f_i$ 是给定的数列。

Snuke 需要求出 $F(n, m)$,所以她对于 $1 \le i \le n, 1 \le j \le m$ 计算了 $F(i, j)$,这些值形成了一个 $n \times m$ 的矩阵。

Takahashi 希望你告诉她这个矩阵。为了避免过多的输出,你只需要输出这个矩阵的哈希值。

具体来说,给定整数 $h$ 和质数 $p$,你需要输出 $$ \left( \sum_{i = 1}^{n} \sum_{j = 1}^{m} F(i, j) \, h^{(i - 1)m + (j - 1)} \right) \bmod p $$ 的值。

输入格式

从标准输入读入数据。

第一行输入一个整数 $T$,表示子任务编号。

第二行输入四个整数 $n, m, h, p$。

第三行输入三个整数 $a, b, c$。

第四行输入 $n$ 个整数 $f_1, \ldots, f_n$。

输出格式

输出到标准输出。

输出一行,包含一个整数,表示矩阵的哈希值。

样例一

input

1
2 2 2 998244353
2 1 1
0 1

output

93

explanation

题目描述中提到的矩阵是 $\left( \begin{matrix} 1 & 2 \\ 4 & 9 \end{matrix} \right)$,其哈希值为 $$ 1 \times 2^0 + 2 \times 2^1 + 4 \times 2^2 + 9 \times 2^3 = 93. $$

样例二

input

2
9 100000 5 998244353
5 4 7
6 5 6 9 3 7 4 5 2

output

623270548

样例三

input

3
9 1000000000 5 235497281
5 0 7
6 5 6 9 3 7 4 5 2

output

211538270

限制及约定

对于所有数据,保证:

  • $1 \le n \le 10^6$
  • $1 \le m \le 10^9$
  • $10^8 \le p \le 10^9$,$p$ 是质数
  • $0 \le h, a, b, c, f_i < p$
子任务编号$n \le$$m \le$特殊性质分值依赖的子任务
1$1000$$1000$$p = 998244353$5
2$10^5$$10^5$241
3$10^6$$10^9$$b = 0$10
4$c = 0$28
5$f_i = 0$30
631, 2, 3, 4, 5
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.