Graveyard

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$n$ \le$1000, 1$ \le$m$ \le$1000)   . The length of the alley along the park perimeter is exactly 10 000 feet.

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}

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值