Nyoj 301 递推求值

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=301

问题意思:

给你一个递推公式:

f(x)=a*f(x-2)+b*f(x-1)+c。给你a,b,c,f1,f2,求第n项。

其中0<=f(1),f(2)<100,-100<=a,b,c<=100,1<=n<=1000000000 (10^9)

这就用到了刚学的矩阵快速幂求递推值。

思路:

[fn, fn-1, 1] = [fn-1,fn-2, 1] * A,A为一个3*3的矩阵。可以求解出来。

A = {

   b,1,0,

   a,0,0,

   c,0,1,

};

这 样就很简单了。[fn, fn-1,1] = [f1,f2,1]*A^n-2;求出来的1*3的矩阵就是一个答案。。

Code:

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
const LL mod = 1e6 + 7;
const int N = 5;
struct Matrix
{
    int n, m;
    LL a[N][N];
    Matrix(){
        memset(a, 0, sizeof(a));
    }
}A, B;

Matrix operator * (Matrix a, Matrix b){
    Matrix ans;
    ans.n = a.n; ans.m = b.m;
    for(int i = 1; i <= a.n; i ++){
        for(int j = 1; j <= b.m; j ++){
            for(int k = 1; k <= b.n; k ++)
            ans.a[i][j] = ((ans.a[i][j] + a.a[i][k] * b.a[k][j]) % mod + mod) % mod;
        }
    }
    return ans;
}

LL Quick_Power(LL n)
{
    Matrix ans = A, k = B;
    while(n){
        if(n & 1) {
            ans = ans * k;
        }
        k = k * k;
        n = n >> 1;
    }
    return (ans.a[1][1] % mod + mod) % mod;
}
int main()
{
    int T;
    LL f1, f2, a, b, c, n;
    cin >> T;
    while(T -- && cin >> f1 >> f2 >> a >> b >> c >> n){
        A.n = 1; A.m = 3;
        A.a[1][1] = f2; A.a[1][2] = f1; A.a[1][3] = 1;
        B.n = 3; B.m = 3;
        B.a[1][1] = b; B.a[2][1] = a; B.a[3][1] = c;
        B.a[1][2] = 1; B.a[3][3] = 1;
        if(n == 1){
            cout << (f1 % mod + mod) % mod<< endl;
            continue;
        }
        if(n == 2){
            cout << (f2 % mod + mod) % mod<< endl;
            continue;
        }
        cout<< Quick_Power(n - 2) << endl;
    }
    return 0;
}

学习了一个矩阵快速幂求解递推值的好办法。。!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值