20161022 Noip Simulation Game T2 Problem Solving report

Source: Internet
Author: User
Tags gcd

Traveler questions

"Problem 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 Format"

The first line is a positive integer n.

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

"Output Format"

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

"Input Sample"

3

2 3 5

"Output Example"

22 3

the sample Tips "

There are 6 possible travel examples:

[2, 3, 5]: The distance to travel: |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

————————————— Split Line —————————————

Each point contributes to the answer:

Answer:

1#include"bits/stdc++.h"2 3 using namespacestd;4typedefLong LongQaq;5 Const intMAXN = 1e6 +1e3;6 7 Qaq GCD (Qaq x, Qaq y) {8          while(x ^= y ^= x ^= y%=x);9         returny;Ten } One inta[MAXN]; AInlineintINPUT () { -         intx =0, F =1;CharCH =GetChar (); -          while(Ch <'0'||'9'< CH) {if(ch = ='-') F =-1; CH =GetChar ();} the          while('0'<= CH && Ch <='9') {x = (x <<1) + (x <<3) + CH-'0'; CH =GetChar ();} -         returnX *F; - } -  + intMain () { - Qaq N; +N =INPUT (); A          for(intI=1; I<=n; ++i) { ata[I] =INPUT (); -         } -Qaq sum =0 ; -          -Stable_sort (A +1, A + N +1 ) ; -          in          for(intI=1; I<=n; ++i) { -Sum + = (QAQ) a[i] * (4*i-2*n-1 ) ; to         } +Qaq D =GCD (sum, N); -cout << sum/d <<' '<< n/d <<Endl; the         return 0 ;  *}
View Code

2016-10-25 18:07:05

20161022 Noip Simulation Game T2 Problem Solving report

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.