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:
![](https://i-blog.csdnimg.cn/blog_migrate/a3ffcf1c3e12b33768ccdfcb85af58fb.png)
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).
比较简单的矩阵快速幂题目;
递推矩阵为|1 -1|
|1 0 |
#include<bits/stdc++.h>
using namespace std;
struct Mat
{
long long mat[2][2];
};
Mat operator *(Mat a,Mat b)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
for(int k=0;k<2;++k)
{
for(int i=0;i<2;++i)
{
for(int j=0;j<2;++j)
{
c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
c.mat[i][j]%=1000000007;
}
}
}
return c;
}
Mat operator ^(Mat a,int k)
{
Mat c;
for(int i=0;i<2;++i)
for(int j=0;j<2;++j)
c.mat[i][j]=(i==j);
for(;k;k>>=1)
{
if(k&1) c=c*a;
a=a*a;
}
return c;
}
int main()
{
int x,y;
int n;
scanf("%d %d",&x,&y);
scanf("%d",&n);
int ans=0;
if(n==1)
{
ans=x%1000000007;
}
else if(n==2)
{
ans=y%1000000007;
}
else
{
Mat a;
a.mat[0][0]=1;
a.mat[0][1]=-1;
a.mat[1][0]=1;
a.mat[1][1]=0;
Mat temp;
temp=a^(n-2);
ans=(temp.mat[0][0]*y+temp.mat[0][1]*x)%1000000007;
}
if(ans<0) ans+=1000000007;
printf("%d\n",ans);
return 0;
}