Codeforces round #258 (Div. 2) B. jzzhu and sequences (matrix fast power)

Source: Internet
Author: User

Link: http://codeforces.com/problemset/problem/450/ B

----------------------------------------------------------------------------------------------------------------------------------------------------------
Welcome to tianci Hut: http://user.qzone.qq.com/593830943/main
 
----------------------------------------------------------------------------------------------------------------------------------------------------------

B. jzzhu and sequencestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Jzzhu has sorted Ted a kind of sequences, they meet the following property:

You are givenXAndY, Please calculateFNModulo 1000000007 (109? +? 7 ).

Input

The first line contains two integersXAndY(|X| ,? |Y|? ≤? (109). The second line contains a single integerN(1? ≤?N? ≤? 2 · 109 ).

Output

Output a single integer representingFNModulo 1000000007 (109? +? 7 ).

Sample test (s) Input
2 33
Output
1
Input
0 -12
Output
1000000006
Note

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


The Code is as follows:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; struct a {int mat [2] [2] ;}; a D, F; __int64 N, MOD; a mul (A, a B) {A T; memset (T. mat, 0, sizeof (T. mat); For (INT I = 0; I <n; I ++) {for (int K = 0; k <n; k ++) {if (. mat [I] [k]) for (Int J = 0; j <n; j ++) {T. mat [I] [J] + =. mat [I] [k] * B. mat [k] [J]; T. mat [I] [J] % = mod ;}} return t;} A quickp (int K) {A P = D, M; memset (M. mat, 0, sizeof (M. mat); For (INT I = 0; I <n; ++ I) // unit matrix {M. mat [I] [I] = 1 ;}while (k) {If (K & 1) M = MUL (m, p); P = MUL (P, P ); k> = 1;} return m;} int main () {n = 2; int K, t ;__ int64 x, y, z; while (scanf ("% i64d % i64d", & X, & Y )! = EOF) {int S = 0; scanf ("% i64d", & Z); mod = 1000000007; If (Z = 1) {If (x <0) printf ("% i64d \ n", x + mod); else printf ("% i64d \ n", x); continue;} D. mat [0] [1] =-1; D. mat [1] [1] = 0; D. mat [0] [0] = D. mat [1] [0] = 1; A ret = quickp (Z-2); // Number of Z-2 multiplication _ int64 ans = (Ret. mat [0] [0] * Y % mod + ret. mat [0] [1] * x % mod) % MOD; If (ANS <0) ans + = MOD; printf ("% i64d \ n", ANS );} return 0 ;}


Codeforces round #258 (Div. 2) B. jzzhu and sequences (matrix fast power)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.