#L2667. NOI2013」矩阵游戏

NOI2013」矩阵游戏

「NOI2013」矩阵游戏

传统题
10001000 ms
256256 MiB

题目描述

婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的 nnmm 列的矩阵(你不用担心她如何存储)。她生成的这个矩阵满足一个神奇的性质:若用 F[i][j]F[i][j] 来表示矩阵中第 ii 行第 jj 列的元素,则 F[i][j]F[i][j] 满足下面的递推式:

$$\begin{cases} F[1][1] = 1 \\ F[i][j] = a \times F[i][j-1] + b & j \neq 1 \\ F[i][1] = c \times F[i-1][m] + d & i \neq 1 \end{cases} $$

递推式中 abcda,b,c,d 都是给定的常数。

现在婷婷想知道 F[n][m]F[n][m] 的值是多少,请你帮助她。由于最终结果可能很大,你只需要输出 F[n][m]F[n][m] 除以 109+710^9+7 的余数。

输入格式

一行有六个整数 nmabcdn,m,a,b,c,d。意义如题所述。

输出格式

包含一个整数,表示 F[n][m]F[n][m] 除以 109+710^9+7 的余数。

样例

样例输入 1

3 4 1 3 2 6

样例输出 1

85

样例解释 1

样例中的矩阵为:

$$\begin{pmatrix} 1 & 4 & 7 & 10 \\ 26 & 29 & 32 & 35 \\ 76 & 79 & 82 & 85 \end{pmatrix} $$

样例输入输出 2

见附加文件中的 matrix.inmatrix.ans

数据范围与提示

测试点编号 数据范围
11 1n,m101 \le n,m \le 101a,b,c,d10001 \le a,b,c,d \le 1000
22 1n,m1001 \le n,m \le 1001a,b,c,d10001 \le a,b,c,d \le 1000
33 1n,m1031 \le n,m \le 10^31a,b,c,d1091 \le a,b,c,d \le 10^9
44 -
55 1n,m1091 \le n,m \le 10^91a=c1091 \le a = c \le 10^91b=d1091 \le b = d \le 10^9
66 1n,m1091 \le n,m \le 10^9a=c=1a = c = 11b,d1091 \le b,d \le 10^9
77 1n,m,a,b,c,d1091 \le n,m,a,b,c,d \le 10^9
88 -
99
1010
1111 1n,m1010001 \le n,m \le 10^{1000}a=c=1a = c = 11b,d1091 \le b,d \le 10^9
1212 1n,m1010001 \le n,m \le 10^{1000}1a=c1091 \le a = c \le 10^91b=d1091 \le b = d \le 10^9
1313 1n,m1010001 \le n,m \le 10^{1000}1a,b,c,d1091 \le a,b,c,d \le 10^9
1414 -
1515 1n,m10200001 \le n,m \le 10^{20000}1a,b,c,d1091 \le a,b,c,d \le 10^9
1616 -
1717 1n,m101,000,0001 \le n,m \le 10^{1,000,000}a=c=1a = c = 11b,d1091 \le b,d \le 10^9
1818 1n,m101,000,0001 \le n,m \le 10^{1,000,000}1a=c1091 \le a = c \le 10^91b=d1091 \le b = d \le 10^9
1919 1n,m101,000,0001 \le n,m \le 10^{1,000,000}1a,b,c,d1091 \le a,b,c,d \le 10^9
2020 -