【矩阵加速】[SPOJ SEQ]Recursive Sequence

题目描述

Sequence ( ai ) of natural numbers is defined as follows:

ai=bi(ik)
ai=c1ai1+c2ai2+...+ckaik(i>k)

where bj and cj are given natural numbers for 1jk . Your task is to compute an for given n and output it modulo 109 .

Input

On the first row there is the number C of test cases (equal to about 1000).
Each test contains four lines:

k - number of elements of (c) and (b) (1 <= k <= 10)
b1,…,bk - k natural numbers where 0 <= bj <= 109 separated by spaces
c1,…,ck - k natural numbers where 0 <= cj <= 109 separated by spaces
n - natural number (1 <= n <= 109 )

Output

Exactly C lines, one for each test case: an modulo 109

Example

Input:

3
3
5 8 2
32 54 6
2
3
1 2 3
4 5 6
6
3
24 354 6
56 57 465
98765432

Output:

8
714
257599514

题目分析

矩阵加速裸题

代码

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 4;
const long long MOD = 1e9;
struct Matrix {
    long long Ma[MAXN+10][MAXN+10];
    int n, m;
    void Clear(int u, int un, int um){
        n = un, m = um;
        for(int i=1;i<=un;i++)
            for(int j=1;j<=um;j++)
                Ma[i][j] = 0;
        if(u) for(int i=1;i<=un;i++)
            Ma[i][i] = 1;
    }
    Matrix operator* (const Matrix& ma) {
        Matrix ret ;
        ret.Clear(0, n, m);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=ma.m;j++){
                for(int k=1;k<=ma.n;k++){
                    ret.Ma[i][j] += Ma[i][k] * ma.Ma[k][j];
                    ret.Ma[i][j] %= MOD;
                }
            }
        }
        return ret;
    }
};
Matrix Mpow(Matrix m, int p){
    Matrix ret;
    if(p == 0){
        ret.Clear(1, 2, 2);
        return ret;
    }else if(p == 1) return m;
    ret = Mpow(m, p/2);
    if(p%2 == 0) return ret * ret;
    return (ret * ret) * m;
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        Matrix str, _base;
        int n;
        scanf("%d", &n);
        str.Clear(0, n, n);
        _base.Clear(0, n, n);
        for(int i=1;i<=n;i++) scanf("%lld", &_base.Ma[1][n-i+1]);
        for(int i=1;i<=n;i++){
            scanf("%lld", &str.Ma[i][1]);
            str.Ma[i][i+1] = 1;
        }
        int k;
        scanf("%d", &k);
        if(k <= n) printf("%lld\n", _base.Ma[1][n-k+1]);
        else{
            int ps = k - n;
            Matrix ans = Mpow(str, ps);
            long long tans = 0;
            for(int i=1;i<=n;i++)
                tans += (_base.Ma[1][i] * ans.Ma[i][1])%MOD;
            printf("%lld\n", (tans%MOD+MOD)%MOD);
        }
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值