先上题目:

A - Tourist Problem

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

Input
3
2 3 5
Output
22 3

  题意:给你一维上的一些点,坐标用自然数表示,起点为0,根据不同的遍历顺序遍历这些点(这些点不重复,遍历是指到达这个点,经过它并不算),点与点之间的距离是两点坐标的绝对值,一条路径的长度等于根据某个顺序遍历完所有点所走的距离。求出所有路径的长度之和,然后根据这个和求出总的平均长度,并用最简分数表示。
  假设有n个点,那么n个点的全排列是n!,然后先忽略点之间的距离(只考虑它们的先后顺序),考虑某2个点在这n!种排列中相邻出现的次数,那就是(n-1)!*2次(考虑上这两个点的顺序),然后考虑上距离那么这n个点相互到达的总距离就是Σ(|ai-aj|*2*(n-1)!),但是,还需要考虑以不同点作为第一个遍历的点的时候从0到达这个点所走的距离这和,对于某一个点一共有(n-1)!种情况是它作为开头第一个点,那走过的距离就是ai*(n-1)!这么多种,n个点就是Σai*(n-1)!。
  所以总的所走距离就是Sum = Σ(|ai-aj|*2*(n-1)!)+Σ(ai*(n-1)!),AVG = Sum/n!约分以后得到:                                   (Σ|ai-aj|*2+Σai)/n   由于n最大可以达到100000 所以时间复杂度要小于O(n^2),分析如下:

  这是枚举不同的两个点的连线情况(上面的数字是下标),然后发现规律s'=s-2*i+1+n,s'是当前这个位置到还没有连线的位置的连线数目,s是上一个位置的连线数目。因为这是不考虑两个点的顺序的,所以对s'求和以后还要乘以2,再加上Σai就等于分子,然后分子和分母n都除以gcd(分子,分母)就得到正确答案了。

上代码:
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define LL long long
using namespace std; LL a[MAX];
LL sum,s,S; bool cmp(LL u,LL v){
return u<v;
} LL gcd(LL e,LL f){
return f== ? e : gcd(f,e%f);
} int main()
{
int n;
//freopen("data.txt","r",stdin);
scanf("%d",&n);
sum=;
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
S+=a[i];
}
//cout<<S<<endl;
sort(a+,a+n+,cmp);
s=;
for(int i=;i<n;i++){
s=s-*i++n;
sum+=s*(a[i+]-a[i]);
}
//cout<<sum<<endl;
sum<<=;
S+=sum;
//cout<<S<<endl;
LL g = gcd(S,n);
//cout<<g<<endl;
printf("%I64d %I64d\n",S/g,n/g);
return ;
}

340C

CodeForces - 340 C - Tourist Problem的更多相关文章

  1. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...

  2. Codeforces Round #198 (Div. 2) C. Tourist Problem

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

  3. Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)

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

  4. C. Tourist Problem

    http://codeforces.com/problemset/problem/340/C 赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容 ...

  5. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  6. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  7. Codeforces 442B Andrey and Problem(贪婪)

    题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...

  8. CodeForces 867B Save the problem

    B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...

  9. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

随机推荐

  1. uboot 解压缩

    在uboot中进行解压缩是非常实用的 uboot中完毕delay 用户进行交互的段 if(BootType == '3') { char *argv[3]; printf("   \n3: ...

  2. 803E

    dp dp[i][j]表示到了i赢和输的差为j 如果这位是?向dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1]转移,如果是W向dp[i-1][j-1]转移,如果是L向dp[i- ...

  3. 希尔shell排序——java实现

    希尔排序是对插入排序的优化,将插入排序的交换步长由1增加到h. 希尔排序的思想是使数组中任意间隔为h的元素有序.步长调幅为h = 3*h + 1, 也就是1,4,13,40,121,364, 1003 ...

  4. Prism.Interactivity 之 PopupWindowAction 用法简记

    PopupWindow通过InteractionRequestTrigger(EventTrigger的派生类)侦听目标对象(InteractionRequest<T>类型)的Raised ...

  5. hibernate_04_hbm.xml介绍

    先贴上类文件Students.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC & ...

  6. c# ado.net eftity framework 返回多表查询结果

    public static IQueryable GetWeiXinTuWenList() { using (var Model = new Model.WeiXinEntities()) { var ...

  7. java就业前景发展方向分析

    随着信息化的发展,IT培训受倒了越来越多人的追捧.在开发领域,JAVA培训成为了许多人的首选!java拥有强大的开发者的数量已超过了之前的900万,将近97%的企业电脑也在运行着java,其下载量每年 ...

  8. ubuntu 14.04安装x11VNC

    环境:Ubuntu 14.04, 1)安装x11vnc: sudo apt-get install x11vnc 2)设置VNC的连接密码: x11vnc -storepasswd Enter VNC ...

  9. window phone 8 开发准备工作(一)

    一.下载安装Window phone SDK 1.Windows Phone SDK 8.0下载 http://www.microsoft.com/ZH-CN/download/details.asp ...

  10. token的问题汇总

    token的作用:认证.授权: 生成:随机码.时间戳.用户 设备 合成: 验证:是否存在.合成验证: 管理:有效期(服务器存储时间or cookie存储过期时间).展期. token生成:或者和用户信 ...