A - Jzzhu and Sequences
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description
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 Input
Input
2 3 3
Output
1
Input
0 -1 2
Output
1000000006
Hint
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).
题目大意:
输入f2和f1的值,最后算出fn的值。
解题思路:
f[1] = x;f[2] = y;
f[i] = f[i-1]+f[i+1]i = 1;f[1] = f[0]+f[2];->f[0] = x-y;i = 2;f[2] = f[1]+f[3];->f[3] = y-x;i = 3;f[3] = f[2]+f[4];->f[4] = -x;i = 4;f[4] = f[3]+f[5];->f[5] = -yi = 5;f[5] = f[4]+f[6];->f[6] = x-y;一开始被51组数据卡了三次,原因就是在取MOD的过程中,如果a[n]<0..while ( a[n]<0 )a[n]+=MOD;将其变为正数后,才能进行a[n]%MOD的运算。
代码:
# include# include # include # include # include # include # include # include # include # include # include # include # include # include # include # include
# include