HDOJ 4549 M Fibonacci series ferma small theorem + rapid matrix power, hdoj4549

Source: Internet
Author: User

HDOJ 4549 M Fibonacci series ferma small theorem + rapid matrix power, hdoj4549


MF (I) = a ^ fib (I-1) * B ^ fib (I) (I> = 3)

Mod 1000000007 is a prime number. According to Fermat's small Theorem a ^ phi (p) = 1 (mod p) Here p is a prime number and a is smaller than p so a ^ (p-1) = 1 (mod p)

Therefore, a ^ k % p = a ^ (k % P-1) % p can be reduced for a large index.

Use the Matrix to quickly calculate the fib number.

M Fibonacci Series Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission (s): 1672 Accepted Submission (s): 482


Problem DescriptionM Fibonacci Series F [n] is an integer series. Its definition is as follows:

F [0] =
F [1] = B
F [n] = F [n-1] * F [N-2] (n> 1)

Can you find the value of F [n] For a, B, n?
The Input contains multiple groups of test data;
Each group of data occupies one row and contains three integers a, B, n (0 <= a, B, n <= 10 ^ 9)
Output outputs an integer F [n] for each group of test data. Since F [n] may be very large, you only need to Output the value after mod of F [n] To 1000000007, output a row of data in each group.
Sample Input
0 1 06 10 2
 
Sample Output
060
 
Source2013 Jinshan xishanju creative game program challenge-Preliminary Round (2)


/*************************************** * ******** Author: CKbossCreated Time:, Thursday, March 12, 2015 File Name: HDOJ4549.cpp *************************************** * ********/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> using namespace std; typedef long int LL; const LL mod = limit 7ll; const LL md = limit 6ll; // getfibLL a, B, n; struct Matrix {Matrix (LL a = 0, LL B = 0, LL c = 0, LL d = 0) {m [0] [0] = a; m [0] [1] = B; m [1] [0] = c; m [1] [1] = d;} LL m [2] [2] ;}; Matrix MUI (Matrix &, matrix & B) {Matrix ret; ret. m [0] [0] = (. m [0] [0] * B. m [0] [0]) % md + (. m [0] [1] * B. m [1] [0]) % md; ret. m [0] [1] = (. m [0] [0] * B. m [0] [1]) % md + (. m [0] [1] * B. m [1] [1]) % md; ret. m [1] [0] = (. m [1] [0] * B. m [0] [0]) % md + (. m [1] [1] * B. m [1] [0]) % md; ret. m [1] [1] = (. m [1] [0] * B. m [0] [1]) % md + (. m [1] [1] * B. m [1] [1]) % md; return ret;} Matrix QUICKPOW (LL m) {Matrix E (,); Matrix, 1, 0); while (m) {if (m & 1LL) E = MUI (E, A); A = MUI (A, A); m/= 2LL ;} return E;} void showMat (Matrix M) {cout <endl; for (int I = 0; I <2; I ++) {for (int j = 0; j <2; j ++) cout <M. m [I] [j] <","; cout <endl ;}cout <endl ;}// get p_th fib numberLL getfib (LL p) {p --; Matrix M1 = QUICKPOW (p); return M1.m [0] [0];} LL QUICKPOW2 (LL a, LL x) {LL e = 1LL; while (x) {if (x & 1LL) e = (e * a) % mod; a = (a * a) % mod; x/= 2LL ;} return e;} LL solve () {if (n = 0) return a; else if (n = 1) return B; else if (n = 2) return (a * B) % mod; // fib coefficient of a-> fib (n-1) LL xa = getfib (n-1); LL partA = QUICKPOW2 (a, xa ); /// fib coefficient of B-> fib (I) LL xb = getfib (n); LL partB = QUICKPOW2 (B, xb); return (partA * partB) % mod;} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout ); while (cin> a> B> n) cout <solve () <endl; return 0 ;}


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.