POJ3154(Graveyard)

Graveyard

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1452   Accepted: 751   Special Judge

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

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

Output

Write a single real number to the output file — the minimal sum of travel distances of all statues (in feet). The answer must be precise to at least 4 digits after decimal point.

Sample Input

sample input #1
2 1

sample input #2
2 3

sample input #3
3 1

sample input #4
10 10
2 1

sample input #2
2 3

sample input #3
3 1

sample input #4
10 10

Sample Output

sample output #1
1666.6667

sample output #2
1000.0

sample output #3
1666.6667

sample output #4
0.0
1666.6667

sample output #2
1000.0

sample output #3
1666.6667

sample output #4
0.0

Hint

Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.

题意:一个圆上原先均匀分布着n个点(可移动),现要加入新的m个点,使(n+m)个点仍然均匀分布在圆上,m个点可任意加在任意位置,求重新均匀分布时,原来n个点的移动距离的和的最小值。

这是lrj大白书上的一道题目(P9)。

思路:

首先,要推理出两点:

1.对每个旧点(n个点)的原来位置,总有至少一个离它最近的新的点(n+m个点),且必存在任意相邻旧点之间的最近新点并不相同的情况(假设与旧点匹配的最近新点只有一个,若有2个,则任意取一个)。

2.若移动距离最小,可以等价看成新旧点必有一点重合。

根据第二点,将圆从任意一点拆开成一条线段,设线段长度为lcm(n,n+m)(最小公倍数)。

之后几种方法都基于以上。

方法一:

对每个旧点两边扫描,扫到最新的新点,记录距离。最后累加距离,输出结果即可。

方法二:

可以发现,若(n+m)和n不互质,则线段上新旧点的分布情况,会有重复的情况,且重复了(n+m)和n的最大公约数次,即gcd(n+m,n)次,那么我们可以只考虑第一次的重复的情况,即可以直接将(n+m)和n除以最大公约数;

且当(n+m)和n互质时,新旧点的最近距离在0--(n-1)/2(n为奇数),或0--n/2(n为偶数)变化。且n为奇数时,每个数值等价出现二次,n为偶数时,n/2只出现一次,其余数值二次。那么只要累加数值即可。

注意变色部分,可以参考以下规律:

若a,b互质,设lcm(a,b) = k1*a = k2*b,则k*a%b(1<=k<=k1)的值域范围为:0--b-1,且每个数值有且仅出现一次。

#include<cstdio>
int gcd(int a, int b){return b == 0 ? a : gcd(b, a%b);}
int main(){
    int n, m;
    while(~scanf("%d %d",&n, &m)){
          int t = gcd(n, n+m);
          m += n;
          n /= t; m /= t;
          int lcm = n*m, ans = 0, tmp = (n-1)/2;
          ans = (1+tmp)*tmp;
          if((n-1)%2) ans += tmp + 1;
          printf("%.4f\n",(double)ans/lcm*10000);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mad_boys/article/details/48198509
POJ