Recursive sequence matrix fast power

 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.
        
 A fast power matrix, the difficulty is how to construct a matrix, there is a good fast power matrix matrix construction blog, are interested can poke here. I also learned a matrix structure inside. So this question, then, we are to find a matrix system, available from known formulas
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 symbol I will not play, anyway, is the n ^ 4 are separated by a binomial equation used to construct the matrix obtained several ,,,,, oh say ,,, Please, do not know how to construct a matrix that can poke the link above, which is more telling. code show as below:
#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]);
        }
    }
}

 

Guess you like

Origin blog.csdn.net/qq_40510246/article/details/81484249