3101 Astronomy Java大数,分数的最小公倍数
2010-08-21 11:42
246 查看
Astronomy
Description
There are n planets in the planetary system of star X. They orbit star X in circular orbits located in the same plane. Their tangent velocities are constant. Directions of orbiting of all planets are the same.
Sometimes the event happens in this planetary system which is called planet parade. It is the moment when all planets and star X are located on the same straight line.
Your task is to find the length of the time interval between two consecutive planet parades.
Input
The first line of the input file contains n — the number of planets (2 ≤ n ≤ 1 000).
Second line contains n integer numbers ti — the orbiting periods of planets (1 ≤ ti ≤ 10 000). Not all of ti are the same.
Output
Output the answer as a common irreducible fraction, separate numerator and denominator by a space.
Sample Input
Sample Output
Hint
Source
Northeastern Europe 2005, Northern Subregion
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String args[])throws Exception
{
Scanner cin=new Scanner(System.in);
int n = cin.nextInt();
int num[]=new int
;
int den[]=new int
;
int per[]=new int
;
for(int i = 0; i < n; i++)
per[i]=cin.nextInt();
Arrays.sort(per);
int cnt=1;
for(int i = 1; i < n; i++)
if(per[i]!=per[cnt-1])per[cnt++]=per[i];
for(int i = 0; i < cnt-1; i++)
{
num[i]=per[cnt-1]*per[i];
den[i]=2*(per[cnt-1] - per[i]);
int g=gcd(num[i],den[i]);
num[i]/=g;den[i]/=g;//important
//int c = gcd(num[i],den[i]);
//num[i]=num[i]/c;
//den[i]=den[i]/c;
}
BigInteger ansn= new BigInteger(num[0]+"");
BigInteger ansd= new BigInteger(den[0]+"");
for(int i = 1; i < cnt-1; i++)
{
BigInteger tempn = new BigInteger(num[i]+"");
BigInteger tempd = new BigInteger(den[i]+"");
/*ansn=ansn.multiply(tempd);
tempn=tempn.multiply(ansd);
ansn=lcm(ansn,tempn);
ansd=ansd.multiply(tempd);
BigInteger g = ansn.gcd(ansd);
ansn=ansn.divide(g);//ansn = ansn/g;
ansd=ansd.divide(g);//ansd = ansd/g;*/
ansn=lcm(ansn,tempn);
ansd=ansd.gcd(tempd);
}
BigInteger g = ansn.gcd(ansd);
ansn=ansn.divide(g);
ansd=ansd.divide(g);
System.out.println(ansn+" "+ansd);
}
static int gcd(int x,int y)
{
if (x == 0 || y == 0) if(x > y) return x; else return y;
for (int t; (t = x % y) != 0; x = y, y = t);
return y;
}
static BigInteger lcm(BigInteger a,BigInteger b)
{
BigInteger c = a.gcd(b);
return a.multiply(b).divide(c);
}
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2704 | Accepted: 553 |
There are n planets in the planetary system of star X. They orbit star X in circular orbits located in the same plane. Their tangent velocities are constant. Directions of orbiting of all planets are the same.
Sometimes the event happens in this planetary system which is called planet parade. It is the moment when all planets and star X are located on the same straight line.
Your task is to find the length of the time interval between two consecutive planet parades.
Input
The first line of the input file contains n — the number of planets (2 ≤ n ≤ 1 000).
Second line contains n integer numbers ti — the orbiting periods of planets (1 ≤ ti ≤ 10 000). Not all of ti are the same.
Output
Output the answer as a common irreducible fraction, separate numerator and denominator by a space.
Sample Input
3 6 2 3
Sample Output
3 1
Hint
Source
Northeastern Europe 2005, Northern Subregion
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String args[])throws Exception
{
Scanner cin=new Scanner(System.in);
int n = cin.nextInt();
int num[]=new int
;
int den[]=new int
;
int per[]=new int
;
for(int i = 0; i < n; i++)
per[i]=cin.nextInt();
Arrays.sort(per);
int cnt=1;
for(int i = 1; i < n; i++)
if(per[i]!=per[cnt-1])per[cnt++]=per[i];
for(int i = 0; i < cnt-1; i++)
{
num[i]=per[cnt-1]*per[i];
den[i]=2*(per[cnt-1] - per[i]);
int g=gcd(num[i],den[i]);
num[i]/=g;den[i]/=g;//important
//int c = gcd(num[i],den[i]);
//num[i]=num[i]/c;
//den[i]=den[i]/c;
}
BigInteger ansn= new BigInteger(num[0]+"");
BigInteger ansd= new BigInteger(den[0]+"");
for(int i = 1; i < cnt-1; i++)
{
BigInteger tempn = new BigInteger(num[i]+"");
BigInteger tempd = new BigInteger(den[i]+"");
/*ansn=ansn.multiply(tempd);
tempn=tempn.multiply(ansd);
ansn=lcm(ansn,tempn);
ansd=ansd.multiply(tempd);
BigInteger g = ansn.gcd(ansd);
ansn=ansn.divide(g);//ansn = ansn/g;
ansd=ansd.divide(g);//ansd = ansd/g;*/
ansn=lcm(ansn,tempn);
ansd=ansd.gcd(tempd);
}
BigInteger g = ansn.gcd(ansd);
ansn=ansn.divide(g);
ansd=ansd.divide(g);
System.out.println(ansn+" "+ansd);
}
static int gcd(int x,int y)
{
if (x == 0 || y == 0) if(x > y) return x; else return y;
for (int t; (t = x % y) != 0; x = y, y = t);
return y;
}
static BigInteger lcm(BigInteger a,BigInteger b)
{
BigInteger c = a.gcd(b);
return a.multiply(b).divide(c);
}
}
相关文章推荐
- POJ - 3101 - Astronomy - (JAVA大数,分数的最小公倍数)
- poj 3101 Astronomy(分数的最小公倍数)
- poj 3101 Astronomy (java 分数的最小公倍数 gcd)
- poj 3101 Astronomy(分数的最小公倍数)
- POJ 3101 Astronomy 解题报告(大数乘法+分数最小公倍数)
- (Relax 数论 1.17)POJ 3101 Astronomy(分数的最小公倍数)
- poj3101--Astronomy(分数的最小公倍数)
- nyoj 517 最小公倍数 【java大数】
- 黄金连分数【大数】
- poj 3101 Astronomy 分数lcm+大数java
- 蓝桥杯--2013--黄金连分数(大数)
- 趣味分数-三个数的最小公倍数-java
- 续谈分数求和:最小公倍数的求法
- POJ 3101 大数+gcd
- NYOJ 517 最小公倍数 (大数+求最小公倍数思想)
- 蓝桥杯第四届 黄金连分数(大数 斐波那契数列与黄金分割)
- POj-3101-Astronomy(分数GCD+BigInteger)
- nyoj 517 最小公倍数【数学】大数处理
- 蓝桥杯--2013--黄金连分数(大数)
- POJ 3101 Astronomy (挖坑待学Java……最小公倍数---大数表示)