luogu#P5175. 数列

数列

Background

Solution: https://blog.csdn.net/kkkksc03/article/details/85008130

Problem Description

Given a sequence ana_n, where a1a_1 and a2a_2 are known.

The sequence ana_n satisfies the recurrence $a_n = x \times a_{n-1} + y \times a_{n-2} \ (n \ge 3).$

Compute i=1nai2\sum_{i=1}^n a_i^2.

Since the answer may be very large, output it modulo 109+710^9+7.

Input Format

The first line contains an integer TT, the number of test cases.

The next TT lines each contain 55 integers n,a1,a2,x,yn, a_1, a_2, x, y, with the meanings as described above.

Output Format

Output TT lines, each containing one integer, the answer for each test case.

3
5 1 1 1 1
4 3 4 3 2
461564597527246 987489553 321654648 164165256 315648984
40
4193
480929868

Hint

Sample explanation:

For the first sample, the sequence is 1,1,2,3,51, 1, 2, 3, 5, so the answer is 12+12+22+32+52=401^2 + 1^2 + 2^2 + 3^2 + 5^2 = 40.

For the second sample, the sequence is 3,4,18,623, 4, 18, 62, so the answer is 32+42+182+622=41933^2 + 4^2 + 18^2 + 62^2 = 4193.

The third sample will not be explained.

For the first 20%20\% of the testdata, it is guaranteed that x=y=1x = y = 1.

For 100%100\% of the testdata, T=30000T = 30000, 1n10181 \le n \le 10^{18}, 1a1,a2,x,y1091 \le a_1, a_2, x, y \le 10^9.

Translated by ChatGPT 5