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;
}

最新文章

  1. Android中查找一个Layout中指定的子控件
  2. oracle修改sys用户密码
  3. linux:/etc/rc.local 不能自动启动问题
  4. 【你吐吧c#每日学习】11.10 C# Data Type conversion
  5. Javascript 基础--数组
  6. mysql_upgrade命令
  7. iOS语言国际化
  8. Kinetic使用注意点--animation
  9. RPM包制作最简单样例
  10. javascript 中 undefined 和 null 区别
  11. Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same entity解决方法
  12. c#中如何跨线程调用windows窗体控件?
  13. Xcode 断点无用,也不打印输出
  14. spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
  15. C#一例绘制字体不清晰的解决办法
  16. UICollectionViewController的用法1
  17. openFileDialog的使用
  18. 【手记】解决“未能创建 SSL/TLS 安全通道”异常
  19. Delphi: 模态窗体最小化
  20. [置顶] js 实现 &lt;input type=&quot;file&quot; /&gt; 文件上传

热门文章

  1. Android学习笔记(十五)——碎片的生命周期(附源代码)
  2. JSP 9 大内置对象详解
  3. 设计模式 - 适配器模式(adapter pattern) 具体解释
  4. ORACLE 使用RMAN管理归档日志 archived log
  5. 编译和安装shibboleth-sp遇到的问题
  6. js 特效 手风琴效果
  7. [Swust OJ 767]--将军回家(Dijkstra算法)
  8. JavaSE学习总结第09天_面向对象4
  9. poj 2480 Longge&#39;s problem
  10. 迪杰斯特拉算法c语言实现