Problem Description
source:https://uva.onlinejudge.org/external/120/12068.html
Input
The first line of the input file contains an integer S (0 < S < 501), which indicates how many sets of inputs are there. Each of the next S lines contains one set of input. The description of each set is given below: Each set starts with an integer N (0 < N < 9), which indicates how many numbers are there in this set. This number is followed by N integers a1, a2, a3 . . . aN−1, aN (0 < ai < 101).
Output
For each set of input produce one line of output. This line contains the serial of output followed by two integers m and n separated by a front slash. These two numbers actually indicate that the harmonic mean of the given four numbers is m n . You must ensure that gcd(m, n) = 1 or in other words m and n must be relative prime. The value of m and n will fit into a 64-bit signed integer.
Sample Input
2
4 1 2 3 4
4 2 2 3 1
Sample Output
Case 1: 48/25
Case 2: 12/7
source:https://uva.onlinejudge.org/external/120/12068.html
Input
The first line of the input file contains an integer S (0 < S < 501), which indicates how many sets of inputs are there. Each of the next S lines contains one set of input. The description of each set is given below: Each set starts with an integer N (0 < N < 9), which indicates how many numbers are there in this set. This number is followed by N integers a1, a2, a3 . . . aN−1, aN (0 < ai < 101).
Output
For each set of input produce one line of output. This line contains the serial of output followed by two integers m and n separated by a front slash. These two numbers actually indicate that the harmonic mean of the given four numbers is m n . You must ensure that gcd(m, n) = 1 or in other words m and n must be relative prime. The value of m and n will fit into a 64-bit signed integer.
Sample Input
2
4 1 2 3 4
4 2 2 3 1
Sample Output
Case 1: 48/25
Case 2: 12/7
Solution :
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
long long int gcd(long long int a, long long int b)
{
if(!b)return a;
return gcd(b,a%b);
}
long long int lcd(long long int a, long long int b)
{
long long int c;
if(a>b)
{
c=a;
a=b;
b=c;
}
return (a*b)/gcd(a,b);
}
int main()
{
long long int t,i,j,n,sum,rlcd,a,nominator,dividor,value;
while(scanf("%lld",&t)==1)
{
for(i=1;i<=t;i++)
{
scanf("%lld",&n);
vector<long long int> myvector;
a=1;
sum=0;
for(j=0;j<n;j++)
{
scanf("%lld",&value);
myvector.push_back(value);
rlcd=lcd(a,myvector[j]);
a=rlcd;
}
for(j=0;j<n;j++)
sum+=(rlcd/myvector[j]);
nominator = (n*rlcd);
while(gcd(nominator,sum)!=1)
{
dividor = gcd (nominator,sum);
nominator/=dividor;
sum/=dividor;
}
printf("Case %lld: %lld/%lld\n",i,nominator,sum);
}
}
return 0;
}
No comments:
Post a Comment
Write your comment - Share Knowledge and Experience