Language: C++

#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
long long int gcd(long long int l, long long int r) {
	assert(l > 0 && r > 0);
	if (l > r)return gcd(r, l);
	else {
		const long long int num = r%l;
		if (num) {
			return gcd(l, num);
		}
		else {
			return l;
		}
	}
}
long long int lca(long long int l, long long int r) {
	return l / gcd(l, r)*r;
}

//  nd/de
class Fruction
{
public:
	long long int nd;
	long long int de;
	void reduct() {
		if (nd) {
			bool ndminus = false;
			if (nd < 0) {
				ndminus = true;
				nd = -nd;
			}

			const long long int agcd(gcd(nd, de));
			de /= agcd;
			nd /= agcd;
			if (ndminus)nd = -nd;
		}
		else {
			de = 1;
		}
	}
	Fruction() :nd(0), de(1) {

	}
	void initialize() {
		if (de < 0) {
			de = -de;
			nd = -nd;
		}
		reduct();
		
	}
	Fruction(const long long int nd_,const long long int de_) 
		:nd(nd_), de(de_) {
		initialize();
	}
	Fruction(const long long int nd_, const Fruction de_) :
		nd(nd_*de_.de), de(de_.nd) {
		initialize();
	}
	Fruction(const Fruction nd_, const long long int de_) :
		nd(nd_.nd), de(nd_.de*de) {
		initialize();
	}
	Fruction(const long long int num):nd(num),de(1) {
	}
	Fruction(const Fruction& f) :de(f.de), nd(f.nd) {

	}
	pair<long long int, Fruction>mixed() {
		long long int num = nd / de;
		return make_pair(num, Fruction(nd - de*num, de));
	}
};
Fruction operator-(const Fruction&f) {
	return Fruction(-f.nd, f.de);
}
Fruction operator+(const Fruction&l, const Fruction&r) {
	long long int nede = lca(l.de, r.de);
	long long int nend = l.nd*(nede / l.de) + r.nd*(nede / r.de);
	return Fruction(nend, nede);
}
Fruction operator-(const Fruction&l, const Fruction&r) {
	return l+(-r);
}
Fruction operator*(const Fruction&l, const Fruction&r) {
	long long int nend = l.nd*r.nd;
	long long int nede = l.de*r.de;
	Fruction nefr(nend, nede);
	nefr.initialize();
	return nefr;
}
Fruction operator*(const Fruction&l, const long long int&r) {
	return l*Fruction(r, 1);
}
Fruction operator*(const long long int &l, const Fruction&r) {
	return Fruction(l, 1)*r;
}
Fruction operator/(const Fruction&l, const Fruction&r) {
	long long int nend = l.nd*r.de;
	long long int nede = l.de*r.nd;
	Fruction nefr(nend, nede);
	nefr.initialize();
	return nefr;
}
Fruction operator/(const Fruction&l, const long long int&r) {
	return l / Fruction(r, 1);
}
Fruction operator/(const long long int&l, const Fruction&r) {
	return Fruction(l,1) / r;
}

bool operator<(const Fruction&l, const Fruction&r) {
	return l.nd*r.de < r.nd*l.de;
}
bool operator<(const Fruction&l, const Fruction&r) {
	return l.nd*r.de <= r.nd*l.de;
}
bool operator==(const Fruction&l, const Fruction&r) {
	return l.nd*r.de == r.nd*l.de;
}
bool operator>(const Fruction&l, const Fruction&r) {
	return l.nd*r.de > r.nd*l.de;
}
bool operator>(const Fruction&l, const Fruction&r) {
	return l.nd*r.de >= r.nd*l.de;
}

int main() {
	while (1) {
		int T, H, M, S; cin >> T >> H >> M >> S;
		if (!T)break;
		int total = T*60*60;
		int ntime = H * 3600 + M * 60 + S;
		if (!T)break;
		Fruction speeds[3];
		speeds[0] = Fruction(1, T * 60 * 60);
		speeds[1] = Fruction(1, 60 * 60);
		speeds[2] = Fruction(1, 60);
		Fruction now[3];
		for (int i = 0; i < 3; ++i) {
			now[i] = Fruction(speeds[i] * ntime);
			now[i] = now[i].mixed().second;
		}
		Fruction ans(1e9,1);
		for (int i = 0; i < 1; ++i) {
			Fruction dspeed = -(speeds[i] + speeds[(i + 1) % 3]) / 2 + speeds[(i + 2) % 3];
			Fruction dkaku = (now[i] + now[(i + 1) % 3]) / 2 - now[(i + 2) % 3];
			if (dspeed < 0) {
				dkaku = -dkaku;
				dspeed = -dspeed;
			}
			for (int j = -49; j < 50; ++j) {
				Fruction nans = (dkaku +  j* Fruction(1, 2)) / dspeed;
				Fruction next[3];
				for (int k = 0; k < 3; ++k) {
					next[k] = now[k] + nans*speeds[k];
					next[k] = next[k].mixed().second;
				}
				bool ok = true;
				for (int k = 0; k < 3; ++k) {
					if (next[k] == next[(k + 1) % 3])ok = false;
				}
				if (nans >= Fruction(0,1)) {
					if (ok) {
						ans = min(ans, nans);
					}
				}
			}
		}
		pair<long long int, Fruction>p(ans.mixed());
		long long int fintime = ntime + p.first;
		fintime %= T * 60 * 60;
		const int nh = fintime / 3600;
		fintime -= nh * 3600;
		const int nm = fintime / 60;
		fintime -= nm * 60;
		Fruction ns(fintime*p.second.de + p.second.nd, p.second.de);
		cout << nh << " " << nm << " " << ns.nd << " " << ns.de << endl;
	}
	return 0;
}

Compiler output:
a.cc:123:6: error: redefinition of 'bool operator<(const Fruction&, const Fruction&)'
  123 | bool operator<(const Fruction&l, const Fruction&r) {
      |      ^~~~~~~~
a.cc:120:6: note: 'bool operator<(const Fruction&, const Fruction&)' previously defined here
  120 | bool operator<(const Fruction&l, const Fruction&r) {
      |      ^~~~~~~~
a.cc:132:6: error: redefinition of 'bool operator>(const Fruction&, const Fruction&)'
  132 | bool operator>(const Fruction&l, const Fruction&r) {
      |      ^~~~~~~~
a.cc:129:6: note: 'bool operator>(const Fruction&, const Fruction&)' previously defined here
  129 | bool operator>(const Fruction&l, const Fruction&r) {
      |      ^~~~~~~~
a.cc: In function 'int main()':
a.cc:171:42: error: no match for 'operator>=' (operand types are 'Fruction' and 'Fruction')
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                     ~~~~ ^~ ~~~~~~~~~~~~~
      |                                     |       |
      |                                     |       Fruction
      |                                     Fruction
In file included from /usr/include/c++/14/regex:68,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:181,
                 from a.cc:1:
/usr/include/c++/14/bits/regex.h:1165:5: note: candidate: 'template<class _BiIter> bool std::__cxx11::operator>=(const sub_match<_BiIter>&, const sub_match<_BiIter>&)'
 1165 |     operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1165:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/regex.h:1248:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator>=(__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const sub_match<_BiIter>&)'
 1248 |     operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1248:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/regex.h:1341:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator>=(const sub_match<_BiIter>&, __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&)'
 1341 |     operator>=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1341:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/regex.h:1415:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator>=(const typename std::iterator_traits<_Iter>::value_type*, const sub_match<_BiIter>&)'
 1415 |     operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1415:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/regex.h:1509:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator>=(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type*)'
 1509 |     operator>=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1509:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/regex.h:1586:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator>=(const typename std::iterator_traits<_Iter>::value_type&, const sub_match<_BiIter>&)'
 1586 |     operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1586:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/regex.h:1686:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator>=(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type&)'
 1686 |     operator>=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1686:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_pair.h:1070:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator>=(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1070 |     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1070:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::pair<_T1, _T2>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:476:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator>=(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  476 |     operator>=(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:476:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::reverse_iterator<_Iterator>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/stl_iterator.h:520:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator>=(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  520 |     operator>=(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:520:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::reverse_iterator<_Iterator>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/stl_iterator.h:1724:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator>=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1724 |     operator>=(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1724:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::move_iterator<_IteratorL>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/bits/stl_iterator.h:1781:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator>=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1781 |     operator>=(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1781:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'const std::move_iterator<_IteratorL>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
In file included from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/string_view:739:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator>=(basic_string_view<_CharT, _Traits>, basic_string_view<_CharT, _Traits>)'
  739 |     operator>=(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:739:5: note:   template argument deduction/substitution failed:
a.cc:171:57: note:   'Fruction' is not derived from 'std::basic_string_view<_CharT, _Traits>'
  171 |                                 if (nans >= Fruction(0,1)) {
      |                                                         ^
/usr/include/c++/14/string_view:746:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator>=(basic_string_view<_CharT, _Traits>, __type_identity_t<basic_string_view<_CharT, _Traits> >)'
  746 |     operator>=(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:746:5: note:   template argument deduction/substitut