数列

题目链接

 

#include<bits/stdc++.h>
#define p 1000000007
using namespace std;
long long n,a1,a2,x,y,T;
struct Node{
    long long a[4][4];
}a,t;
Node operator *(Node a,Node b){
    Node c;
    for (int i=0;i<4;++i)
        for (int j=0;j<4;++j)
            c.a[i][j]=0;
    for (int i=0;i<4;++i)
        for (int j=0;j<4;++j)
            for (int k=0;k<4;++k)
                c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%p;
    return c;
}
int main(){
    scanf("%lld",&T);
    while (T--){
        scanf("%lld%lld%lld%lld%lld",&n,&a1,&a2,&x,&y);
        if (n==1){
            printf("%lld\n",a1*a1%p);
            continue;
        }
        if (n==2){
            printf("%lld\n",a2*a2%p);
            continue;
        }
        for (int i=0;i<4;++i)
            for (int j=0;j<4;++j)
                a.a[i][j]=t.a[i][j]=0;
        a.a[0][0]=a1*a1%p;
        a.a[0][1]=a2*a2%p;
        a.a[0][2]=a1*a2%p;
        a.a[0][3]=(a.a[0][0]+a.a[0][1])%p;
        t.a[0][1]=t.a[0][3]=y*y%p;
        t.a[1][1]=t.a[1][3]=x*x%p;
        t.a[2][1]=t.a[2][3]=2*x*y%p;
        t.a[1][0]=t.a[3][3]=1;
        t.a[1][2]=x;
        t.a[2][2]=y;
        for (n-=2;n;n>>=1,t=t*t)
            if (n&1)
                a=a*t;
        printf("%lld\n",a.a[0][3]);
    }
    return 0;
}

 

  

 

posted @ 2019-02-22 12:46  Trimsteanima  阅读(92)  评论(0编辑  收藏  举报
Live2D