Recursive sequence 矩阵快速幂

 Recursive sequence

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

        
  

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=Fn−1+2Fn−2+n^4
Fn−1+2Fn−2+(n+1−1)^4
=Fn−1+2Fn−2+∑i=14Ci4(n−1)i=Fn−1+2Fn−2+∑i=14C4i(n−1)i,emmmm 符号我不会打,反正就是将n^4通过二项式分开得到几个式子用来构造矩阵,,,,,哎呀,,,说不请,不知道怎么构造矩阵的可以戳上面那个链接,里面讲的很详细。代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include<string>
#include <algorithm>
#define INF 0x3f3f3f3f
#define ll long long
#define mmm(a,b) memset(a,b,sizeof(a))
//const int N=1e9+10;
const ll mod=2147493647;
struct mat
{
    ll m[7][7];
};
mat I=
{
    0, 1, 0, 0, 0, 0, 0,
    2, 1, 1, 4, 6, 4, 1,
    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
};
mat mul(mat a,mat b)
{
    mat c;
    for(int i=0; i<7; i++)
    {
        for(int j=0; j<7; j++)
        {
            c.m[i][j]=0;
            for(int k=0; k<7; k++)
            {
                c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
                c.m[i][j]%=mod;
            }
        }
    }
    return c;
}
mat quick_pow(mat a,ll b)
{
    mat ans=I;
    mat tmp=a;
    while(b>0)
    {
        if(b&1)
            ans=mul(ans,tmp);
        tmp=mul(tmp,tmp);
        b>>=1;
    }
    return ans;
}
int main()
{
    ll n,t,a,b;
    scanf("%lld",&t);
    while(t--)
    {
        mat p,ans;
        scanf("%lld%lld%lld",&n,&a,&b);
        if(n==1) printf("%lld\n",a);
        else if(n==2) printf("%lld\n",b);
        else
        {
          mmm(p.m,0);
          p.m[0][0]=a;
          p.m[1][0]=b;
          p.m[2][0]=1;
          p.m[3][0]=2;
          p.m[4][0]=4;
          p.m[5][0]=8;
          p.m[6][0]=16;
          ll zhishu=n-3;
          ans=quick_pow(I,zhishu);
          ans=mul(ans,p);
          printf("%lld\n",ans.m[1][0]);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40510246/article/details/81484249