Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2470 Accepted Submission(s): 987
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
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
#define mod 10007
ll n;
ll x;
ll y;
struct matrix
{
ll v[4][4];
};
matrix mul(matrix m1, matrix m2)
{
matrix c;
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
{
c.v[i][j] = 0;
for (int k = 0; k<4; k++)
c.v[i][j] += (m1.v[i][k] * m2.v[k][j]) % mod;
c.v[i][j] %= mod;
}
return c;
}
matrix qupow(matrix A)
{
n--;
matrix res = A, m = A;
while (n)
{
if (n & 1)
res = mul(res, m);
n >>= 1;
m = mul(m, m);
}
return res;
}
void solve(matrix A)
{
matrix temp = qupow(A);
ll ans = 0;
for (int i = 0; i<4; i++)
ans += temp.v[i][0]%mod;
ans%=mod;
printf("%lld
",ans);
//cout << ans << endl;
}
int main()
{
matrix A;
ll a, b, c;
while (~scanf("%lld%lld%lld",&n,&x,&y))
{
a = x*x%mod;
b = y*y%mod;
c = x*y * 2%mod;
//matrix A = { 1,0,0,0 ,1,a,1,x,0,b,0,0 0,c,0,y };
A.v[0][0]=1;
for(int i=1;i<4;i++)
A.v[0][i]=0;
A.v[1][0]=1;
A.v[1][1]=a%mod;
A.v[1][2]=1;
A.v[1][3]=x%mod;
A.v[2][0]=0;
A.v[2][1]=b%mod;
A.v[2][2]=0;
A.v[2][3]=0;
A.v[3][0]=0;
A.v[3][1]=c%mod;
A.v[3][2]=0;
A.v[3][3]=y%mod;
/*for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
cin>>A.v[i][j];*/
solve(A);
}
return 0;
}