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 sequence a1, 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.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., 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.
中文提示
给出数轴上n个地点的坐标a1,...,an,随机的给出一个排列,按照该排列去一个个到达排列对应的地点,初始在原点处,问所走距离的期望值
第一行一整数n,之后输入n个整数ai(2≤n≤105,1≤ai≤107)ai(2≤n≤105,1≤ai≤107)
输出距离期望值的最简分数表示
Input
3
2 3 5
Output
22 3
Note
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.
solution:
对于一个序列,排列的总数一共有n!种,然后对于任意两个相邻的数 ai和ai+1 在这n!个排列里,出现的次数是
(n-1)!个,这一部分的和就是 ((n-1)!) × 然后,对于每一个数和起点0的距离,也有(n-1)!种,这一部分的和是 ((n-1)!) ,所以加起来就是
((n-1)!)+ ((n-1)!) ×
然后一共有n!种,化简完了是(+)/ n;
求任意两个相邻的数的差的时候可以看上图,,1-2,出现了1×4次,2-3出现了2×3次,3-4出现了3×2次,4-5出现了4×1次
所以找出公式为(n-i)×i;
以下为代码:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
ll gcd(ll a,ll b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
ll a[maxn];
int main()
{
int n;
scanf("%d",&n);
ll ans=0,res=0,s;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
ans+=a[i];
}
sort(a+1,a+1+n);
for(int i=1;i<=n-1;i++)
{
s=(ll)(n-i)*i;
res+=(s*abs(a[i]-a[i+1]));
}
res<<=1;
ans+=res;
ll g=gcd(ans,n);
printf("%lld %lld\n",ans/g,n/g);
return 0;
}