HDU 5950 Recursive sequence(矩阵快速幂)

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.



思路

先得到一个递推式

fn=fn1+2fn2+n4 f n = f n − 1 + 2 f n − 2 + n 4

易得 (n+1)4FnFn1 { ( n + 1 ) 4 F n F n − 1 } = = {(n+1n)400112010} · n4Fn1Fn2 { n 4 F n − 1 F n − 2 }
然而系数矩阵与n相关不能使用
所以我们需要使系数矩阵变的与n无关
系数矩阵要与n无关,需要对 n4 n 4 进行二项式展开
Fn=Fn1+2Fn2+n4 F n = F n − 1 + 2 F n − 2 + n 4
=Fn1+2Fn2+(n+11)4 = F n − 1 + 2 F n − 2 + ( n + 1 − 1 ) 4
=Fn1+2Fn2+i=14Ci4(n1)i = F n − 1 + 2 F n − 2 + ∑ i = 1 4 C 4 i ( n − 1 ) i

FnFn1n0n1n2n3n4 { F n F n − 1 n 0 n 1 n 2 n 3 n 4 } = = {1214641100000000100000011000001210000133100014641} · {Fn1Fn2(n1)0(n1)1(n1)2(n1)3(n1)4}

可以看下这篇博客学习构造系数矩阵 根据递推公式构造系数矩阵用于快速幂


代码
#include<stdio.h>
#include<string.h>
using namespace std;

#define ll long long
const ll maxn = 7;
const ll mod = 2147493647;

struct mat
{
    ll m[maxn][maxn];
};

mat operator *(mat x, mat y)
{
    mat ret;
    for(int i = 0; i < maxn; i++)
    {
        for(int j = 0; j < maxn; j++)
        {
            ret.m[i][j] = 0;
            for(int k = 0; k < maxn; k++)
            {
                ret.m[i][j] = (x.m[i][k] * y.m[k][j] + ret.m[i][j]) % mod;
            }
        }
    }
    return ret;
}

mat pow_mat(mat a, ll fuck)
{
    mat ret;
    memset(ret.m,0,sizeof(ret.m));
    for(int i = 0; i < maxn; i++) ret.m[i][i] = 1;
    while(fuck)
    {
        if(fuck&1) ret = ret*a;
        a = a*a;
        fuck >>= 1;
    }
    return ret;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll f1, f2, m;
        scanf("%lld%lld%lld",&m,&f1,&f2);
        if(m == 1) printf("%lld\n",f1);
        else if(m == 2) printf("%lld\n",f2);
        else
        {
            mat a;
            memset(a.m,0,sizeof(a.m));
            a.m[0][0] = f2;
            a.m[1][0] = f1;
            a.m[2][0] = 1;
            a.m[3][0] = 2;
            a.m[4][0] = 4;
            a.m[5][0] = 8;
            a.m[6][0] = 16;
            mat b =
            {
                1, 2, 1, 4, 6, 4, 1,
                1, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 0, 0, 0, 0,
                0, 0, 1, 1, 0, 0, 0,
                0, 0, 1, 2, 1, 0, 0,
                0, 0, 1, 3, 3, 1, 0,
                0, 0, 1, 4, 6, 4, 1
            };
            a = pow_mat(b,m-2)*a;
            printf("%lld\n",a.m[0][0]);
        }
    }
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值