POJ 3101 Astronomy (java大数,物理参考系)

题目

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.

思路

首先这个题目看到的时候肯定没什么思路,因为他要求所有的星球相对恒星处于同一个平面,那么嗯做肯定写不出来,那么如果我们把一个星球当作参考系,然后算出其他星球对这个星球的相对速度,然后求出这些速度的最小公倍数就可以了,所以这里我们用大数来写一个。

代码实现

import java.math.*;
import java.util.*;
public class b {
    public static BigInteger [] fz=new BigInteger [1200];
    public static BigInteger [] fm=new BigInteger [1200];

    public static int t[]=new int [1200];
    public static int tt[]=new int[1200];

    public static int gcd (int a,int b) {
         if (b==0) return a;
         return gcd (b,a%b);
    }
    public static int lcm (int a,int b) {
        return a/gcd (a,b)*b;
    }
    public static void main (String[] args) {
        Scanner cin=new Scanner (System.in);
        int n;
        n=cin.nextInt();
        for (int i=0;i<n;i++) t[i]=cin.nextInt();
        Arrays.sort(t,0,n);
        int cnt=0;
        tt[cnt++]=t[0];
        for (int i=1;i<n;i++) if (t[i]!=t[i-1]) tt[cnt++]=t[i];
        for (int i=1;i<cnt;i++) {
            int a=tt[i]*tt[0];
            int b=2*(tt[i]-tt[0]);
            int g=gcd (a,b);
            fz[i]=BigInteger.valueOf(a/g);
            fm[i]=BigInteger.valueOf(b/g);
        } 
        BigInteger t1=fz[1],t2=fm[1];
        for (int i=2;i<cnt;i++) {
            BigInteger aa=t1.multiply(fz[i]);
            BigInteger gg=t1.gcd (fz[i]);
            t1=aa.divide(gg);
            t2=t2.gcd (fm[i]);
        }
        System.out.print(t1+" "+t2);
    }
}
原文地址:https://www.cnblogs.com/hhlya/p/13744924.html