HDU 1568 Fibonacci (number theory)

Source: Internet
Author: User

Fibonacci
Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3654 Accepted Submission (s): 1671


Problem Description2007 year has come. After 2006 years of cultivation, mathematical prodigy Zouyu finally put 0 to 100000000 of Fibonacci series
The values (f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2] (i>=2) are all backed down.
Next, Codestar decided to test him, so each asked him a number, he will say the answer, but some numbers are too long. So the provision of more than 4 people as long as the first 4 can be said, but Codestar himself cannot remember. So he decided to write a program to test whether Zouyu said it was correct.
Input inputs several digits n (0 <= n <= 100000000), one row per digit. Read the end of the file.
Output output F[N] The first 4 digits (if less than 4 digits, all outputs).
Sample Input
012345353637383940

Sample Output
011235922714932415390863241023



Analysis: Number theory, IQ is not enough, the online great God of the arrangement of ideas briefly.

The formula for the Fibonacci sequence is used.

First look at the properties of logarithms, Loga (b^c) =c*loga (b), Loga (b*c) =loga (b) +loga (c);
Suppose given a number of 10234432, then log10 (10234432) =log10 (1.0234432*10^7) =log10 (1.0234432) +7;

LOG10 (1.0234432) is the small part of log10 (10234432).

LOG10 (1.0234432) =0.010063744
10^0.010063744=1.023443198
Then it's obvious to take a few.
First take the logarithm (to 10), and then get the result of the decimal part Bit,pow (10.0,bit) Later if the answer is still <1000 then always multiply by 10.
Note that I first processed the 0~20 item for ease of handling ~

This question takes the formula of the sequence: an= (1/√5) * [((1+√5)/2) ^n-((1-√5)/2) ^n] (n=1,2,3 ...)


The logarithm is taken out

log10 (AN) =-0.5*log10 (5.0) + ((Double) n) *log (f)/log (10.0) +log10 (+ ((1-√5)/(1+√5)) ^n) where f= (sqrt (5.0) +1.0)/2.0;
log10 ((1-√5)/(1+√5) ^n)->0
so it can be written as log10 (AN) =-0.5*log10 (5.0) + ((Double) n) *log (f)/log (10.0);
finally take its fractional part.




AC Code:

#include <iostream> #include <cstdio> #include <cmath> #include <algorithm>using namespace std; int f[21] = {0, 1, 1};int Main () {//    freopen ("In.txt", "R", stdin); int n;for (int i = 2; i <; ++i) f[i] = f[i-1] + F[i-2];while (scanf ("%d", &n)! = EOF) {if (n <=) {printf ("%d\n", F[n]); continue;} Else{double temp = -0.5 * LOG (5.0)/log (10.0) + ((double) n) * Log ((sqrt (5.0) +1.0)/2.0)/log (10.0); temp-= Floor (temp); te MP = POW (10.0, temp), while (temp < n) temp *= 10;printf ("%d\n", (int) temp);}} return 0;}


Woo-woo ~ ~ ~ Number number problem, really bad math, flawed ah ...



HDU 1568 Fibonacci (number theory)

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.