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

我们可以找出规律,答案就是(sum{pri[i]}+任意两点间的距离)/n,而任意两点间的距离我们不能用n*n来枚举,我们可以找出公式sum{(p[i]-p[i-1])*i*(n-i)*2};这样,排完序之后,就可以用线性时间内a掉了!

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define M 100500
__int64 pri[M];
__int64 gcd(__int64 a,__int64 b){
if(a==0)return b;
return gcd(b%a,a);
}
bool cmp(int a,int b){
return a<b;
}
int main()
{
__int64 n,sum,s,k,tempn;
int i,j;
while(scanf("%I64d",&n)!=EOF){
for(sum=0,s=0,i=0;i<n;i++){
scanf("%I64d",&pri[i]);
sum+=pri[i];
}
sort(pri,pri+n,cmp);
for(s=0,i=1;i<n;i++){
s+=(pri[i]-pri[i-1])*i*(n-i);
}
s*=2;
k=gcd(s+sum,n);
printf("%I64d %I64d\n",(s+sum)/k,n/k);
}
return 0;
}

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

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

  2. Codeforces Round #367 (Div. 2) C. Hard problem

    题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...

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

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

  4. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题

    Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...

  5. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  6. Codeforces Round #198 (Div. 2)

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

  7. Codeforces Round #198 (Div. 2) 340C

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

  8. Codeforces Round #198 (Div. 2)C,D题解

    接着是C,D的题解 C. Tourist Problem Iahub is a big fan of tourists. He wants to become a tourist himself, s ...

  9. Codeforces Round #198 (Div. 1 + Div. 2)

    A. The Wall 求下gcd即可. B. Maximal Area Quadrilateral 枚举对角线,根据叉积判断顺.逆时针方向构成的最大面积. 由于点坐标绝对值不超过1000,用int比 ...

随机推荐

  1. Struts2学习笔记(二) 使用通配符动态调用方法

    <package name="other" extends="struts-default"> <action name="xxx_ ...

  2. mrtg监控网络流量简单配置

    Mrtg服务器搭建(监控网络流量) [日期:2012-07-03] 来源:Linux社区  作者:split_two [字体:大 中 小]   [实验环境] 监控机:Red Hat linux 5.3 ...

  3. [C++ Basic] Const 用法

    定义: const 主要用于声明常量.当常量为对象时,对象值不可改变:当常量为指针时,该指针不可移动或重新赋值,但我们可以通过它去修改该指针的指向对象的值(前提是无需移动指针的修改).所谓的形参.返回 ...

  4. ++i和i++哪个效率高?

    这个问题需要分两种情况来解说: 1.当变量i的数据类型是c++语言默认提供的类型的话,他们的效率是一样的. int a,i=0;     a=++i;汇编代码如下: int a,i=0; 01221A ...

  5. epoll相关

    1) 能不能给一个使用epoll相关API进行IO监控的示例?在<<epoll学习笔记>>中有一个简单的示例说明epoll相关API的使用, 但是这个示例是非常简单的, 它仅仅 ...

  6. SQL中的Update、delete与inner join 联合使用

    Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,太神奇了. update的格式是 update ...

  7. 简单JSONP跨域请求

    JSONP原理:利用<script>标签的src属性实现跨域的请求.可在URL中提供回调函数的名字.后台进过处理后将数据以回调函数参数的形式返回. demo:JSONP请求不同端口的数据 ...

  8. codeforces 478B Random Teams

    codeforces   478B  Random Teams  解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...

  9. 比赛F-F Perpetuum Mobile

    比赛F-F     Perpetuum Mobile 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/ ...

  10. 转:sublime text快捷键 (很实用的东东)

    一个好的编辑器,能大大提高编程的效率.如果能熟知软件的快捷键,那更能让你得心印手.这些内容都是我网上和自己实际使用过程中所收集而来的,在网络上应该也算比较全面的了吧.欢迎大家补充,我也会在以后慢慢添加 ...