原题链接: https://codeforces.com/problemset/problem/450/B
测试样例
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 equals
.
题意: 给出一个序列如下:
需要你求出 mod
的值。
解题思路: 这道题我们肯定不是无脑递归推下去的,这肯定不现实,那么我们该如何做呢?根据给定信息入手,既然。那么
.又因为
.这两个式子相加我们自然可以得到
。 故我们可以得到这个序列周期为6,则我们把前六个数求出来即可。注意,我们负数取模的规则为先加模再取模。
AC代码
/*
*
*/
//POJ不支持
using namespace std;
const int inf=0x3f3f3f3f;//无穷大。
const int maxn=1e5;//限定值。
typedef long long ll;
const ll mod=1000000007;
ll a[6];
int n;
int main(){
while(cin>>a[0]>>a[1]){
cin>>n;
rep(i,2,5){
a[i]=(a[i-1]-a[i-2])%mod;
}
cout<<(a[(n-1)%6]+mod)%mod<<endl;
}
return 0;
}