B. Jzzhu and Sequences

来源:互联网 发布:windows 7 位置不可用 编辑:程序博客网 时间:2024/05/17 09:35

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).

Sample test(s)
input
2 33
output
1
input
0 -12
output
1000000006
Note

In the first sample, f2 = f1 + f33 = 2 + f3f3 = 1.

In the second sample, f2 =  - 1 - 1 modulo (109 + 7) equals (109 + 6).


解题说明:此题是一个数列求值,很显然用暴力计算超时,所以需要通过找规律来缩短时间。试验过后能发现数列的周期为6,这样题目就很容易了。


#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstdlib>#include<cstring>using namespace std;int F[10];int main() {int i, x, y, n, ans;scanf("%d %d", &x, &y);scanf("%d", &n);F[0] = x;F[1] = y;for (i = 1; i <= 6; ++i) {F[i+1] = (F[i] - F[i-1]) % 1000000007;}ans = (F[(n+5)%6] + 1000000007) % 1000000007;printf("%d\n", ans);return 0;}


0 0
原创粉丝点击