您的位置:首页 > 其它

矩阵连乘(3)

2015-08-17 00:25 381 查看
[align=left]Problem Description[/align]
As we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N - 1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we want to Calculate S(N) , S(N) = A(0)2 +A(1)2+……+A(n)2.

[align=left]Input[/align]
There are several test cases.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1

[align=left]Output[/align]
For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.

[align=left]Sample Input[/align]

2 1 1
3 2 3

[align=left]Sample Output[/align]

6
196

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const long long mod=10007;

typedef struct
{
long long m[4][4];
}mat;

mat I={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};

mat calc(mat a,mat b)
{
int i,j,k;
mat c;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
c.m[i][j]=0;
for(k=0;k<4;k++)
{
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
}
c.m[i][j]=c.m[i][j]%mod;
}
return c;
}

mat matirx(mat P,long long n)
{
mat m=P,b=I;
while(n>=1)
{
if(n&1) b=calc(b,m);
n>>=1;
m=calc(m,m);
}
return b;
}

int main()
{
long long n,x,y;
while(scanf("%lld%lld%lld",&n,&x,&y)!=EOF)
{
long long sum=0;
x=x%mod;
y=y%mod;   //2*x*y可能会溢出
mat P={x*x,2*x*y,y*y,0,x,y,0,0,1,0,0,0,1,0,0,1};
mat a;
a=matirx(P,n);
sum=(a.m[3][0]+a.m[3][1]+a.m[3][2]+a.m[3][3])%mod;
printf("%lld\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: