POJ 3101 Astronomy

本文介绍了一种计算多个行星围绕恒星X运行时形成行星游行周期的方法。行星们在同一平面上以恒定速度沿圆形轨道运行。文章通过示例展示了如何使用Java实现这一计算,并给出了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Astronomy
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4700 Accepted: 996

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

3
6 2 3

Sample Output

3 1

Hint

Source

Northeastern Europe 2005, Northern Subregion

实质是求分数的最小公倍数,有了思路后没仔细推倒分数的最小公倍数的公式,哎,错了很多次啊
分数的最小公倍数 =  分子的最小公倍数/ 分母的最大公约数
自己想想能想明白是为什么的
第一次用java做题

import java.math.BigInteger;
import java.util.Scanner;
import java.util.Arrays;
public class Main
{

	 BigInteger  numerator;
	 BigInteger  denominator;
	public static void main(String[] args) 
	{
		int n;
		Scanner cin = new Scanner(System.in);
		int a[],c[];
		while(cin.hasNextInt())
		{
			n = cin.nextInt();
			a = new int[n];
			c = new int[n];
			for(int i=0;i<=n-1;i++)
			{
				a[i] = cin.nextInt();
			}
			Arrays.sort(a);
			c[0] = a[0];
			int sum = 1;
			for(int i=1;i<=n-1;i++)
			{
				if(a[i]!=a[i-1])
				{
					c[sum++] = a[i];
				}
			}
			n = sum;
			Main b[] = new Main[n-1];
			for(int i=0;i<=n-2;i++)
			{
				b[i] = new Main();
		         BigInteger x  = BigInteger.valueOf(c[i]*c[i+1]);
		         BigInteger y = BigInteger.valueOf(2 * (c[i+1]-c[i]));
		         BigInteger d =x.gcd(y);
		         x = x.divide(d);
		         y = y.divide(d);
		         b[i].numerator = x;
		         b[i].denominator = y;
			} 
			BigInteger numerator = b[0].numerator;
	        BigInteger denominator = b[0].denominator;
	        for(int i=1;i<=n-2;i++)
	        {
	        	BigInteger x1 = numerator;
	        	BigInteger y1 = denominator;
	        	BigInteger x2 = b[i].numerator;
	        	BigInteger y2 = b[i].denominator;
	        	BigInteger d = x1.gcd(x2);
	        	BigInteger t1 = (x1.multiply(x2)).divide(d);
	        	BigInteger t2 = y1.gcd(y2);
	        	
	        	d = t1.gcd(t2);
	        	t1 = t1.divide(d);
	        	t2 = t2.divide(d);
	            numerator = t1;
	            denominator  = t2;
	        } 
	        System.out.println(numerator+" "+denominator);
		} 
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值