HDU 1568 formula of Fibonacci sequence and its thinking skills

Source: Internet
Author: User
Tags pow
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 so to take a few is very obvious bar ~ First take the logarithm (to 10), and then get the result of the number of parts Bit,pow (10.0,bit)
If the answer is still <1000 then always multiply by 10.

Note that I first processed the 0~20 item is to facilitate processing ~ This problem to use the formula of the series: an= (1/√5) * [((1+√5)/2) ^n-((1-√5)/2) ^n] (n=1,2,3 ...)
Take the logarithm log10 (AN) =-0.5*log10 (5.0) + ((double) n) *log (f)/log (10.0) +LOG10 (n ((1-√5)/(1+√5)) ^n) where f= (sqrt (5.0) +1.0)/2.0;
LOG10 ((1-√5)/(1+√5) ^n)->0 so can be written log10 (AN) =-0.5*log10 (5.0) + ((double) n) *log (f)/log (10.0);

Finally take its fractional part.
#include <iostream> #include <cmath> using namespace std;
int fac[21]={0,1,1};
Const double f= (sqrt (5.0) +1.0)/2.0;
    int main () {double bit;
    int n,i;
            for (i=3;i<=20;i++) fac[i]=fac[i-1]+fac[i-2];//the first 20 items while (cin>>n) {if (n<=20) {
            cout<<fac[n]<<endl;
        Continue } bit=-0.5*log (5.0)/log (10.0) + ((double) n) *log (f)/log (10.0);//ignores the last infinitesimal bit=bit-floor (bit);
        Bit=pow (10.0,bit);
        while (bit<1000) bit=bit*10.0;
    printf ("%d\n", (int) bit);
} 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.