Jzzhu have invented a kind of sequences, they meet the following property:
You is given x and y, please calculate fn modulo 1000000007 (9 + 7).
Input
The first line contains 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 (9 + 7).
Examplesinput
2 3
3
Output
1
Input
0-1
2
Output
1000000006
Note
In the first sample, f2 = F1 + f3, 3 = 2 + f3, F 3 = 1.
In the second Sample, F 2 =-1; -1 modulo (109 + 7) equals (109 + 6).
test instructions < Span class= "Tex-span" >: < Span class= "Upper-index" to give a recursive and first two items, to find the value of the nth Xiangmo 1e9+7.
The following: The problem is actually very water. Just recently are trying to write some matrix fast power of the topic, the most difficult is the recursive type and construct the matrix, and this problem directly gave a recursive, the heart itch to use the matrix. _ (: 3"∠) _
F (i+1) =f (i)-f (i-1) can be derived from F (i) =f (i+1) +f (i-1)
And since i>=2, starting with F (1),
F (3) = (1) * F (2) + ( -1) * F (1)
F (2) = (1) * F (1) + (0) * F (0)
It is also important to note that the resulting value is negative and must be processed again. (Recent total WA in this)
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5 #definell __int646 using namespacestd;7 8 Const intMoD =1000000007;9 structMatrixTen { Onell x[2][2]; A }; - Matrix Mul (matrix A, matrix B) - { the Matrix C; -c.x[0][0] = c.x[0][1] = c.x[1][0] = c.x[1][1] =0; - for(inti =0; I <2; i++) - for(intj =0; J <2; J + +) + { - for(intK =0; K <2; k++) + { AC.X[I][J] + = a.x[i][k] *B.x[k][j]; at } -C.X[I][J]%=MoD; - } - returnC; - } - Matrix Powe (Matrix X, ll N) in { - Matrix R; tor.x[1][1] = r.x[0][0] =1; // Note initialization +r.x[0][1] = r.x[1][0] =0; - while(n) the { * if(N &1) $R =Mul (R, x);Panax Notoginsengx =mul (x, x); -N >>=1; the } + returnR; A } the intMain () + { - $ ll x, y, n, ans; $ while(~SCANF ("%i64d%i64d%i64d", &x, &y, &N)) - { - if(n = =2) theprintf"%i64d\n", (Y%mod + MoD)%MoD); ///Negative case Considerations - Else if(n = =1)Wuyiprintf"%i64d\n", (X%mod + MoD)%MoD); the Else - { Wu Matrix D; -d.x[0][0] =1; Aboutd.x[0][1] = -1; $d.x[1][0] =1; -d.x[1][1] =0; - -D = Powe (d, N-2); AAns = d.x[0][0] * y +d.x[0][1]*x; +printf"%i64d\n", (ans%mod+mod)%MoD); the } - $ } the}
Codeforces 450B Div.2 Jzzhu and sequences matrix fast power or law