# POJ 3154 Graveyard【多解，数论，贪心】

## Graveyard

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 1707

Accepted: 860

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```

Sample Output

```sample output #1
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.

Source

Northeastern Europe 2006

然后我们沿着这个重合的点，将圆环剪开，注意首尾相同，颜色没有变哦。

你有看出什么东西吗？好吧，如果没有，那么请你看下一步，将AB‘，B’D , DC'，C'A线段（即所有的蓝色点间隔形成的线段）重合。

```for（int i=0;i<n;i++）
a[i] = i*k%n;
sort（a,a+n）;```

,

``` 1 #include <stdio.h>
2 #define min(a,b) (a)>(b)?(b):(a)
3 int gcd(int a,int b)
4 {
5     return b==0?a:gcd(b,a%b);
6 }
7 int main()
8 {
9     int n,m;
10     while(scanf("%d%d",&n,&m)!=EOF)
11     {
12         int temp=gcd(n,m);
13         n/=temp;
14         m/=temp;
15         int ans=0;
16         for(int i=1;i<n;i++)
17             ans+=min(i,n-i);
18         printf("%.4lf\n",(double)ans/(n*(n+m))*10000);
19     }
20     return 0;
21 }```

lrj解法(直接贪心，然后求最小距离(按比例缩小))：

``` 1 #include <stdio.h>
2 #include <math.h>
3 int main()
4 {
5     int n,m;
6     while(scanf("%d%d",&n,&m)!=EOF)
7     {
8         double ans=0.0;
9         for(int i=1;i<n;i++)
10         {
11             double pos=(double)i/n*(n+m);
12             ans+=fabs(pos-floor(pos+0.5))/(n+m);
13         }
14         printf("%.4lf\n",ans*10000);
15     }
16     return 0;
17 }```

780 篇文章62 人订阅

0 条评论

## 相关文章

### Metal入门教程（六）边界检测

Metal入门教程（一）图片绘制 Metal入门教程（二）三维变换 Metal入门教程（三）摄像头采集渲染 Metal入门教程（四）灰度计算 Metal入门教程...

2999

### 机器学习之线性代数

完整内容已上传到github：https://github.com/ZingP/machine-learning/tree/master/linear_al...

1761

1620

3736

### PyTorch(总)---PyTorch遇到令人迷人的BUG与记录

BUG1 在使用NLLLoss()激活函数时，NLLLoss用来做n类分类的，一般最后一层网络为LogSoftmax，如果其他的则需要使用CrossEntrop...

8678

4255

3270

3314

### Leetcode 39 Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique c...

2138

### 有基础（Pytorch/TensorFlow基础）mxnet+gluon快速入门mxnet基本数据结构mxnet的数据载入网络搭建模型训练准确率计算模型保存与载入

import numpy as np import mxnet as mx import logging logging.getLogger().setLeve...

9008