1. 程式人生 > >poj3101--Astronomy(分數的最小公倍數)

poj3101--Astronomy(分數的最小公倍數)

[] style valueof 能夠 blank content art [0 for

題目鏈接:點擊打開鏈接

題目大意:有n個行星,給出每個行星的旋轉的周期。問最少多少時間後n個行星會在一條直線上,初始點在一起,不存在全部的行星都有同一個周期

如果A行星的周期是t1。B行星的周期是t2(t2>t1),要在一條直線上,一定會執行的相差半個周期的倍數,時間(t/t2 - t/t1) % (1/2) = 0。也就是t*(t1-t2)/(t1*t2)%(1/2) = 0,要是時間最小。所以也就是差出一個半周期。也就是t = (t2-t1)/(t2*t1*2)這個t也就是A。B執行到一條直線上的最小時間,我們能夠求出其它全部行星和A行星的在一條直線的最小時間,然後求出這個時間的最小公倍數。也就是整體的時間。

分數的最小公倍數 = (分子的最小公倍數)/(分母的最大公約數)

分數的最大公約數 = (分子的最大公約數)/ (分母的最小公倍數)

import java.util.*;
import java.math.* ;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in) ;
        int n , i , cnt = 0 ;
        int a[] = new int[1010];
        int b[] = new int[1010] ;
        BigInteger x , y , temp , u , v ;
        n = cin.nextInt() ;
        for(i = 0 ; i < n ; i++)
            a[i] = cin.nextInt() ;
        Arrays.sort(a,0,n) ;
        b[cnt++] = a[0] ;
        for(i = 1 ; i < n ; i++)
            if( a[i] != b[cnt-1] )
                b[cnt++] = a[i] ;
        x = BigInteger.valueOf(b[1]*b[0]) ;
        y = BigInteger.valueOf((b[1]-b[0])*2) ;
        temp = x.gcd(y) ;
        x = x.divide(temp) ;
        y = y.divide(temp) ;
        for(i = 2 ; i < cnt ; i++) {
            u = BigInteger.valueOf(b[i]*b[0]) ;
            v = BigInteger.valueOf((b[i]-b[0])*2) ;
            temp = u.gcd(v) ;
            u = u.divide(temp) ; v = v.divide(temp) ;
            temp = x.gcd(u) ;
            x = x.multiply(u).divide(temp) ;
            y = y.gcd(v) ;
        }
        System.out.println(x + " " + y) ;
    }
}


poj3101--Astronomy(分數的最小公倍數)