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 sequencea1, a2, ..., 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.
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
3 2 3 5
22 3
Consider 6 possible routes:
- [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
- [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
- [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
- [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
- [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
- [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is =
=
.
题意:
在一条数轴上有n个点,以一定次序去访问这些点共需要走sum的距离,求n个点所有排列次序的sum之和与n的比值。
思路:
又是全排列问题。
n个点自然有n!种排列,一个个来必然TL;
于是分析可知:
sum1:第一个点确定后有(n - 1)!种排列,而第一个点与原点间距离为|ai - 0| = ai,所以第一步距离为所有ai * (n - 1)!之和;
sum2:而后考虑任取两个点后有(n - 2)! 种排列,这两个点有(n - 1)个位置可插入,所以后面n - 1步距离之和为 所有|ai - aj| * (n - 1)* (n - 2)!之和;
上两步除以n!种排列得到(sum1 + 2*sum2)/n;//|ai - aj|出现两次
而后可解。
/*
*Author : Flint_x
*Created Time : 2015-03-01 23:35:03
*File name : e.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
lint gcd(lint a,lint b){
if(a > b) return gcd(b, a);
if(b % a == 0) return a;
return gcd(b % a, a);
}
lint a[200005];
int main(){
lint n;
lint sum1 = 0,sum2 = 0;
while(cin >> n){
for( int i = 1 ; i <= n ; i++ ){
cin >> a[i];
sum1 += a[i];
}
sort(a + 1 , a + 1 + n);
for( int i = 1 ; i <= n ; i++){
sum2 += (4*i - 2*n - 2) * a[i];
}
lint sum = sum1 + sum2;
lint gys = gcd(sum , n);//求公约数
cout << sum / gys << ' ' << n / gys << endl;
}
return 0;
}