您的位置:首页 > 其它

Chang'an(YY's problem-矩阵乘法)

2015-04-24 19:13 405 查看


矩阵乘法

#include <bits/stdc++.h>

#define LL long long

#define MOD 1000000007

using namespace std;

LL MMM(int a, int b)
{
return ((a%MOD)*(LL)(b%MOD))%MOD;
}

LL x[4], a[4], t[4];

LL* mult(LL *a,LL *b)
{

x[0]=MMM(a[0],b[0])+MMM(a[1],b[2]);
x[0]%=MOD;
x[1]=MMM(a[0],b[1])+MMM(a[1],b[3]);
x[1]%=MOD;
x[2]=MMM(a[2],b[0])+MMM(a[3],b[2]);
x[2]%=MOD;
x[3]=MMM(a[2],b[1])+MMM(a[3],b[3]);
x[3]%=MOD;
return x;
}

void print(LL *a)
{
cout<<a[0]<<" "<<a[1]<<endl;
cout<<a[2]<<" "<<a[3]<<endl;
}

LL e[4]={1,0,0,1};
LL s[4]={0,-1,1,1};

void eq(LL *a,LL *b)
{
for (int i=0;i<4;i++) a[i]=b[i];
}

int main()
{
int T, p;
long long f1, f2;
cin>>T;
for(int I=1; I<=T; I++){
cin>>f1>>f2;
cin>>p;p--;

eq(t, s);
if (p%2==0){
eq(a, e);
} else {
eq(a, t);
}
p=p>>1;
while (p>0) {
eq(t,mult(t,t));
if (p%2==1){eq(a,mult(a,t));}
p=p>>1;
}

long long ans = ((f1*a[0]+MOD)%MOD + (f2*a[2]+MOD)%MOD+MOD)%MOD;
if (ans<0) {cout<<ans+MOD<<endl;	}else{cout<<ans<<endl;}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: