CodeForces 340C Tourist Problem(组合排列,公式推导)

题目要求计算在一条直线上有n个目的地的旅行中,按照所有可能的顺序访问每个目的地一次的平均行走距离。目的地的位置由一串非负整数表示,且不允许经过但不访问。输出平均行走距离的分子和分母,确保分数是最简形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 sequencea1a2, ..., 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 a1a2, ..., 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.

Sample test(s)
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个点,以一定次序去访问这些点共需要走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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值