Recursive sequence HDU - 5950 (矩阵构造)


Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.
Input
The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231231 as described above.
Output
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
Sample Input
2
3 1 2
4 1 10
Sample Output
85
369


头一次写矩阵构造的题,看了别人的才懂得如何构造…矩阵构造主要就是看An与An-1的关系,这里的an=n^4+a(n-1)+2*a(n-2),主要的难点就是怎么把n^4变成递推过来的形式,也就是找到与n-1有关的东西,通过把n^4展开就能找到递推关系了,然后套入矩阵快速幂就能得出答案,处理溢出的地方得注意。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<stack>
using namespace std;
const long long mod = 2147493647;
long long n, aa, bb;
struct ju{
    long long a[7][7];
    void init(){
        memset(a, 0, sizeof(a));
        for (int i = 0; i < 7; i++){
            a[i][i] = 1;
        }
    }
    ju operator *(const ju & tmp){
        ju ans;
        memset(ans.a, 0, sizeof(ans.a));
        for (int i = 0; i<7; i++){
            for (int j = 0; j<7; j++){
                for (int k = 0; k<7; k++){
                    ans.a[i][j] = (ans.a[i][j] + a[i][k] * tmp.a[k][j]%mod) % mod;
                }
            }
        }
        return ans;
    }
};

long long pow_mod(ju cur, long long n){
    ju ans;
    ans.init();
    while (n){
        if (n % 2)ans = ans*cur;
        cur = cur*cur;
        n = n / 2;
    }
    long long cnt;
    cnt = ans.a[0][0]*bb%mod;
    cnt = (cnt + ans.a[0][1] * aa) % mod;
    cnt = (cnt + ans.a[0][2] * 16) % mod;
    cnt = (cnt + ans.a[0][3] * 8) % mod;
    cnt = (cnt + ans.a[0][4] * 4) % mod;
    cnt = (cnt + ans.a[0][5] * 2) % mod;
    cnt = (cnt + ans.a[0][6] * 1) % mod;
    return cnt;
}

int main(){
    int t;
    scanf("%d", &t);
    ju go;
    go.a[0][0] = 1, go.a[0][1] = 2, go.a[0][2] = 1, go.a[0][3] = 4, go.a[0][4] = 6, go.a[0][5] = 4 ,go.a[0][6] = 1;
    go.a[1][0] = 1, go.a[1][1] = 0, go.a[1][2] = 0, go.a[1][3] = 0, go.a[1][4] = 0, go.a[1][5] = 0, go.a[1][6] = 0;
    go.a[2][0] = 0, go.a[2][1] = 0, go.a[2][2] = 1, go.a[2][3] = 4, go.a[2][4] = 6, go.a[2][5] = 4, go.a[2][6] = 1;
    go.a[3][0] = 0, go.a[3][1] = 0, go.a[3][2] = 0, go.a[3][3] = 1, go.a[3][4] = 3, go.a[3][5] = 3, go.a[3][6] = 1;
    go.a[4][0] = 0, go.a[4][1] = 0, go.a[4][2] = 0, go.a[4][3] = 0, go.a[4][4] = 1, go.a[4][5] = 2, go.a[4][6] = 1;
    go.a[5][0] = 0, go.a[5][1] = 0, go.a[5][2] = 0, go.a[5][3] = 0, go.a[5][4] = 0, go.a[5][5] = 1, go.a[5][6] = 1;
    go.a[6][0] = 0, go.a[6][1] = 0, go.a[6][2] = 0, go.a[6][3] = 0, go.a[6][4] = 0, go.a[6][5] = 0, go.a[6][6] = 1;
    while (t--){
        scanf("%lld%lld%lld", &n, &aa, &bb);
        if (n == 1){
            printf("%lld\n", aa%mod);
        }
        else if (n == 2){
            printf("%lld\n", bb%mod);
        }
        else{
            long long ans = pow_mod(go, n - 2);
            printf("%lld\n", ans);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值