Uva 12068 - Harmonic Mean


 #include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long LL;

#define sf scanf
#define pf printf

template<class T> T gcd(T a, T b) {return b!=0?gcd<T>(b,a%b):a;}

int main()
{
    int t,tc=0;
    sf("%d",&t);

    while(t--)
    {
        int n;
        LL lcm=1;
        sf("%d",&n);
        int num[n+9];

        for(int i=0;i<n;i++)
        {
            sf("%d",&num[i]);
            lcm*=num[i];
        }

        LL s=0;

        for(int i=0;i<n;i++)
        {
            s+=(lcm / num[i]);
        }

        LL mul = n * lcm;
        LL divi = gcd<LL>(mul,s);
        LL lob = mul / divi , hor = s / divi;

        pf("Case %d: %lld/%lld\n",++tc,lob,hor);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number