Codeforces 340C Tourist Problem【思维】

C. Tourist Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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.

The average travel distance is = = .


题目大意:


给你N个数的序列(1~N),首先我们写出他的全排列,然后统计值tot:Σ|Ai-Ai-1|;

然后求出tot/n!的最简化,输出分子和分母。


思路:


很显然,我们现在有一个序列:

A1,A2,A3.

对于所有值的和,其中一项:|Ai-Aj|来讲,其出现的次数为(n-1)*(n-2)*(n-3)*............==(n-1)!

假设对于样例来说,|A3-A2|出现了两次,|A2-A3|也出现了两次,|A1-A3|同样出现了两次..............................


那么对应所求值:

Σ|Ai-Aj|*(n-1)!/n!=Σ|Ai-Aj|/n


那么我们再考虑求Σ|Ai-Aj|.


很显然我们对原序列从小到大排序对结果没有影响。

那么求结果的时候分成两类计算即可。

那么对于|Ai-Aj|来说,我们可以O(n)遍历序列,找到位子i,那么在位子i前边的都是比a【i】小的,后边的都是比a【i】大的。

那么对应后边的贡献值为:sum[n]-sum[i]-a[i]*(n-(i+1)+1);

那么对应前边的贡献值为:a[i]*(i-1)-sum[i-1];

这里sum【i】表示区间【1,i】的前缀和。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll __int64
ll a[150000];
ll sum[150000];
ll gcd(ll x,ll y)
{
    if(x%y==0)return y;
    else return gcd(y,x%y);
}
int cmp(ll a,ll b)
{
    return a<b;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
        sort(a+1,a+1+n,cmp);
        ll output=0;
        for(int i=1;i<=n;i++)
        {
            sum[i]=sum[i-1]+a[i];
        }
        for(int i=1;i<=n;i++)
        {
            output+=a[i]*(i-1)-sum[i-1];
            output+=sum[n]-sum[i]-a[i]*(n-(i+1)+1);
        }
        output+=sum[n];
        printf("%I64d %I64d\n",output/gcd(output,n),n/gcd(output,n));
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值