HDU 5950 Recursive sequence(构造矩阵+矩阵乘法)——2016ACM/ICPC亚洲区沈阳站(重现赛)

传送门

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 98    Accepted Submission(s): 46


Problem Description
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 i4 . 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 < 231 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
Hint
In the first case, the third number is 85 = 2*1十2十3^4.
In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

题目大意:

f(n) = f(n1)+2f(n2)+n4 ,其中 f(1)=a,f(2)=b

解题思路:

首先一看这个题目,很容易想到构造矩阵:那么我们现在来分析一下怎么构造这个矩阵,那么 (n+1)4 = n4+4n3+6n2+4n+1 所以光 (n+1)4 这个矩阵就能构造出 55 的一个矩阵来, 然后 f(n) = f(n1)+2f(n2) 这个是 22 的矩阵,所以构造出来就应该是 77 的转移矩阵 A
{f(n),f(n1),n4,n3,n2,n,1}A={f(n+1),f(n),(n+1)4,(n+1)3,(n+1)2,(n+1),1}

然后 f(n+1) = f(n)+2f(n1)+(n+1)4 , 可得矩阵 A

1214641100000000146410001331000012100000110000001

然后 f(n+1) = {{f(n),f(n1),n4,n3,n2,n,1}An2}.mat[0][0]

My Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef long long LL;
const int MAXN = 7;
const LL MOD = 2147493647;
typedef long long LL;
typedef struct
{
    LL mat[MAXN][MAXN];
    void Init(){
        memset(mat, 0, sizeof(mat));
        for(int i=0; i<MAXN; i++)
            mat[i][i] = 1;
    }
} Matrix;
///求得的矩阵
Matrix p = {1, 1, 0, 0, 0, 0, 0,
            2, 0, 0, 0, 0, 0, 0,
            1, 0, 1, 0, 0, 0, 0,
            4, 0, 4, 1, 0, 0, 0,
            6, 0, 6, 3, 1, 0, 0,
            4, 0, 4, 3, 2, 1, 0,
            1, 0, 1, 1, 1, 1, 1
           };
///矩阵乘法
Matrix Mul_Matrix(Matrix a, Matrix b)
{
    Matrix c;
    for(int i=0; i<MAXN; i++)
    {
        for(int j=0; j<MAXN; j++)
        {
            c.mat[i][j] = 0;
            for(int k=0; k<MAXN; k++)
            {
                c.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
                c.mat[i][j] %= MOD;
            }
        }
    }
    return c;
}
///矩阵的快速幂
Matrix quick_Mod_Matrix(LL m)
{
    Matrix ans, b = p;
    ans.Init();
    while(m)
    {
        if(m & 1)
            ans = Mul_Matrix(ans, b);
        m>>=1;
        b = Mul_Matrix(b, b);
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        LL n, a, b;
        scanf("%lld%lld%lld",&n,&a,&b);
        if(n == 1){
            printf("%lld\n",a);
            continue;
        }
        if(n == 2){
            printf("%lld\n",b);
            continue;
        }
        Matrix tmp = quick_Mod_Matrix(n-2);
        LL ans = b*tmp.mat[0][0] % MOD;
        ans = (ans + a*tmp.mat[1][0]%MOD) % MOD;
        ans = (ans + 16*tmp.mat[2][0]%MOD) % MOD;
        ans = (ans + 8*tmp.mat[3][0]%MOD) % MOD;
        ans = (ans + 4*tmp.mat[4][0]%MOD) % MOD;
        ans = (ans + 2*tmp.mat[5][0]%MOD) % MOD;
        ans = (ans+tmp.mat[6][0]) % MOD;
        printf("%lld\n",ans);
    }
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值