CodeForces - 340 C - Tourist Problem

先上题目:

A - Tourist Problem
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequencea1a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.

Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.

The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.

Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.

Input

The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1a2, ..., an (1 ≤ ai ≤ 107).

Output

Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.

Sample Input

Input
3
2 3 5
Output
22 3

  题意:给你一维上的一些点,坐标用自然数表示,起点为0,根据不同的遍历顺序遍历这些点(这些点不重复,遍历是指到达这个点,经过它并不算),点与点之间的距离是两点坐标的绝对值,一条路径的长度等于根据某个顺序遍历完所有点所走的距离。求出所有路径的长度之和,然后根据这个和求出总的平均长度,并用最简分数表示。
  假设有n个点,那么n个点的全排列是n!,然后先忽略点之间的距离(只考虑它们的先后顺序),考虑某2个点在这n!种排列中相邻出现的次数,那就是(n-1)!*2次(考虑上这两个点的顺序),然后考虑上距离那么这n个点相互到达的总距离就是Σ(|ai-aj|*2*(n-1)!),但是,还需要考虑以不同点作为第一个遍历的点的时候从0到达这个点所走的距离这和,对于某一个点一共有(n-1)!种情况是它作为开头第一个点,那走过的距离就是ai*(n-1)!这么多种,n个点就是Σai*(n-1)!。
  所以总的所走距离就是Sum = Σ(|ai-aj|*2*(n-1)!)+Σ(ai*(n-1)!),AVG = Sum/n!约分以后得到:

                                  (Σ|ai-aj|*2+Σai)/n

  由于n最大可以达到100000 所以时间复杂度要小于O(n^2),分析如下:
  这是枚举不同的两个点的连线情况(上面的数字是下标),然后发现规律s'=s-2*i+1+n,s'是当前这个位置到还没有连线的位置的连线数目,s是上一个位置的连线数目。因为这是不考虑两个点的顺序的,所以对s'求和以后还要乘以2,再加上Σai就等于分子,然后分子和分母n都除以gcd(分子,分母)就得到正确答案了。





上代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #define MAX 100010
 6 #define LL long long
 7 using namespace std;
 8 
 9 LL a[MAX];
10 LL sum,s,S;
11 
12 bool cmp(LL u,LL v){
13     return u<v;
14 }
15 
16 LL gcd(LL e,LL f){
17     return f==0 ? e : gcd(f,e%f);
18 }
19 
20 int main()
21 {
22     int n;
23     //freopen("data.txt","r",stdin);
24     scanf("%d",&n);
25     sum=0;
26     for(int i=1;i<=n;i++){
27         scanf("%I64d",&a[i]);
28         S+=a[i];
29     }
30     //cout<<S<<endl;
31     sort(a+1,a+n+1,cmp);
32     s=0;
33     for(int i=1;i<n;i++){
34         s=s-2*i+1+n;
35         sum+=s*(a[i+1]-a[i]);
36     }
37     //cout<<sum<<endl;
38     sum<<=1;
39     S+=sum;
40     //cout<<S<<endl;
41     LL g = gcd(S,n);
42     //cout<<g<<endl;
43     printf("%I64d %I64d\n",S/g,n/g);
44     return 0;
45 }
340C

 

posted @ 2014-01-16 12:05  海拉鲁的林克  阅读(333)  评论(0编辑  收藏  举报