Title Link: BZOJ-2048
Problem analysis
When there is only one book, the weight of the book falls on the edge of the table, and the length of the desktop is 1/2.
When there are two books, the center of gravity of the first book falls on the edge of the second book, the center of the two books on the edge of the table, the center of the two books is at the bottom of the book at the right end of 1/4. So stretch out 1/2 + 1/4.
Three books, you can stretch more than 3 of the book's center of Gravity 1/6.
The continuation of the calculation can find the law, I book The center of Gravity falls on the bottom of the book at the right end of the 1/2i.
Then the total length we ask for is sigma (1/2i) = Sigma (1/i)/2.
Sigma (1/2i) is the sum of harmonic series, if n is relatively small, we will direct O (n), because n is relatively small when the limit formula to find the error will be larger.
If n is large, we use the limit formula of the harmonic series Sigma (1/i) (i = 1 to n) = ln (n + 1) + R. R is Euler constant, r = 0.5772156649015328 ...
And then it's done.
Code
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath > #include <algorithm>using namespace std;typedef double lf;typedef long long LL; #define R 0.5772156649const LF Ep s = 1e-10; LL N, M; LF Ans;int Main () {scanf ("%lld%lld", &n, &m), if (n <= 1000000ll) {for (int i = 1; I <= n; ++i) Ans + = 1.0/(LF ) I;} else Ans = log ((LF) (n + 1)) + R; Ans/= 2.0; Ans *= (LF) m;printf ("%d\n", (int) (ans-eps)); return 0;}
[Bzoj 2048] [2009 Countries Training Team] book stack "harmonic series"