Codeforces round #257 (Div. 2) Fast Power of B Matrix

Source: Internet
Author: User

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



Question

A simple question of the progressive method, I used the Matrix to quickly power it.

The constructor is also simple.

F1 + F3 = F2

F3 = F2-F1

F3 = 1 * F2 +-1 * F1

F2 = 1 * F2 + 0 * F1

So the matrix is

1,-1

1, 0

Then the modulo operation and negative number processing are completed. Negative modulo returns a negative number. Because I am writing a fast power, the number returned at last must be between (-mod, MoD), so I just need to add a mod directly and then take the modulo again.

Sample Code
/*************************************** **************************************** Copyright Notice * copyright (c) 2014 All Rights Reserved * ---- stay hungry Stay Foolish ---- ** @ Author: Shen * @ name: B * @ file: G: \ My source code \ [ACM] competition \ 0719-CF \ B. CPP * @ Date: 2014/07/19 20:57 * @ algorithm: matrix fast power method ************************************ **************************************** **/# include <CST Dio >#include <string >#include <cstring >#include <iostream >#include <algorithm> using namespace STD; typedef long int64; const int maxn = 2; const int maxm = 2; const int mod = 1000000007; struct matrax {int n, m; int64 mat [maxn] [maxm]; matrax (): n (-1 ), M (-1) {} matrax (INT _ n, int _ m): n (_ n), m (_ m) {memset (MAT, 0, sizeof (MAT);} void unit (INT _ s) {n = _ s; M = _ s; For (INT I = 0; I <n; I ++) {for (in T j = 0; j <n; j ++) {mat [I] [J] = (I = J )? 1: 0 ;}}void print () {printf ("n = % d, M = % d \ n", n, m); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) printf ("% 8d", mat [I] [J]); printf ("\ n") ;}}; matrax add_mod (const matrax & A, const matrax & B, const int64 mod) {matrax ans (. n,. m); For (INT I = 0; I <. n; I ++) {for (Int J = 0; j <. m; j ++) {ans. mat [I] [J] = (. mat [I] [J] + B. mat [I] [J]) % mod ;}return ans ;}matrax MUL (const matrax & A, const matrax & B) {matrax ans (. n, B. m); For (INT I = 0; I <. n; I ++) {for (Int J = 0; j <B. m; j ++) {int64 TMP = 0; For (int K = 0; k <. m; k ++) {TMP + =. mat [I] [k] * B. mat [k] [J];} ans. mat [I] [J] = TMP;} return ans;} matrax mul_mod (const matrax & A, const matrax & B, const int mod) {matrax ans (. n, B. m); For (INT I = 0; I <. n; I ++) {for (Int J = 0; j <B. m; j ++) {int64 TMP = 0; For (int K = 0; k <. m; k ++) {TMP + = (. mat [I] [k] * B. mat [k] [J]) % MOD;} ans. mat [I] [J] = TMP % MOD;} return ans;} matrax pow_mod (const matrax & A, int64 K, const int mod) {matrax P (. n,. m), ANS (. n,. m); P = A; ans. unit (. n); If (k = 0) return ans; else if (k = 1) return a; else {While (k) {If (K & 1) {ans = mul_mod (ANS, P, MoD); k --;} else {k/= 2; P = mul_mod (p, p, MoD );}} return ans ;}} int64 X, Y, N, Res; void solve () {CIN >>> x >>> y >> N; If (n = 1) res = x; else if (n = 2) RES = y; else {matrax ans (2, 1); // TMP = CEF ^ (n-2 ); // ans = TMP * beg; // res = ans. mat [0] [0]; matrax CEF (2, 2); CEF. mat [0] [0] = 1; CEF. mat [0] [1] =-1; CEF. mat [1] [0] = 1; CEF. mat [1] [1] = 0; // CEF. print (); matrax beg (2, 1); beg. mat [0] [0] = y; beg. mat [1] [0] = x; matrax TMP (2, 2); TMP = pow_mod (CEF, N-2, MoD); // TMP. print (); ans = mul_mod (TMP, beg, MoD); // ans. print (); Res = ans. mat [0] [0];} If (RES <0) RES + = MOD; cout <res <Endl;} int main () {solve (); return 0 ;}


Codeforces round #257 (Div. 2) Fast Power of B Matrix

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.