#!/usr/bin/python

"""
26 名前：デフォルトの名無しさん[sage] 投稿日：2014/06/15(日) 21:35:27.50 ID:lBQJsMnk
地球の周りを3つの衛星が回っています
衛星Aは1時間23分かけて地球一周できます
衛星Bは3時間9分かけて地球一周できます
衛星Cは5時間45かけて地球一周できます
衛星は同じ位置にくると自動的によけてくれるので衝突して故障することはありません。速度も一定に移動します。
衛星Aは60度の位置、衛星Bは192度の位置、衛星Cは265の位置からスタートする。
3つの衛星が同じ位置に重なりあうのは何時間後か求めて出力せよ。また、重なりあうまで何周したかそれぞれ出力せよ。
"""

from fractions import Fraction
from fractions import gcd
lcm = lambda a,b: a*b/gcd(a,b)

class Satellite:
	def __init__(self, id, period, phase, t=0):
		self.name = chr(ord("A")+id)
		self.period = Fraction(period)
		self.phase = Fraction(phase)
		self.t = Fraction(t)
	def __str__(self):
		return "{0.name}:per={0.period},pha={0.phase},time={0.t}".format(self)

def merge(a,b):
#	print a,b
	# 古い方の時間を進める
	if not a.t < b.t:
		a, b = b, a
	a.phase = (a.phase + (b.t - a.t) * 360 / a.period) % 360
	a.t = b.t
	
	# 追いつくまでの時間
	if not a.period < b.period:
		a, b = b, a
	delta = ((b.phase - a.phase) % 360) / 360 / (1/a.period - 1/b.period)
	
	# 重なるところまで移動
	for s in [a, b]:
		s.t += delta
		s.phase = (s.phase +  s.t * 360 / s.period) % 360
#	print a, b, delta
	
	# 1つの仮想の天体として返す
	return Satellite(-1, lcm(a.period, b.period), a.phase, a.t)

def f26(sats_inf):
	sats = [Satellite(i, s[0]*60+s[1], s[2]) for (i,s) in enumerate(sats_inf)]
	
	uni = reduce(merge, sats)
	print "t={}(h)\npha={}(degree)".format(float(uni.t/60), float(uni.phase))
	print "-"*3
	for s in sats:
		print "{0.name}:{1}(cyc)".format(s, float(uni.t / s.period))

q = [[1,23,60],[3,29,192],[5,45,265]]
#q = [[3,0,60],[6,0,0],]
f26(q)


