poj 3101 Astronomy
2个星球周期为a,b。则相差半周的长度为a*b/(2*abs(a-b)),对于n个只需求这n个
分数的最小公倍数即可!
公式:
分数的最小公倍数 = 分子的最小公倍数/分母的最大公约数
由于涉及到大数所以用java写的方便!
1 import java.math.*; 2 import java.util.*; 3 4 public class Main { 5 public static void main(String arg[]){ 6 Scanner cin = new Scanner(System.in); 7 int n = cin.nextInt(); 8 int an[] = new int[n]; 9 int a[] = new int[n]; 10 int b[] = new int[n]; 11 int i,j,k; 12 for (i=0;i<n;i++) 13 an[i] = cin.nextInt(); 14 Arrays.sort(an); 15 for (i=1,j=1;i<n;i++) 16 if (an[i]!=an[i-1]) 17 an[j++] = an[i]; 18 for (i=1,k=0;i<j;i++){ 19 a[k] = (an[i] - an[i-1])*2; 20 b[k] = an[i] * an[i-1]; 21 int t = gcd(a[k],b[k]); 22 a[k] /= t; 23 b[k++] /= t; 24 } 25 BigInteger ans1 = BigInteger.valueOf(a[0]),ans2 = BigInteger.valueOf(b[0]),ans; 26 for (i=1;i<k;i++){ 27 ans1 = ans1.gcd(BigInteger.valueOf(a[i])); 28 ans = ans2.multiply(BigInteger.valueOf(b[i])); 29 ans2 = ans.divide(ans2.gcd(BigInteger.valueOf(b[i]))); 30 } 31 ans = ans2.gcd(ans1); 32 System.out.println(ans2.divide(ans)+" "+ans1.divide(ans)); 33 } 34 public static int gcd(int a,int b){ 35 int t; 36 if (a<b){ 37 t = a; 38 a = b; 39 b = t; 40 } 41 while(true){ 42 if (b==0) break; 43 t = a; 44 a = b; 45 b = t%b; 46 } 47 return a; 48 } 49 }