Codeforces Round #198 (Div. 2) C. Tourist Problem - 找规律

来源:互联网 发布:淘宝网网购流程图 编辑:程序博客网 时间:2024/05/21 06:56

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 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 test(s)
input
32 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  =  = .


PS:虽说,喵呜(六银大神)写过了这道题目的解题报告,但是本菜还是要写这道题目的解题报告。因为这道题目虽说比赛的时候没有做出来,但是刚好在今天吃午饭的时候突然灵感迸发,想到了属于自己的解法。O(∩_∩)O~

题目大意:

在数轴上给定n个点,求从0开始走完所有这些点平均的路程。

题目分析:

给定n个点,那么有n!种走法,而且(2 ≤ n ≤ 105) 。显然不可能硬算。那么只有可能想到化简,将n!化简为O(n)的数量级最好。因为最后一步是除法,按道理是可以化简的。

这里CF的note是解题的关键

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

观察note可以得到下面的规律(不妨先设点的坐标依次为a1,a2,···,an)

  • |ai-0| 有(n-1)!个
  • 对于ai,那么(ai-a1+ai-a2 + ··· +ai-a(i-1))有(n-1)! * 2个
所以可以得到下面的结论:

化简既可以得到最后的表达式,时间复杂度为O(n)。
但是要注意两个问题:
1)点的坐标不是按照大小给的,所以要先排序
2)注意在乘法的时候不要溢出(对于int来说)
下面是代码:
#include <stdio.h>#include <algorithm>using namespace std;#define maxn 100100int a[maxn];long long gcd(long long a,long long b){    return b == 0 ? a : gcd(b,a%b);}int main(){    int n;    long long sum1 = 0;    long long sum2 = 0;    long long sum3 = 0;    scanf("%d",&n);    for(int i = 1; i <= n; i++) scanf("%d",&a[i]);    sort(a+1,a+1+n);    for(int i = 1; i <= n; i++)    {        sum3 += sum1;        sum1 += a[i];        sum2 += (long long)(i-1) * a[i];    }    //printf("%I64d %I64d %I64d\n",sum1,sum2,sum3);    long long up = sum1 + (sum2  - sum3)*2;    long long down = n;    long long m = gcd(up,down);    printf("%I64d %I64d\n",up/m,down/m);    return 0;}

PS:果然,一般题目可能会暗示解题的方向...

原创粉丝点击