UV-10870 recurrences (matrix power)

Source: Internet
Author: User

Consider recurrent functions ofthe following form:

F (n) = A1 F (n-1) + A2 F (n-2) + A3 F (n-3) +... + Ad f (n-d), for N> D.
A1, A2,..., ad-Arbitrary Constants.

A famous example is the Fibonacci sequence, defined as: F (1) = 1, F (2) = 1, F (n) = f (n-1) + f (n-2 ). here d = 2, a1 = 1, a2 = 1.

Every such function is completely described byspecifying D (which is called the order of recurrence), values of dcoefficients: A1, A2 ,..., AD, and values off (1), F (2 ),..., F (d ). you'll be given these numbers, and two integers n and M. your program's job is to compute F (n) Modulo M.

Input

Input file contains several test cases. Each testcase begins with three integers:D,N,M, Followed by twosetsDNon-negative integers. The first set contains coefficients: A1, A2,...,D. The second set gives values of F (1), F (2),..., F (D).

You can assume that: 1 <=D<= 15, 1 <=N<= 231-1, 1 <=M<= 46340. allnumbers in the input will fit in signed 32-bit integer.

Input is terminated by line containing threezeroes instead of d, n, m. Two consecutive test cases are separated by a blankline.

Output

For each test case, print the value of F (N) (ModM) On a separate line. It must be a non-negative integer, lessM.

 

Sampleinput output for sample input
1 1 100
2
1
 
2 10 100
1 1
1 1
 
3 2147483647 12345
12345678 0 12345

1 2 3

 

0 0 0

1
55
423

 

 

Problem setter: maxfurlong

Special thanks: derekkisman, EPS.

Consider a recursive relationship f [N] = A1 * f [n-1] + A2 * f [N-2] + .... ad * f [n-d], evaluate f [N] % m

Idea: similar to the recursive matrix structure of the classic Fibonacci

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 30;int n, m, d;struct Matrix {ll v[maxn][maxn];Matrix() {} Matrix(int x) {init();for (int i = 0; i < maxn; i++)v[i][i] = x;}void init() {memset(v, 0, sizeof(v));}Matrix operator *(Matrix const &b) const {Matrix c;c.init();for (int i = 0; i < d; i++)for (int j = 0; j < d; j++)for (int k = 0; k < d; k++)c.v[i][j] = (c.v[i][j] + (v[i][k] * b.v[k][j]) % m) % m;return c;}Matrix operator ^(int b) {Matrix tmp = *this, res(1);while (b) {if (b & 1)res = res * tmp;tmp = tmp * tmp;b >>= 1;}return res;}} a, b, res;ll f[maxn];int main() {while (scanf("%d%d%d", &d, &n, &m) != EOF && n+m+d) {a.init();b.init();for (int i = 0; i < d; i++)scanf("%lld", &a.v[0][i]);for (int i = 0; i < d; i++)scanf("%lld", &f[i]);for (int i = 1; i < d; i++)a.v[i][i-1] = 1;if (n == 1) {printf("%lld\n", f[n-1]);continue;}b = a ^ (n-d);ll ans = 0;for (int i = 0; i < d; i++)ans = (ans + (f[d - 1 - i] * b.v[0][i]) % m) % m;printf("%lld\n", ans);}return 0;}


UV-10870 recurrences (matrix power)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.