Description
On a straight line of n planets, there are planets I in the X=i, and Planet J is subjected to the force of the planet I, when and only as I<=aj. The size of the Force J is fi->j=
mi*mj/(J-i) where a is a very small constant, it is intuitively said that each planet is only affected by distant planets. Please calculate the force of each planet
, as long as the relative error of the result is not more than 5%.
Input
The first line is two integers n and a. 1<=n<=10^5.0.01< A < = 0.35, next n lines enter the mass Mi of n planets, guaranteeing 0<=mi<=10^7
Output
n rows, sequentially outputting the forces of each planet
Sample Input5 0.3
3
5
6
2
4Sample Output0.000000
0.000000
0.000000
1.968750
2.976000HINT
The exact result should be 0 0 0 2 3, but the result error of the sample output is not more than 5%, also
SourceSolution
According to test instructions simulation, the complexity is $o (an^2) $, but there seems to be no optimization method
The breach is "the error does not exceed 5%" this sentence
Make $i=\lfloor{aj}\rfloor$
For Planet $j$, we're asking for $\displaystyle\sum_{k=1}^{i}\frac{m_k*m_j}{j-k}$.
When the $j$ is very big, $i $ is not too big, we can bashi into $\displaystyle\sum_{k=1}^{i}\frac{m_k*m_j}{j-0.5i}$
Using $sum_i$ to represent the quality of the former $i$ planets, then the formula can be turned into a $\displaystyle\frac{sum_i*m_j}{j-0.5i}$
So that we can use less than one variable $k$, the complexity is reduced to $o (n) $
1#include <bits/stdc++.h>2 using namespacestd;3 Doublem[100005], sum[100005];4 intMain ()5 {6 intN;7 DoubleA, ans;8scanf"%D%LF", &n, &a);9 for(intj =1; J <= N; ++j)Ten { One inti = (int) (A * j + 1e-8); Ascanf"%LF", M +j); -Ans =0; - if(J <= -) the for(intK =1; K <= i; ++k) -Ans + = m[k] * M[j]/(J-k); - Else -Ans = sum[i] * M[j]/(J-i/2); +printf"%f\n", ans); -SUM[J] = sum[j-1] +M[j]; + } A return 0; at}
View Code
[BZOJ1011] [HNOI2008] a distant planet