Derivation of the pure mathematical method of "Noip pre-Test"--traveler problem

Source: Internet
Author: User
Tags greatest common divisor

First, write in front

This problem seems to be an original topic (not Bo Master original), so it can not be evaluated on any OJ, bloggers uploaded data on the network disk (network address: http://pan.baidu.com/s/1mibdMXi), you crossing need to pick up. In addition, Bo master use this question and did not get the title of the person authorized, if the topic people see this blog and believe that under the infringement of your rights, please use the message in the station and the next contact, the next will immediately delete this blog, to bring you the confusion of the place please understand.

Bloggers upload this problem is mainly because this problem involves many mathematical operations, the derivation process is more complex, but did not use any algorithm or mathematical theorem, it can be said that this is a model of the idea problem. This blog is introduced in the method for Bo Master original, reproduced please indicate the source.

Second, the topic title Description

Lahub is a fan of the traveler who wants to be a real traveler, so he plans to start a trip. Lahub wanted to visit n destinations (all on a straight line). Lahub began his travels at the beginning. The distance between the first destination and the starting point is ai km (AI is a nonnegative integer). There are no two destinations and the same distance from the starting point.

The journey from the I destination to the first J destination is |ai-aj| km. We call the order of visits to n destinations a "trip". Lahub can visit any order he wants to visit, but each destination has and can only be visited once (the order of visits is N).

Lahub writes all possible "travels" on a piece of paper and notes the distance each "trip" is going to take. He is interested in the average value of the sum of all "travel" distances. But he thought the calculation was too dull, so he asked you for help.

Input

The first line is a positive integer n.

The second row n non-negative integers a1,a2,...., an (1≤ai≤10^7).

Output

Two integers, the answer is output in the simplest fractional form, the first is the numerator and the second is the denominator.

Sample input

3

2 3 5

Sample output

22 3

Sample Hints

There are 6 possible travel examples:

[2, 3, 5]: |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 answer is 1/6 * (5+7+7+8+9+8) =44/6=22/3

Data range

30% n<=10

50% n<=1000

100% n<=100000

Third, the problem analysis

First of all, we may as well put aside the topic background, the topic is roughly translated into a mathematical problem:

A positive integer n is given, and the A1 and A2......an are given a total of n distinct positive integers. For each permutation of the sequence an ax1, AX2......AXN, the sx=|ax1-0|+|ax2-ax1|+......+|axn-axn-1|, the average of S.

As the topic clearly stated, when I≠j, there is no ai=aj situation. Then, the total number of permutations of n is the full permutation of the n! species.

For ease of interpretation, we stipulate that the sequence an is strictly ascending (only need to do a sort processing when the code is implemented)

Through the definition of s we can not find that, if the AI is not considered as a AXN case, for each I, in each calculation of S, AI will appear as Minuend and meiosis. In addition, because when I=XN, AI in the calculation does not appear as a meiosis, and for each I,I=XN case has (n-1)! species, so for each i,ai a total as minuend appears n! times, as the meiosis appears [n!-(n-1)!] Times.

Let's talk about AI as a minuend occurrence:

When AI as Minuend, the number of 0 and the rest of the n-1 can be used as the AI's meiosis, and their chances of becoming AI are equal (Ai dew equitably?? So the number of N, including 0, will be used as the AI's (n-1)! times. Set AJ to the AI's meiosis, then when and only when J<i, |ai-aj| to the absolute value of the symbol will get ai-aj, conversely, when and only when J>i, |ai-aj| to the absolute value of the symbol will be aj-ai. Since when I is legal, 0 is always smaller than AI, so we can get the number of I so that the AI goes to the absolute value symbol with a plus sign, the number of n-i so that the AI goes to the absolute symbol with a minus sign.

Above, we get the following: The effect of AI as Minuend on Σs is:

Similarly, we discuss AI as a condition of meiosis:

When the AI as a meiosis, the remaining number of n-1 can be used as Ai minuend, and they become AI minuend chance is equal (AI dew equitably + 1) so the remaining number of n-1 each will be as the AI's meiosis (n-1)! (think, why). Set AJ as the AI's minuend, so ditto, when and only when J<i, |aj-ai| to the absolute value of the symbol will be ai-aj, conversely, when and only when J>i, |aj-ai| to the absolute value of the symbol will be aj-ai. So we can get the i-1 number so that the AI goes to the absolute value symbol with a plus sign, the number of n-i so that the AI goes to the absolute sign with a minus sign.

Above, we get the following: The effect of AI as a meiosis on Σs is:

In both cases, we get: the effect of AI on Σs is:

Finally, we only need to combine the effects of each AI on σs to get the answer:

Note: 1, remember to simplify the answer to the simplest fraction after the output (numerator, denominator divided by its greatest common divisor);

    2, do not open long long see ancestors, ten years Oi an empty.

Iv. Implementation of the Code
1#include <stdio.h>2#include <algorithm>3 using namespacestd;4 Const intmaxn=100010;5 Long LongA[MAXN];6 Long LonggcdLong LongXintN)7 {8     Long Longxx=x,yy=N;9      while(yy)Ten     { One         Long Longt=xx%yy; Axx=yy; -yy=T; -     } the     returnxx; - } - intMain () - { +Freopen ("tourist.in","R", stdin); -Freopen ("Tourist.out","W", stdout); +     intN; A     Long Longx=0; atscanf"%d",&n); -     inti; -      for(i=1; i<=n;++i) -scanf"%d",&a[i]); -Sort (A +1, A +1+n); -      for(i=1; i<=n;++i) inx=x+a[i]* (4*i-2*n-1); -     Long Longg=gcd (x,n); toprintf"%lld", x/g); +printf"%d\n", n/g); - fclose (stdin); the fclose (stdout); *     return 0; $}
Traveler Questions

Weakly weak to say, the Konjac Konjac code word is not easy, reproduced please indicate the source http://www.cnblogs.com/Maki-Nishikino/p/5994679.html

Derivation of the pure mathematical method of "Noip pre-Test"--traveler problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.