30 矩阵快速幂;

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.
 n,a,b,的数据范围都是e9
这道题目的意思很简单就是当n>=3的时候a[n]=a[n-1]+2*a[n-2]+n^4;
已知n,第一项,第二项,让你来求第n项的数值是多少,如果我们只是看这个式子的前两项的话可以用矩阵快速幂求出来,其实如果是这个样子     的话我们也可以用矩阵快速幂来求,就是形如f(n)=p*f(n-1)+q*f(n-2)+一个多项式,这个样的话我们在构造矩阵的时候把后面的那个多项式对应着相应的第多少项展开就可以了,左面就是关于n的多项式,右边是关于(n-1)的多项式,左边的项是为了和右面的项的形式统一才会出现相应的形式,最根本的原因是右面凑出了左上角的那一项,为了在使用矩阵快速幂的时候形式是统一的,所以左面的形式要向右边的来看齐;

f[ n ]  = f[n-1]+2*f[n-2]+n^4;

f[n]   = {   1    2                   }    f[n-1]

                                                f[n-2]

接下来我们就要用(n-1)来凑出n^4,在这个过程中会产生n^3,这个时候我们要消掉,就会用到(n-1)^3,同理会出现n^2,我们要用(n-1)^2,消掉,出现n我们用n来消掉,所以为了凑出f[n]=f[n-1]+2*f[n-2]+n^4;

我们会有下面部分转移矩阵

f[n]   = {   1    2   1   4   6   4  1 }    f[n-1]

                                                      f[n-2]

                                                      (n-1)^4

                                                      (n-1)^3

                                                      (n-1)^2

                                                       n

                                                       1

为了矩阵转移我们需要左右两边的形式要统一所以会有下边的矩阵

f[n]   = {   1    2   1   4   6   4  1 }   * {f[n-1]

f[n-1]   {                                    }      f[n-2]

  n^4    {                                    }     (n-1)^4

  n^3    {                                    }     (n-1)^3

  n^2    {                                    }      (n-1)^2

  n        {                                    }      n

  1        {                                    }      1}

下面就是简单的矩阵构造了,线代

有了转移矩阵接下来我们就可以用矩阵快速幂来求了,并且我们再来普及一下就是如果这个多项式如果是这个递推数列的前n项的和的时候也可以来使用,快速幂就是解决递推问题,将线性o(n)的时间复杂度缩减到log(n)的时间复杂度内,只要你可以求出转移矩阵一切就都ojbk了,我写代码有点措

#include <bits/stdc++.h>
using namespace std;

typedef long long ll ;
const  ll  mod=2147493647;
#define rep(i,s,n) for(ll i=s;i<=n;i++)
#define per(i,n,s) for(ll i=n;i>=s;i--)
void multi(ll res[][10],ll a[][10]){
    ll c[10][10];
    rep(i,1,7) {
       rep(j,1,7){
        c[i][j]=0;
      }
    }
    rep(i,1,7){
      rep(j,1,7){
        rep(k,1,7){
          c[i][j]+=res[i][k]*a[k][j]%mod;
          c[i][j]=(c[i][j]%mod+mod)%mod;
        }
      }
    }
    rep(i,1,7){
       rep(j,1,7){
         res[i][j]=c[i][j];
       }
    }
}
void pow1(ll res[][10],ll a[][10], ll k){
 rep(i,1,7){
   rep(j,1,7){
      if(i==j) res[i][j]=1;
      else res[i][j]=0;
   }
 }
 a[1][1]=1,a[1][2]=2,a[1][3]=1,a[1][4]=4,a[1][5]=6,a[1][6]=4,a[1][7]=1;
 a[2][1]=1,a[2][2]=0,a[2][3]=0,a[2][4]=0,a[2][5]=0,a[2][6]=0,a[2][7]=0;
 a[3][1]=0,a[3][2]=0,a[3][3]=1,a[3][4]=4,a[3][5]=6,a[3][6]=4,a[3][7]=1;
 a[4][1]=0,a[4][2]=0,a[4][3]=0,a[4][4]=1,a[4][5]=3,a[4][6]=3,a[4][7]=1;
 a[5][1]=0,a[5][2]=0,a[5][3]=0,a[5][4]=0,a[5][5]=1,a[5][6]=2,a[5][7]=1;
 rep(i,1,5) a[6][i]=0,a[7][i]=0; a[6][6]=1,a[6][7]=1;
 a[7][6]=0,a[7][7]=1;
 while(k){
    if(k&1) {
            multi(res,a);
    }
    multi(a,a);
    k/=2;
 }
}
int main(){
  int t;
  //printf("%lld\n",mod);
  scanf("%d",&t);
  while(t--){
    ll aa,bb,ans1,ans2,n;
    scanf("%lld %lld %lld",&n,&aa,&bb);
    if(n==1){
        printf("%lld\n",(aa%mod+mod)%mod);
    }
    else if(n==2){
        printf("%lld\n",(bb%mod+mod)%mod);
    }
    else {
        ans1=((aa*2%mod+bb%mod)%mod+(ll)81)%mod;
        ans2=((bb*2%mod+ans1%mod)%mod+(ll)256)%mod;
        if(n==3){
            printf("%lld\n",(ans1%mod+mod)%mod);
        }
        else if(n==4){
            printf("%lld\n",(ans2%mod+mod)%mod);
        }
        else {
            ll k=n-4;
            ll a[10][10],res[10][10];
            pow1(res,a,k);
            ll ans[10];
            ll sum=0;
            ans[1]=ans2,ans[2]=ans1,ans[3]=256,ans[4]=64,ans[5]=16,ans[6]=4,ans[7]=1;
            rep(i,1,7){
              sum=(sum%mod+(res[1][i]%mod*ans[i])%mod)%mod;
            }
            printf("%lld\n",(sum%mod+mod)%mod);
        }
    }
  }
  return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值