Description
Programming contests became so popular in the year 2397 that the governor of New Earck -- the largest human-inhabited planet of the galaxy -- opened a special Alley of Contestant Memories (ACM) at the local graveyard. The ACM encircles a green park, and holds the holographic statues of famous contestants placed equidistantly(等距的) along the park perimeter(周长). The alley has to be renewed from time to time when a new group of memorials arrives.
When new memorials are added, the exact place for each can be selected arbitrarily along the ACM, but the equidistant disposition(布置) must be maintained by moving some of the old statues along the alley.
Surprisingly, humans are still quite superstitious in 24th century: the graveyard keepers believe the holograms are holding dead people souls, and thus always try to renew the ACM with minimal possible movements of existing statues (besides, the holographic equipment is very heavy). Statues are moved along the park perimeter. Your work is to find a renewal plan which minimizes the sum of travel distances of all statues. Installation of a new hologram adds no distance penalty, so choose the places for newcomers wisely!
Input
The input file contains several test cases, each of them consists of a a line that contains two integer(整数)numbers: n -- the number of holographic statues initially located at the ACM, and m -- the number of statues to be added (2![$ \le$](https://icpcarchive.ecs.baylor.edu/external/37/3708img1.png)
![$ \le$](https://icpcarchive.ecs.baylor.edu/external/37/3708img1.png)
![$ \le$](https://icpcarchive.ecs.baylor.edu/external/37/3708img1.png)
![$ \le$](https://icpcarchive.ecs.baylor.edu/external/37/3708img1.png)
Output
For each test case, write to the output a line with a single real number -- the minimal sum of travel distances of all statues (in feet). The answer must be precise(精确) to at least 4 digits after decimal point(小数点) .![\epsfbox{p3708.eps}](https://icpcarchive.ecs.baylor.edu/external/37/p3708.jpg)
Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.
Sample Input
2 1 2 3 3 1 10 10
Sample Output
1666.6667
1000.0
1666.6667
0.0
我们假设原来的一个雕塑不动,并把它当作坐标原点,为了简化求解,把周长缩短为n+m。这样加入新雕塑后所有雕塑的新位置就是整数了。原来雕塑的位置此时可以表示成:i*(n+m)/n 只要我们把原来的雕塑移动到离它们最近的新位置处,就可以使总的移动距离最小。而我们注意到缩小周长之后,离旧位置较近的位置就是旧位置四舍五入之后的数。如3.6四舍五入后为4,而3.6离4比3近。四舍五入可以表示为:fioor(pos+0.5)最后不要忘记将结果放大回真实大小。代码如下:
#include<cstdio>
#include<cmath>
double N[1002]; //注意是double类型的数组
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
double s=0;
for(int i=1;i<n;i++)
{
N[i]=(double)i*(n+m)/n; //强制转换成double
s+=fabs(N[i]-floor(N[i]+0.5));
}
printf("%.4f\n",s*10000/(n+m)); //将数放大
}
return 0;
}