Bzoj 2048 2009 mathematical algorithm of the National Training Team

Source: Internet
Author: User

The typical physical problem of having a pile of books on the table, the junior high school physics teacher once said, but only remembered the conclusion... It doesn't matter. Just prove it.

First, let's set the length from top to bottom of the I book to a [I], and the prefix is s [I]. so what we need is s [N].

To simplify the problem, we set the length of a book to 1.

Assume n = 1


A [1] = 1/2, no doubt

Then consider two books


When the two books are written, the center of gravity is obviously 3/4 from the left endpoint of the book below, so a [2] = 1-3/4 = 1/4

Okay, I know! The first is 1/2, the second is 1/4, and the third is 1/8!

I think this is just an example.

We are considering three books.


How is this calculated?

First, calculate the distance between the center of gravity of the three books and the left endpoint of the third book.

The focus of the first book is 1/4 + 1/2 + 1/2

The focus of the second book is 1/4 + 1/2

The focus of the Third Book is 1/2

So we can get the center of gravity of the three books (1/4*2 + 1/2 + 1/2*3)/3 = 1/2*2/3 + 1/2

So a [3] = 1-(1/2*2/3 + 1/2) = 1/2-1/2*2/3 = 1/2 * (1-2/3) = 1/2*1/3 = 1/6

I don't need to talk about the four books.


The distance from the center of gravity of the four books to the left endpoint of the fourth book

The center of gravity of the first book is: 1/6 + 1/4 + 1/2 + 1/2

The second book focuses on: 1/6 + 1/4 + 1/2

The center of gravity of the third book is: 1/6 + 1/2

The center of gravity of the fourth book is: 1/2

So the focus of the four books is (1/6*3 + 1/4*2 + 1/2 + 1/2*4)/4 = 1/2*3/4 + 1/2

A [4] = 1-(1/2*3/4 + 1/2) = 1/2-1/2*3/4 = 1/2 * (1-3/4) = 1/2*1/4 = 1/8

And so on, we have a [I] = 1/2I

So how can we sum this thing? N <= 10 ^ 18, O (n) is certainly unrealistic.

We consider the limit formula of harmonic series.

Lim (n-> + ∞) 1/1 + 1/2 + 1/3 +... + 1/N = ln (n + 1) + R

Here, R is an approximate value of Euler's constant, which is about 0.57721566490153286060651209.

But this is the limit formula, and the error will be relatively large for n cases. So when n is relatively small, O (n) brute-force solution N is relatively large, apply the formula.

In the demarcation line theory, O (n) can be used for about million queries, but the accuracy of this question is too low, so 1 million queries can be used for 0 ms.

Then the water passes... Accidentally put down the code and rank1...

In addition, 100% was supplemented.

#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define eps 1e-10#define r 0.5772156649using namespace std;typedef long long ll;ll n,m;double ans;int main(){int i;cin>>n>>m;if(n<=10000)for(i=1;i<=n;i++)ans+=0.5/i;elseans=log(n+1.0)+r,ans/=2.0;ans*=m;printf("%d\n",(int)(ans-eps) );}


Bzoj 2048 2009 mathematical algorithm of the National Training Team

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.