P5175 数列(矩阵加速)

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
ll read(){
    R ll res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
    if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
    while(z[++Z]=x%10+48,x/=10);
    while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int P=1e9+7;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
	R int res=1;
	for(;y;y>>=1,x=mul(x,x))if(y&1)res=mul(res,x);
	return res;
}
struct Matrix{
	int a[4][4];
	Matrix(){memset(a,0,sizeof(a));}
	inline int* operator [](const int &x){return a[x];}
	Matrix operator *(Matrix b){
		Matrix res;
		fp(i,0,3)fp(k,0,3)fp(j,0,3)res[i][j]=add(res[i][j],mul(a[i][k],b[k][j]));
		return res;
	}
}A,B;
ll n;int a1,a2,x,y;
signed main(){
//	freopen("testdata.in","r",stdin);
	int T=read();
	while(T--){
		n=read(),a1=read(),a2=read(),x=read(),y=read();
		if(n<=2){print(n&1?mul(a1,a1):add(mul(a1,a1),mul(a2,a2)));continue;}
		fp(i,0,3)fp(j,0,3)A[i][j]=B[i][j]=0;
		A[0][0]=mul(a2,a2),A[0][1]=mul(a1,a1);
		A[0][2]=mul(a1,a2),A[0][3]=add(A[0][0],A[0][1]);
		B[0][0]=B[0][3]=mul(x,x),B[0][1]=B[3][3]=1;
		B[1][0]=B[1][3]=mul(y,y),B[0][2]=x;
		B[2][0]=B[2][3]=mul(2,mul(x,y)),B[2][2]=y;
		for(n-=2;n;n>>=1,B=B*B)if(n&1)A=A*B;
		print(A[0][3]);
	}return Ot(),0;
}
posted @ 2019-01-05 13:49  bztMinamoto  阅读(206)  评论(0编辑  收藏  举报
Live2D