Tourist Problem CodeForces - 340C (组合数学)

探讨旅行者Iahub在随机排列的路径中,从原点出发访问n个目的地后停止,所走总距离的数学期望。通过分析所有可能路径,计算并输出最简分数形式的平均距离。

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)!)  ×  \sum \left |ai-ai+1 \right |  然后,对于每一个数和起点0的距离,也有(n-1)!种,这一部分的和是     ((n-1)!)\times \sum ai   ,所以加起来就是

((n-1)!)\times \sum ai+  ((n-1)!)  × \sum \left |ai-ai+1 \right |

然后一共有n!种,化简完了是(\sum ai+\sum \left |ai-ai+1 \right |)/  n;

 

 

 

求任意两个相邻的数的差的时候可以看上图,,1-2,出现了1×4次,2-3出现了2×3次,3-4出现了3×2次,4-5出现了4×1次

所以找出公式为(n-i)×i;

以下为代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值