B. Jzzhu and Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
Input
The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).
Output
Output a single integer representing fn modulo 1000000007 (109 + 7).
Examples
input
2 3 3
output
1
input
0 -1 2
output
1000000006
Note
In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.
In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals (109 + 6).
很明显 构造矩阵
F(N) = F(N-1) +F(N+1) 则有 F(N-1) =F(N-2) + F( N) 故 F(N) =F(N-1) --F(N-2)
代码:
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cstring>
typedef long long ll;
const int N= 55;
const int MOD=1000000007;
const int MAXN=2;
using namespace std;
struct Matrix{
ll arr[N][N];
void init()
{
memset(arr,0,sizeof(arr));
for(int i=0;i<MAXN;i++)
arr[i][i]=1;//初始化
}
void iinit()
{
memset(arr,0,sizeof(arr));
arr[0][0]=arr[1][0]=1;
arr[0][1]=-1;
}
}A;
Matrix mul(Matrix X,Matrix Y)// 矩阵乘法
{
Matrix ans;
for(int i=0;i<MAXN;i++)
for(int j=0;j<MAXN;j++){
ans.arr[i][j]=0;
for(int k=0;k<MAXN;k++){
ans.arr[i][j]+=X.arr[i][k]*Y.arr[k][j];
ans.arr[i][j]%=MOD;
}
}
return ans;
}
Matrix Q_pow(Matrix B,int n)// 矩阵快速幂
{
Matrix ans;
ans.init();
while(n)
{
if(n&1)
ans=mul(ans,B);
n>>=1;
B=mul(B,B);
}
return ans;
}
Matrix Add(Matrix a,Matrix b) //(a+b)%mod 矩阵加法
{
int i,j,k;
Matrix ans;
for(i=0;i<MAXN;i++)
for(j=0;j<MAXN;j++)
{
ans.arr[i][j]=a.arr[i][j]+b.arr[i][j];
ans.arr[i][j]%=MOD;
}
return ans;
}
Matrix Sum(Matrix a,int n)// 矩阵和
{
int m;
Matrix ans,pre;
if(n==1) return ans;
m=n/2;
pre=Sum(a,m);
ans=Add(pre,mul(pre,Q_pow(a,m)));
if(n&1)
ans=Add(ans,Q_pow(a,n));
return ans;
}
int main()
{
int x,y,n;
while(~scanf("%d %d",&x,&y))
{
scanf("%d",&n);
Matrix ans;
ans.iinit();
if(n==1)
{
printf("%d\n",(x+MOD)%MOD);
}
else if(n==2)
{
printf("%d\n",(y+MOD)%MOD);
}
else
{
ans=Q_pow(ans,n-2);
ll res=(ans.arr[0][0]*y+ans.arr[0][1]*x)%MOD;
printf("%lld\n",(res+MOD)%MOD);
}
}
return 0;
}