http://codeforces.com/problemset/problem/340/C
赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容易受外界影响
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 =
=
.
/* 分析:由si产生的值可以分为以下两种情况 1.对于si来说,si放到最左端构成(n-1)!个排列,由si构成的总和为si*(n-1)!,有n个数 2.对于si来说,si放到sj的左端,则每一次的位置有(n-2)!个排列,有n-1个位置可放,由si构成的总和为|si-sj|*(n-1)!,有n个数 综合1,2可得总和为: si*(n-1)!*n/((n-1)!*n) + |si-sj|*(n-1)!*n/((n-1)!*n) = ( si+|si-sj| )/n;//i=1...n,j=1...n 最后的难点就是如何去计算|si-sj|,由于si-sj不确定正负,所以可以事先对s进行排序,然后拆开|si-sj|得到: s1-s1 + s2-s1 + s3-s1 + s4-s1 + s5-s1 +...+ sn-s1 + s2-s1 + s2-s2 + s3-s2 + s4-s2 + s5-s2 +...+ sn-s2 + s3-s1 + s3-s2 +s3-s3 + s4-s3 + s5-s3 +...+ sn-s3 ... =i*si-sum[i]+(sum[n]-sum[i])-(n-i)*si=sum[n]-2*sum[i]+(2*i-n)*si;//sum[i]表示前i个数的和 其中2*sum[i]的总和为2(n-i+1)*si;//i=1...n 最终推出ans+=(4*i-2*n-1)*si */ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<iomanip> #include<cmath> #include<iomanip> #define INF 999999999 using namespace std; const int MAX=100000+10; __int64 s[MAX],sum,n; int main(){ while(cin>>n){ sum=0; for(int i=1;i<=n;++i)cin>>s[i]; sort(s+1,s+n+1); for(int i=1;i<=n;++i){ sum+=(4*i-2*n-1)*s[i]; } __int64 gcd=__gcd(sum,n); cout<<sum/gcd<<' '<<n/gcd<<endl; } return 0; }