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 sequencea1, 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.

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

题意:求出所有走的方案的平均花费。。

思路:先找出规律。我们发现,num中每两个组合都会出现(n - 1)!次,而分母是n!次,所以抵消,分母为n,分子为求出每个数字和其他数字(以及0)的差的绝对值之和即可,直接暴力枚举为O(n^2)会超时。所以这样:先把num从小到大排序,然后算出总和,然后用一个now来记录第i个前i个和,然后总和为num[i] * i - now (i的前半部分) + sum - now -num[i] * (n - i) - num[i](后半部分),这样一来时间复杂度只有O(n)。

要注意的是要用longlong。由于当时编译器只有渣渣vc6.0所以就用__int64了。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int main() {
__int64 sum = 0, now = 0, ans = 0, num[100005], n, i;
num[0] = 0;
scanf("%I64d", &n);
for (i = 1; i <= n; i ++) {
scanf("%I64d", &num[i]);
sum += num[i];
}
sort(num, num + 1 + n);
for (i = 1; i <= n; i ++) {
ans += 2 * num[i] * i - 2 * now + sum - num[i] * (n + 1);
now += num[i];
}
__int64 a, b;
a = ans; b = n;
if (a < b) {
__int64 t = b;
b = a;
a = t;
}
while (b) {
__int64 sb = b;
b = a % b;
a = sb;
}
printf("%I64d %d\n", ans / a, n / a);
return 0;
}

Codeforces Round #198 (Div. 2) 340C的更多相关文章

  1. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  2. [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)

    http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...

  3. Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理

    题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...

  4. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  5. Codeforces Round #198 (Div. 2)

    A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...

  6. Codeforces Round #198 (Div. 1) B,C 动态规划

    比赛时,开了大号去做,算了半天发现不会做A,囧.于是跑去看B,发现很水?于是很快敲完了,但是A不会,没敢交.于是去看C,一直找规律啊,后来总算调了出来,看了一下榜,发现还是算了吧,直接去睡觉了.第二天 ...

  7. Codeforces Round #198 (Div. 2) —— D

    昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...

  8. Codeforces Round #198 (Div. 2) —— C

    C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...

  9. Codeforces Round #198 (Div. 2) —— B

    B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...

随机推荐

  1. 七日筑基——C#第一天(上)

    从今天开始,介绍C#如何入门,其实我们学习任何语言的目标都是为了最终能把自己想像的东西做出来,所以在最开始学习的时候要定准方向,很多人在学习过程中学着学着就变味了.比如我之前有个学生,最开始学习编程的 ...

  2. jsp 有哪些内置对象?作用分别是什么? 分别有什么方 法?

    JSP共有以下9种基本内置组件 request对象          客户端请求,此请求包含来自GET/POST的请求参数,通过它才能了解到客户的需求,然后做出相应. response对象       ...

  3. MSSQL2005 修改数据库的排序规则

    1.修改数据库排序规则ALTER DATABASE [DataBaseName] COLLATE Chinese_PRC_CI_AS ; 2.修改表中列的排序规则 如果下列其中之一当前正在引用一个列, ...

  4. new Date()的参数

    前两天发现手机页面的倒计时在Android上正常显示,在iPhone却不能显示. 后来又发现在ff和ie里也不显示.(以前只在chrome里看过,显示正常). 后来同事改了new Date()里字符串 ...

  5. 10.PHP 教程_PHP If...Else 语句

    条件语句用于根据不同条件执行不同动作. PHP 条件语句 当您编写代码时,您常常需要为不同的判断执行不同的动作.您可以在代码中使用条件语句来完成此任务. 在 PHP 中,提供了下列条件语句: if 语 ...

  6. Nexus 5 电信破解问题 CDMA_HDR重启会变回LTE

    解决方法是Nexus 5 Field Test Mode -Advanced LTE Settings 关掉LTE 重启就好了 在Android 5.0 下实测成功 如果不行就换一张卡 重新写 重启再 ...

  7. HTTP使用BASIC认证的原理及实现方法 (转载)

    转自:http://blog.itpub.net/23071790/viewspace-709367 一.   BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许 ...

  8. 韦根(Wiegand)数据传输格式

    韦根数据传输使用TTL电平,有两条数据线,分别称为DATA0和DATA1.无数据传输时,两条线都是高电平,当传输“1”时,DATA0为高,DATA1为低:当传输“0”时,DATA0为低,DATA1为高 ...

  9. MCE遥控---用遥控器玩电脑

    实现功能:利用Vista/Windows7的Media Center或者iMCE的支持,配上电脑遥控器,就可以在电视上用遥控器玩电脑,看高清.听音乐.看照片.录电视等.遥控器比鼠标操作起来更加自然,家 ...

  10. lwp 模拟行锁堵塞 前端超时

    jrhmpt01:/root/async# cat a2.pl use LWP::UserAgent; use utf8; use DBI; use POSIX; use HTTP::Date qw( ...