题目大意:
给定一个序列f,其中f[1]=x,f[2]=y;
序列恒满足f[i]=f[i-1]+f[i+1],求f[n]的值。
考虑模拟一下这个序列,如下为过程
1 2 3 4 5 6 7 8
x y y-x -x -y x-y x y
从模拟的过程我们不难看出,这个序列为一个周期为6的序列,所以代码如下。
写得比较繁琐,用solve计算n为周期中第几个数,mol计算取膜。
#include<cstdio>
#include<cstring>
#include<iostream>
const int N=1000000000+7;
using namespace std;
int x,y,n;
int mol(int x)
{
return (x+N)%N;
}
int solve(int n)
{
int k=n%6;
if(k==0)
return mol(mol(x)-mol(y));
if(k==1)
return mol(x);
if(k==2)
return mol(y);
if(k==3)
return mol(mol(y)-mol(x));
if(k==4)
return mol(-x);
if(k==5)
return mol(-y);
}
int main()
{
cin>>x>>y>>n;
cout<<solve(n);
//while(1);
return 0;
}