Tenth multi-school HDU 3936 fib query (the nature of the Fibonacci series and the ologn matrix acceleration Multiplication Algorithm)

Source: Internet
Author: User
Tags integer division

The general formula of the Fibonacci series is F (n) = (1/√ 5) * {[(1 + √ 5)/2] ^ (n + 1) -[(1-√ 5)/2] ^ (n + 1 )}

Nature:

 
1. F (0) + F (1) + F (2) +... + F (n) = f (n + 2)-1. 2. F (1) + f (3) + f (5) +... + F (2n-1) = f (2n ). 3. F (2) + f (4) + f (6) +... + F (2n) = f (2n + 1)-1. 4. [F (0)] ^ 2 + [F (1)] ^ 2 +... + [F (n)] ^ 2 = f (n) · F (n + 1 ). 5. F (0)-f (1) + F (2 )-... + (-1) ^ N · F (n) = (-1) ^ N · [F (n + 1)-f (n)] + 1. 6. F (m + N-1) = f (S-1) · F (n-1) + f (m) · F (n ). This can be usedProgramCompile a program whose time complexity is only O (log n. 7. [F (n)] ^ 2 = (-1) ^ (n-1) + f (n-1) · F (n + 1 ). 8. F (2n-1) = [F (n)] ^ 2-[F (n-2)] ^ 2. 9.3f (n) = f (n + 2) + f (n-2 ). 10. F (2n-2m-2) [F (2n) + f (2n + 2)] = f (2 m + 2) + f (4n-2m) [n> M ≥-1, n ≥ 1] 11.f( 2n + 1) = [F (n)] ^ 2 + [F (n + 1)] ^ 2.
 
Integer Division and prime generation of the Fibonacci seriesEach 3 numbers has only one divisible by 2. Each 4 numbers has only one divisible by 3. Each 5 numbers has only one divisible by 5, each 6 numbers has only one integer divided by 8, and each 7 numbers has only one integer divided by 13. Each 8 numbers has only one integer divided by 21, each 9 numbers have only one divisible by 34 ,....... we can see that 5th, 7, 11, 13, 17, and 23 are prime numbers: 5, 13, 89,233,159, 19th, 28657 (bits are not)
The number of digits in the Fibonacci sequence: a 60-step cycleListen 35, 83145, 94370,77415, 61785.38190, 99875,27965, 16730,31045, 49325,72910...

Question:

This question can be exploited in Nature 4 and 6

Define sum [I] = sigma (P [J]) 1 <= j <= I.

P [I] = f [2 * I-1] ^ 2 + F [2 * I] ^ 2.

According to formula 4, sum [I] = f [2 * I] * f [2 * I + 1].

Then sum [R]-sum L-1] can be obtained directly.

This is the first time that I used matrix concatenation to pre-process f [2 ^ N].

HDU ran 78 Ms 11th. It is estimated that the first 10 running 62 were all pasted with the same template.

# Include <cstdio> # include <cstring> typedef long ll; const ll mod = 1000000007; ll mtrx [60] [2] [2]; void debug (ll a [] [2]) {printf ("% LLD \ n", a [0] [0], A [0] [1], a [1] [0], a [1] [1]);} void pre_pro () {memset (mtrx, 0, sizeof (mtrx); mtrx [0] [0] [0] = 1; mtrx [0] [0] [1] = 1; mtrx [0] [1] [0] = 1; mtrx [0] [1] [1] = 0; For (int t = 0; t <60; ++ T) for (INT I = 0; I <2; ++ I) for (Int J = 0; j <2; ++ J) {for (int K = 0; k <2; ++ K) mtrx [t + 1] [I] [J] + = mtrx [T] [I] [k] * mtrx [T] [k] [J]; mtrx [t + 1] [I] [J] % = mod ;}} ll fib (LL A) {A --; ll mat [2] [2] = {1, 0, 0, 1}; ll TMP [2] [2]; for (INT p = 0; A >>= 1, ++ p) {If (! (A & 1) continue; TMP [0] [0] = mat [0] [0]; TMP [0] [1] = mat [0] [1]; TMP [1] [0] = mat [1] [0]; TMP [1] [1] = mat [1] [1]; memset (MAT, 0, sizeof (MAT); For (INT I = 0; I <2; ++ I) for (Int J = 0; j <2; ++ J) {for (int K = 0; k <2; ++ K) mat [I] [J] + = mtrx [p] [I] [k] * TMP [k] [J]; mat [I] [J] % = mod ;}// debug (MAT); Return mat [0] [0];} ll sum (LL) {if (a = 0) return 0; else return fib (2 * A) * fib (2 * A + 1) % MOD;} int main () {pre_pro (); int CAS; // freopen ("in. in "," r ", stdin); // freopen (" out.txt "," W ", stdout); // For (INT I = 0; I <20; ++ I) debug (mtrx [I]); scanf ("% d", & CAS); While (CAS --) {ll l, R; scanf ("% i64d % i64d", & L, & R); printf ("% d \ n", (sum (R)-sum L-1) + mod) % mod);} 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.