HDU 3117 Fibonacci numbers (onacci matrix acceleration recursion + formula)

Source: Internet
Author: User
Tags log10 1

The question is very simple: calculate the number of the n-th ononacci. If the number of the first four and the last four are output in the middle of the Eight-bit output..., otherwise, the number of Fibonacci is directly output.

The last four digits are very good. The result of the direct matrix acceleration recursion for the remainder of 10000 is.

The first four searched: http://blog.csdn.net/xieqinghuang/article/details/7789908

The generic formula of the Fibonacci. For, the number of Fibonacci has a generic formula --

F (n) = 1/SQRT (5) (1 + SQRT (5)/2) ^ N + (1-SQRT (5)/2) ^ N)

If f [N] can be expressed as T * 10 ^ K (T is a decimal number), then for F [N] to retrieve the logarithm log10, the answer is log10 T + K, in this case, it is obvious that log10 T <1, so we get log10 t by removing the integer,
Then we use POW (10, log10 T) to restore T. The first four digits of F [N] are obtained by dividing t x 1000. In actual implementation, log10 f [N] is approximately equal to (1 + SQRT (5)/2) ^ N/SQRT (5). Here we put (1-SQRT (5 )) /2) ^ N is ignored,
Because when n> = 40, this number is small and can be ignored. Therefore, log10 f [N] can be reduced to log10 1/SQRT (5) + N * log10 (1 + SQRT (5)/2

You can obtain the number of Fibonacci By finding the number of pairs.

In addition, the last four digits in a loop section will be repeated once if no 15000 exist.

Fibonacci numbers Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1882 accepted submission (s): 738


Problem descriptionthe Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, which t for the first two elements F0 and F1 which are respectively zero and one.

What is the numerical value of the nth Maid number?
Inputfor each test case, a line will contain an integer I between 0 and 108 degrees sively, for which you must compute the ith fig number fi. fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis ("... ").

There is no special way to denote the end of the input, simply stop when the standard input terminates (after the EOF ).
 
Sample Input
0123453536373839406465
 
Sample output
0112359227465149303522415781739088169632459861023...41551061...77231716...7565
 
# Include <algorithm> # include <iostream> # include <stdlib. h> # include <string. h> # include <iomanip> # include <stdio. h> # include <string> # include <queue> # include <cmath> # include <stack> # include <map> # include <set> # define EPS 1e-10 ///# define M 1000100 # define ll _ int64 // # define ll long // # define INF 0x7ffffff # define INF 0x3f3f3f # define PI 3.1415926535898 # define zero (X) (FABS (x) <EPS )? 0: X) // # define mod 9973int MOD; const int maxn = 2010; using namespace STD; struct matrix {int f [110] [110];}; matrix MUL (matrix A, matrix B, int N) // matrix multiplication {matrix C; memset (C. f, 0, sizeof (C. f); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {for (int K = 0; k <n; k ++) C. f [I] [J] + =. f [I] [k] * B. f [k] [J]; C. f [I] [J] % = MOD;} return C;} matrix pow_mod (matrix A, int B, int N) // matrix fast power {matrix S; memset (S. f, 0, sizeof (S. f); For (INT I = 0; I <n; I ++) s. f [I] [I] = 1; while (B) {If (B & 1) S = MUL (s, A, n); A = MUL (a,, n); B> = 1;} return s;} matrix add (matrix A, matrix B, int N) // matrix addition {matrix C; for (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {C. f [I] [J] =. f [I] [J] + B. f [I] [J]; C. f [I] [J] % = MOD;} return C;} ll num [maxn]; int main () {num [0] = 0ll; num [1] = 1ll; For (INT I = 2; I <= 40; I ++) num [I] = num [I-1] + num [I-2]; moD = 10000; int N; while (CIN> N) {If (n <40) {cout <num [N] <Endl; continue;} double ans; ans =-0.5*(log10 (5.0) + N * log10 (SQRT (5.0) + 1.0)/2); ANS-= (INT) ans; ans = POW (10, ANS); While (ANS <1000) ans * = 10; printf ("% d", (INT) ans); cout <"... "; matrix C; memset (C. f, 0, sizeof (C. f); C. f [0] [0] = 1; C. f [0] [1] = 1; C. f [1] [0] = 1; matrix D; D = pow_mod (C, N-2, 2); int sum = 0; sum + = D. f [0] [0] + D. f [0] [1]; sum % = MOD; If (sum <10) cout <"000"; else if (sum <100) cout <"00 "; else if (sum <1000) cout <"0"; cout <sum <Endl;} return 0 ;}


HDU 3117 Fibonacci numbers (onacci matrix acceleration recursion + formula)

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.