Firepersons - POJ 2118 矩阵乘法递推

Firepersons
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 900 Accepted: 405

Description

The Association for Courtly Manners, an international organization for standardization of social interactions (Better known under the name Absurdly Clumsy Moralists, but let's not take prejudice.) has decided to create a new international standard defining ranks of firepersons (Formerly firemen, but the international standards of course must be politically correct.) - each fireperson receives an integer number describing his rank and when they arrive to a fire, they must enter the fire ground in order of increasing ranks and the low ranked firepersons must keep the fire burning long enough for the high ranked firepersons to enjoy extinguishing sufficiently. 

The ranks are assigned according to an Arbitrary Constant Multiplier Sequence. An ACM-sequence of order k is an integer sequence defined by its first k terms a0, a1,...ak-1 and a recurrence relation an1<=i<=kan-ibimod 10 000 for n >= k, where the bi's are integer constants. The i-th oldest fireperson then gets rank ai. 

Your task is to calculate the rank of the i-th fireperson, given parameters of the ACM-sequence and the number i. 

Input

The input consists of several instances. Each instance is described on a single line containing the following integers separated by a single space: k, a0, , ak-1, b1, , bk, i. Here 1 <= k <= 100 is the order of the sequence, 0 <= ai < 10 000 are the first k elements of the sequence, 0 <= bi < 10 000 are the multipliers and 0 <= i < 1 000 000 000 is the number of the element we ask for. 

The input ends with a line containing a number 0. 

Output

The output consists of several lines corresponding to the instances on the input. The l-th line contains a single integer ai which is the i-th element of the sequence described by the l-th input instance.

Sample Input

2 0 1 1 1 6
0

Sample Output

8

题意:an1<=i<=kan-ibi问第n个数是多少。

思路:矩阵乘法递推+快速幂,矩阵构造如下,如k=3时

           a2  a1  a0                   b1  1  0

           0   0    0           *  (     b2  0  1     )^(i-k);

           0   0    0                    b3   0  0

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
    int f[110][110];
};
int n,m,MOD=10000;
node a,b,first,ret;
node mul(node A,node B)
{
    int i,j,k;
    node C;
    for(i=1;i<=n;i++)
       for(j=1;j<=n;j++)
       {
           C.f[i][j]=0;
           for(k=1;k<=n;k++)
              C.f[i][j]=(C.f[i][j]+A.f[i][k]*B.f[k][j])%MOD;
       }
    return C;
}
int main()
{
    int i,j,k,p;
    for(i=1;i<=100;i++)
       b.f[i][i+1]=1;
    while(~scanf("%d",&n) && n>0)
    {
        for(i=n;i>=1;i--)
           scanf("%d",&a.f[1][i]);
        for(i=1;i<=n;i++)
           scanf("%d",&b.f[i][1]);
        scanf("%d",&m);
        m++;
        if(m<=n)
        {
            printf("%d\n",a.f[1][n-m+1]);
            continue;
        }
        p=m-n-1;
        first=b;
        ret=b;
        while(p)
        {
            if(p&1)
              ret=mul(ret,first);
            first=mul(first,first);
            p/=2;
        }
        ret=mul(a,ret);
        printf("%d\n",ret.f[1][1]);
    }
}


   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值