http://codeforces.com/problemset/problem/340/C

赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容易受外界影响

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

/*
分析:由si产生的值可以分为以下两种情况
1.对于si来说,si放到最左端构成(n-1)!个排列,由si构成的总和为si*(n-1)!,有n个数
2.对于si来说,si放到sj的左端,则每一次的位置有(n-2)!个排列,有n-1个位置可放,由si构成的总和为|si-sj|*(n-1)!,有n个数
综合1,2可得总和为:
si*(n-1)!*n/((n-1)!*n) + |si-sj|*(n-1)!*n/((n-1)!*n) = ( si+|si-sj| )/n;//i=1...n,j=1...n
最后的难点就是如何去计算|si-sj|,由于si-sj不确定正负,所以可以事先对s进行排序,然后拆开|si-sj|得到:
s1-s1 + s2-s1 + s3-s1 + s4-s1 + s5-s1 +...+ sn-s1
+
s2-s1 + s2-s2 + s3-s2 + s4-s2 + s5-s2 +...+ sn-s2
+
s3-s1 + s3-s2 +s3-s3 + s4-s3 + s5-s3 +...+ sn-s3
...
=i*si-sum[i]+(sum[n]-sum[i])-(n-i)*si=sum[n]-2*sum[i]+(2*i-n)*si;//sum[i]表示前i个数的和
其中2*sum[i]的总和为2(n-i+1)*si;//i=1...n
最终推出ans+=(4*i-2*n-1)*si
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#include<cmath>
#include<iomanip>
#define INF 999999999
using namespace std; const int MAX=100000+10;
__int64 s[MAX],sum,n; int main(){
while(cin>>n){
sum=0;
for(int i=1;i<=n;++i)cin>>s[i];
sort(s+1,s+n+1);
for(int i=1;i<=n;++i){
sum+=(4*i-2*n-1)*s[i];
}
__int64 gcd=__gcd(sum,n);
cout<<sum/gcd<<' '<<n/gcd<<endl;
}
return 0;
}

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. CodeForces - 340 C - Tourist Problem

    先上题目: A - Tourist Problem Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  5. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  6. cf C. Tourist Problem

    http://codeforces.com/contest/340/problem/C #include <cstdio> #include <cstring> #includ ...

  7. C. Tourist Problem 2021.3.29 晚vj拉题 cf 1600 纯数学题

    拉题链接  https://vjudge.net/contest/430219#overview 原题链接  https://codeforces.com/problemset/problem/340 ...

  8. codeforces 340C Tourist Problem(简单数学题)

    题意:固定起点是0,给出一个序列表示n个点,所有点都在一条直线上,其中每个元素代表了从起点到这个点所走的距离.已知路过某个点不算到达这个点,则从起点出发,到达所有点的方案有许多种.求所有方案走的总路程 ...

  9. XIII Open Cup named after E.V. Pankratiev. GP of Saratov

    A. Box Game 注意到局面总数不超过$50000$,而且每次操作都会改变石子的奇偶性,因此按奇偶可以将状态建成二分图,然后求出最大匹配. 如果状态数是偶数,那么先手必胜,策略就是每次走匹配边, ...

随机推荐

  1. ubuntu下mysql安装与测试

    原文地址: http://www.cnblogs.com/zhuyp1015/p/3561470.html 注意:原文地址中,最后g++ 编译源代码时少了个字母.添上即可. ubuntu上安装mysq ...

  2. MVC5框架解析之MvcHandler

    从MvcHandler开始 首选MvcHandler显示实现了IHttpHandler接口中的void ProcessRequest(HttpContext context); 外层逻辑: 1.方法参 ...

  3. webform的三级联动

    webform的三级联动 与winform一样,只不过需把DropDownList的AutoPostBack属性改为True. *简单日期的编写方法:用是三个DropDownList分别代表年月日,用 ...

  4. Swift 类和结构体的简单认识

    类和结构体的共同点: 定义属性用于存储值 定义方法用于提供功能 定义附属脚本用于访问值 通过拓展增加默认实现的功能 定义构造器用于生成初始化值 实现协议以提供某种标准功能 类是引用类型 结构体是值类型 ...

  5. JS匿名执行函数

    一.匿名函数的创建 第一种:(调用sum后可执行) var sum=function(x,y){ return x+y; } 第二种:(可自执行) void function(x,y){ }(1,2) ...

  6. 关于Java(常用数据类型)

    工作中,除非特殊需要,一般使用的数据类型较为单一. int int 是最常用的类型之一,一般能满足判断或循环的需求 float 或 double 两个浮点类型,可以在一定程度上确保数据的精度 BigD ...

  7. bzoj 1006: [HNOI2008]神奇的国度 弦图的染色问题&&弦图的完美消除序列

    1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1788  Solved: 775[Submit][Stat ...

  8. bzoj 1005: [HNOI2008]明明的烦恼 prufer编号&&生成树计数

    1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2248  Solved: 898[Submit][Statu ...

  9. [BZOJ 2186] [Sdoi2008] 沙拉公主的困惑 【欧拉函数】

    题目链接:BZOJ - 2186 题目分析 题目要求出 [1, n!] 中有多少数与 m! 互质.(m <= n) 那么在 [1, m!] 中有 phi(m!) 个数与 m! 互质,如果一个数 ...

  10. 【POJ3208】 (DP)

    Apocalypse Someday Description The number 666 is considered to be the occult “number of the beast” a ...