CF340C Tourist Problem

题目描述

Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are nn destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The nn destinations are described by a non-negative integers sequence a_{1}a1​ , a_{2}a2​ , ..., a_{n}an​ . The number a_{k}ak​ represents that the kk th destination is at distance a_{k}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 xx and next destination, located at kilometer yy , is |x-y|∣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 nn 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 nn ( 2<=n<=10^{5}2<=n<=105 ). Next line contains nn distinct integers a_{1}a1​ , a_{2}a2​ , ..., a_{n}an​ ( 1<=a_{i}<=10^{7}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.

题意翻译

给定一条直线上n个点的坐标a_1,a_2,...a_na1​,a2​,...an​,一条路线从原点开始,经过aa的一个排列并在最后一个点结束(也就是说不返回原点)。一条路径的长度定义为排列中相邻两点x,yx,y的距离|x-y|∣x−y∣(包括原点),求所有路线的平均长度,要求化为最简分数。

输入输出样例

输入 #1复制

3
2 3 5

输出 #1复制

22 3

题意:有n个地点。

每个地点离原点的距离是a[i]。

任意两个地点i,j的距离是|a[i]-a[j]|。

求所有走过每个地点且只经过一次的路线的平均值(最简分母和分子)。

思路:经过每个点的所有路线的条数是n的全排列(n!)。

因为我们在算的时候是两个地点挨在一起的绝对值,所以不妨就令大的值减去小的值。

那这一步我们就可以从大到小将a数组排序,然后我们再观察所有路线。

拿例题举例:

2 3 5

全排列之后的所有可能方法是

2 3 5:(2-0)+(3-2)+(5-3)

2 5 3:(2-0)+(5-2)+(5-3)

3 2 5:(3-0)+(3-2)+(5-2)

3 5 2:(3-0)+(5-3)+(5-2)

5 2 3:(5-0)+(5-2)+(3-2)

5 3 2:(5-0)+(5-3)+(3-2)

然后我们可以观察出来与3相关的运算:

(3-2)(3-2) (3-2)(3-2)(5-3)(5-3)(5-3)(5-3)(3-0)(3-0)

(3-0)的情况就是用3开头的时候有几种情况,那么就是除了3剩下的数全排列即2!

那么3就加了2!次,即(n-1)!次。

再分析3和数组中其他数的相减情况:

可以观察出他可以和数组中每个数都能相邻

下标从1开始。

且和每一个数相邻的情况一共有A22*A22种(先挑一个数和他捆起来,然后内部全排列是A22个,之后把捆起来的看成一个整体,再让这个整体来全排列),用通式就是2*(n-1)!

然后我们再看他和每个数相邻是怎么计算的

我们把数组从大到小排序:5 3 2

他可以是和5挨着也可以是和2挨着,当和5挨着的时候就是5-3,当和2挨着的时候就是3-2

那么总结的来说就是他前面的数减去他,他后面的数加上他

所以他自己被加了(n-i)次,被减了(i-1)次。

那么我们就可以得出a[i]在总的运算来说,参与运算的柿子是:

a[i]*(n-1)!+2*(n-1)!*(a[i]*(n-i)-a[i]*(i-1))

那么总数就是从1~n的所有a[i]*(n-1)!+2*(n-1)!*(a[i]*(n-1)-a[i]*(i-1))相加

化简一下:(n-1)!*(a[i]+2*(a[i]*(n-i) )

然后我们要求总数除以路线条数

路线条数是n!

我们先化简一下就是(a[i]+2*(a[i]*(n-i))/n

然后我们就只需要算出i从1~n,(a[i]+2*(a[i]*(n-i))的值

然后输出他们各自除以他们的最大公约数了。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll N=1e5+7;
ll a[N];
ll n;
ll gcd(ll a,ll b){
	if(a<b)swap(a,b);
	if(b!=0){
		return gcd(b,a%b);
	}return a;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	cin>>n;
	ll sum=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	ll ans=0;
	sort(a+1,a+n+1,greater<int>());
	for(int i=1;i<=n;i++){
		ans+=(a[i]*(n-2*i+1));
	}
	ans*=2;
	ans+=sum;
	ll m=gcd(ans,n);
	cout<<ans/m<<" "<<n/m;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值