UVA

来源:互联网 发布:em getsel vb 编辑:程序博客网 时间:2024/05/22 00:05
Consider recurrent functions of the following form:
f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d,
where a1, a2, . . . , ad are 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 by specifying d (which is called the order of recurrence),
values of d coefficients: a1, a2, . . . , ad, and values of f(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 test case begins with three integers: d, n, m, followed by
two sets of d non-negative integers. The first set contains coefficients: a1, a2, . . . , ad. The second set
gives values of f(1), f(2), . . . , f(d).
You can assume that: 1 ≤ d ≤ 15, 1 ≤ n ≤ 2
31 − 1, 1 ≤ m ≤ 46340. All numbers in the input will
fit in signed 32-bit integer.
Input is terminated by line containing three zeroes instead of d, n, m. Two consecutive test cases
are separated by a blank line.
Output
For each test case, print the value of f(n)( mod m) on a separate line. It must be a non-negative integer,
less than m.
Sample Input
1 1 100
2
1
2 10 100
1 1
1 1
3 2147483647 12345
12345678 0 12345

1 2 3


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <stack>#include <map>#include <cmath>#include <vector>#define max_ 200010#define inf 0x3f3f3f3f#define ll long longusing namespace std;struct mat{    ll num[20][20];    int n;};int m,n,k,d;int f[20];mat mul(struct mat a,struct mat b){    struct mat ans;    ans.n=a.n;    memset(ans.num,0,sizeof(ans.num));    for(int i=1;i<=a.n;i++)    {        for(int j=1;j<=a.n;j++)        {            for(int k=1;k<=a.n;k++)            {                ans.num[i][j]+=(a.num[i][k]*b.num[k][j])%m;                ans.num[i][j]%=m;            }        }    }    return ans;}void show(struct mat a){    printf("%d\n",a.n);    for(int i=1;i<=a.n;i++)    {        for(int j=1;j<=a.n;j++)        {            printf("%d ",a.num[i][j]);        }        printf("\n" );    }}int fpow(struct mat a,int k){    struct mat ans,tmp=a;    ans.n=a.n;    memset(ans.num,0,sizeof(ans.num));    for(int i=1;i<=a.n;i++)        ans.num[i][i]=1;    while(k!=0)    {        if(k&1)            ans=mul(ans,tmp);        tmp=mul(tmp,tmp);        k/=2;    }    int sum=0;    for(int i=1;i<=d;i++)    {        sum+=(ans.num[1][i]*f[d-i+1])%m;    }    return (sum%m);}int main(int argc, char const *argv[]) {    while(cin>>d>>n>>m)    {        if(d==0&&n==0&&m==0)        break;        mat a;        a.n=d;        memset(a.num,0,sizeof(a.num));        for(int i=1;i<=d;i++)        {            cin>>a.num[1][i];            a.num[1][i]%=m;            if(i!=d)            a.num[i+1][i]=1;        }        for(int i=1;i<=d;i++)        {            cin>>f[i];            f[i]%=m;        }        if(n<=d)        {            printf("%d\n",f[n]%m);            continue;        }        printf("%d\n",fpow(a,n-d) );    }    return 0;}