Hihocoder 1164 Random Fibonacci

Source: Internet
Author: User

Portal: Random Fibonacci

#1164: Random Fibonacci Time limit:5000msSingle Point time limit:1000msMemory Limit:256MBDescribe

You must be familiar with the Fibonacci sequence:

a0 = 1, a1 = 1, ai = ai-1 + ai-2, (i > 1).

Now consider the following generated Fibonacci sequence:

a0 = 1, ai = AJ + ak, i > 0, J, K are randomly selected from integers [0, I-1] (independent of J and K).

Now given n, it is required to find E (an), that is, the expected value of an in various possible a sequences.

Input

An integer n of one line representing the nth item. (1<=n<=500)

Output

A real number on a line that represents the answer. The absolute or relative error of your output and answer is considered the correct answer if it is less than 10-6.

Sample explanation

Coexistence in 3 possible sequences

1,2,2 1/4

1/2

1,2,4 1/4

Therefore the expectation is 3.

Sample input
2
Sample output
3.000000

Analysis: This problem should pay special attention to J and K Independent of this condition, under which we can get E (an) (hereinafter abbreviated to E[N]) an expression
E[n] = 2*s[n-1]/n,
Where SN is defined as
S[n] = e[0] + e[1] + e[2] + .... + E[n]
I'll start with the two formulas above to launch e[n] about n expressions.
By
E[n] = S[n]-s[n-1]
And
E[n] = 2 * S[n-1]/n
Eliminate s[n] to get
E[n] = 2 * S[n-1]/n
That
n * E[n] = 2 * s[n-1]
Thus there are also
(n+1) * E[n+1] = 2 * S[n]
Combine the above two and the first type to get
(n+1) * E[n+1]-n * e[n] = 2 * E[n]
i.e.
E[N+1]/e[n] = (n+2)/(n+1)
And then get
E[n] = (n+1) e[0] = n+1
    

 

Hihocoder 1164 Random Fibonacci

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.