zoukankan      html  css  js  c++  java
  • codeforces340C

    Tourist Problem

     CodeForces - 340C 

    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 sequence a1a2, ..., 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.

    Examples

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

    sol:小学奥数题.

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=100005;
    int n;
    ll a[N],ans=0;
    ll Jiec[N],Invj[N];
    inline ll gcd(ll a,ll b)
    {
        return (b==0)?a:(gcd(b,a%b));
    }
    int main()
    {
        int i;
        ll S=0,GG;
        R(n);
        for(i=1;i<=n;i++) R(a[i]);
        sort(a+1,a+n+1);
        for(i=2;i<=n;i++)
        {
            S+=a[i-1];
            ans+=1ll*(i-1)*a[i]-S;
        }
        S+=a[n];
        ans=1ll*(ans*2ll+S);
        GG=gcd(ans,n);
        W(ans/GG); Wl(n/GG);
        return 0;
    }
    /*
    Input
    3
    2 3 5
    Output
    22 3
    
    Input
    4
    1 5 77 2
    Output
    547 4
    
    Input
    40
    8995197 7520501 942559 8012058 3749344 3471059 9817796 3187774 4735591 6477783 7024598 3155420 6039802 2879311 2738670 5930138 4604402 7772492 6089337 317953 4598621 6924769 455347 4360383 1441848 9189601 1838826 5027295 9248947 7562916 8341568 4690450 6877041 507074 2390889 8405736 4562116 2755285 3032168 7770391
    Output
    644565018 5
    */
    View Code
  • 相关阅读:
    包 (package)
    Object类
    异常
    接口
    抽象类
    多态(经典案例)
    三大特性:(经典代码)
    对象创建的过程(重点理解)
    final关键字
    cocos2dx工程中接入支付宝sdk
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/11111348.html
Copyright © 2011-2022 走看看