Problem:
Problem D:
Clock Hands
We have an analog clock whose three hands (the second hand, the
minute
hand and the
hour
hand) rotate quite smoothly. You can measure two angles between the second hand and two other hands.
Write a program to find the time at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on or after a given time.
Figure D.1. Angles between the second hand and two other hands
Clocks are not limited to 12-hour clocks. The hour hand of an
H
-hour clock goes around once in
H
hours. The minute hand still goes around once every hour, and the second hand goes around once every minute. At 0:0:0 (midnight), all the hands are at the upright position.
Input
The input consists of multiple datasets. Each of the dataset has four integers
H
,
h
,
m
and
s
in one line, separated by a space.
H
means that the clock is an
H
-hour clock.
h
,
m
and
s
mean hour, minute and second of the specified time, respectively.
You may assume 2 ≤
H
≤ 100, 0 ≤
h
<
H
, 0 ≤
m
< 60, and 0 ≤
s
< 60.
The end of the input is indicated by a line containing four zeros.
Figure D.2. Examples of H-hour clock (6-hour clock and 15-hour clock)
Output
Output the time T at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on and after the specified time.
For
T
being
h
o
:
m
o
:
s
o
(
s
o
seconds past
m
o
minutes past
h
o
o'clock), output four non-negative integers
h
o
,
m
o
,
n
, and
d
in one line, separated by a space, where
n
/
d
is the irreducible fraction representing
s
o
. For integer
s
o
including 0, let
d
be 1.
The time should be expressed in the remainder of
H
hours. In other words, one second after
(
H
− 1):59:59 is 0:0:0, not
H
:0:0.
Sample Input
12 0 0 0
12 11 59 59
12 1 56 0
12 1 56 3
12 1 56 34
12 3 9 43
12 3 10 14
12 7 17 58
12 7 18 28
12 7 23 0
12 7 23 31
2 0 38 29
2 0 39 0
2 0 39 30
2 1 6 20
2 1 20 1
2 1 20 31
3 2 15 0
3 2 59 30
4 0 28 48
5 1 5 40
5 1 6 10
5 1 7 41
11 0 55 0
0 0 0 0
Output for the Sample Input
0 0 43200 1427
0 0 43200 1427
1 56 4080 1427
1 56 47280 1427
1 57 4860 1427
3 10 18600 1427
3 10 61800 1427
7 18 39240 1427
7 18 82440 1427
7 23 43140 1427
7 24 720 1427
0 38 4680 79
0 39 2340 79
0 40 0 1
1 6 3960 79
1 20 2400 79
1 21 60 79
2 15 0 1
0 0 2700 89
0 28 48 1
1 6 320 33
1 6 40 1
1 8 120 11
0 55 0 1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define srep(i, s, t) for(int i = (s); i < (t); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
using i64 = long long;
using f64 = long double;
i64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) <  0 && n % d != 0); }
i64  ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }

#define int i64
tuple<int, int, int, int> solve(int H, int h, int m, int s) {
    const int D = 119 * H - 1;
    const int M = H * 60 * 60 * D;
    int d = 0;
    while(true) {
        bool ok = [&] {
            int rs = H * 60 * (s * D + d);
            int rm = H * ((m * 60 + s) * D + d);
            int rh = ((h * 60 + m) * 60 + s) * D + d;
            if(rm == rh) return false;
            return (rm - rs + M) % M == (rs - rh + M) % M;
        }();
        if(ok) {
            int u = s * D + d;
            int l = D;
            const int g = gcd(u, l);
            return {h, m, u / g, l / g};
        }

        d++;
        if(d == D ) s++, d=0;
        if(s == 60) m++, s=0;
        if(m == 60) h++, m=0;
        if(h == H )      h=0;
    }
    assert(0);
}
#undef int

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    while(true) {
        int H, h, m, s; cin >> H >> h >> m >> s; if(H == 0) return 0;
        auto [ho, mo, n, d] = solve(H, h, m, s);
        cout << ho << " " << mo << " " << n << " " << d << "\n";
    }
}
accuracy: 1
time_ms: 140
memory_kb: 3216
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define srep(i, s, t) for(int i = (s); i < (t); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
using i64 = long long;
using f64 = long double;
i64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) <  0 && n % d != 0); }
i64  ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }

#define int i64
tuple<int, int, int, int> solve(int H, int h, int m, int s) {
    const int D = 119 * H - 1;
    const int M = H * 60 * 60 * D;
    int d = 0;
    while(true) {
        bool ok = [&] {
            int rs = H * 60 * (s * D + d);
            int rm = H * ((m * 60 + s) * D + d);
            int rh = ((h * 60 + m) * 60 + s) * D + d;
            if(rm == rh) return false;
            int dm = rm - rs; if(dm < 0) dm += M;
            int dh = rs - rh; if(dh < 0) dh += M;
            return dm == dh;
        }();
        if(ok) {
            int u = s * D + d;
            int l = D;
            const int g = gcd(u, l);
            return {h, m, u / g, l / g};
        }

        d++;
        if(d == D ) s++, d=0;
        if(s == 60) m++, s=0;
        if(m == 60) h++, m=0;
        if(h == H )      h=0;
    }
    assert(0);
}
#undef int

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    while(true) {
        int H, h, m, s; cin >> H >> h >> m >> s; if(H == 0) return 0;
        auto [ho, mo, n, d] = solve(H, h, m, s);
        cout << ho << " " << mo << " " << n << " " << d << "\n";
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3252
final_rank: 11

code_snippet: #include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for(int i = (l); i < (r); i++)
#define per2(i, l, r) for(int i = (r)-1; i >= (l); i--)
#define each(e, x) for(auto &e : x)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define eb emplace_back
#define MM << ' ' <<
#define TT template<typename T>
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }

TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

TT void print(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (n == 0) cout << '\n';
}

TT void printn(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << '\n';
}

TT void rearrange(vector<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), end(v));
}

TT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }

TT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main() {
    while (1) {
        int H, h, m, s;
        cin >> H >> h >> m >> s;
        if (!H)
            break;
        ll v = (H * 238 - 2);
        ll div = v * H * 3600;
        ll c = 0;
        while (1) {
            ll ns = (h * 3600 + m * 60 + s) * v + c;
            ll d1 = ns * (H * 60 - 1), d2 = ns * H * 59;
            d1 %= div, d2 %= div;
            chmin(d1, div - d1);
            chmin(d2, div - d2);
            if (d1 != 0 && ns * H % div != ns % div && d1 == d2) {
                ll g = gcd(ns % (v * 60), v);                
                cout << ns / (v * 3600) % H << ' ' << ns / (v * 60) % 60 << ' ' << ns % (v * 60) / g << ' ' << v / g << endl;
                break;
            }
            c++;
        }
    }
}
accuracy: 1
time_ms: 480
memory_kb: 3256
final_rank: 12

code_snippet: #include "bits/stdc++.h"
using namespace std;

typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()

#define int long long

int H, h, m, s, d;

bool check()
{
  ll x = 60 * H * s, y = (60 * m * d + s) * H, z = 3600 * h * d + 60 * m * d + s;
  if (y == z)
    return false;
  ll dify = y - x, difz = x - z;
  if (dify < 0)
    dify += 3600 * H * d;
  if (difz < 0)
    difz += 3600 * H * d;
  return dify == difz;
}

void solve()
{
  cin >> H >> h >> m >> s;
  if(H == 0)
    exit(0);
  d = 119 * H - 1;
  s *= d;
  while (true)
  {
    if (check())
    {
      cout << h << " " << m << " " << s / gcd(s, d) << " " << d / gcd(s, d) << endl;
      return;
    }
    s++;
    if (s / d == 60)
      s = 0, m++;
    if (m == 60)
      m = 0, h++;
    if (h == H)
      h = 0;
  }
}
#undef int

// generated by oj-template v4.7.2
// (https://github.com/online-judge-tools/template-generator)
int main()
{
  // Fasterize input/output script
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(100);
  // scanf/printf user should delete this fasterize input/output script

  int t = 10000;
  // cin >> t; // comment out if solving multi testcase
  for (int testCase = 1; testCase <= t; ++testCase)
  {
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3376
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Clock {
    int h, m, n, d;
    Clock(int h_, int m_, int n_, int d_) : h(h_), m(m_), n(n_), d(d_) {
        int g = gcd(n, d);
        n /= g;
        d /= g;
    }
    bool operator<(const Clock& rhs) const {
        if (h != rhs.h) return h < rhs.h;
        if (m != rhs.m) return m < rhs.m;
        return n * rhs.d < rhs.n * d;
    }
};

bool check(int H, int h, int m, int n, int d) {
    int pos_h = (h * 60 * 60 + m * 60) * 2 * d + 2 * n;
    int pos_m = m * 60 * (2 * H) * d + (2 * H) * n;
    return pos_h != pos_m;
}

void solve(int H, int h, int m, int s) {
    int speed = 120 * H - (H + 1);
    vector<Clock> collision;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < 60; j++) {
            int tmp = ((H + 1) * (i * 60 * 60 + j * 60)) % (7200 * H);
            vector<int> v = {tmp, (tmp + 3600 * H) % (7200 * H)};
            if (v[0] > v[1]) swap(v[0], v[1]);
            for (int _ = 0; _ < 2; _++) {
                int pos_mid = v[_];
                if (pos_mid >= 60 * speed) continue;
                if (!check(H, i, j, pos_mid, speed)) continue;
                collision.emplace_back(i, j, pos_mid, speed);
            }
        }
    }
    auto itr = lower_bound(collision.begin(), collision.end(), Clock(h, m, s, 1));
    if (itr == collision.end()) itr = collision.begin();
    auto ans = *itr;
    cout << ans.h << ' ' << ans.m << ' ' << ans.n << ' ' << ans.d << '\n';
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, h, m, s;
    while (cin >> H >> h >> m >> s, H) solve(H, h, m, s);
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3520
final_rank: 15

code_snippet: #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

struct Clock {
    int h, m, n, d;
    Clock(int h_, int m_, int n_, int d_) : h(h_), m(m_), n(n_), d(d_) {
        int g = gcd(n, d);
        n /= g;
        d /= g;
    }
    bool operator<(const Clock& rhs) const {
        if (h != rhs.h) return h < rhs.h;
        if (m != rhs.m) return m < rhs.m;
        return n * rhs.d < rhs.n * d;
    }
};

bool check(int H, int h, int m, int n, int d) {
    int pos_h = (h * 60 * 60 + m * 60) * 2 * d + 2 * n;
    int pos_m = m * 60 * (2 * H) * d + (2 * H) * n;
    return pos_h != pos_m;
}

void solve(int H, int h, int m, int s) {
    int speed = 120 * H - (H + 1);
    vector<Clock> collision;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < 60; j++) {
            int tmp = ((H + 1) * (i * 60 * 60 + j * 60)) % (7200 * H);
            vector<int> v = {tmp, (tmp + 3600 * H) % (7200 * H)};
            if (v[0] > v[1]) swap(v[0], v[1]);
            for (int _ = 0; _ < 2; _++) {
                int pos_mid = v[_];
                if (pos_mid >= 60 * speed) continue;
                if (!check(H, i, j, pos_mid, speed)) continue;
                collision.emplace_back(i, j, pos_mid, speed);
            }
        }
    }
    auto itr = lower_bound(collision.begin(), collision.end(), Clock(h, m, s, 1));
    if (itr == collision.end()) itr = collision.begin();
    auto ans = *itr;
    cout << ans.h << ' ' << ans.m << ' ' << ans.n << ' ' << ans.d << '\n';
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, h, m, s;
    while (cin >> H >> h >> m >> s, H) solve(H, h, m, s);
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3628
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#include <iostream>
#include <limits>
#include <numeric>
#include <type_traits>
#include <bitset>
#include <map>
#include <cassert>

using namespace std;

using ll = long long;
#define rep(i,n,m) for(ll (i)=(n);(i)<(m);(i)++)
#define rrep(i,n,m) for(ll (i)=(n);(i)>(m);(i)--)

pair<ll,ll> vmin(pair<ll,ll> x,pair<ll,ll> y){

  if (x.second < 0){
    x.first *= -1;
    x.second *= -1;
  }

  if (y.second < 0){
    y.first *= -1;
    y.second *= -1;
  }
  if (y.first < 0) {
    return x;
  }

  assert ( (x.first * y.second) / y.second == x.first);
  assert ( (y.first * x.second) / x.second == y.first);

  if (x.first * y.second > y.first * x.second){
    return y;
  }else{
    return x;
  }

}

ll modplus(ll a, ll b) {
  a %= b;
  return (a + b) % b;
}

int main(){

  while (true){

    ll H,h,m,s;
    scanf ("%lld%lld%lld%lld",&H,&h,&m,&s);



    if ( H == 0 && h == 0 && m == 0 && s == 0 ){
      break;
    }

    pair<ll,ll> ans = {INT_MAX-1000,1};

    { // H-1
      ll mod = 3600 * H;
      ll hang = ( (3600*h + 60*m + s) * 1) % mod;
      ll mang = ( (60*m+s) * H ) % mod;
      ll sang = ( s * (60*H)   ) % mod;
      ll hch  = 1;
      ll mch  = H;
      ll sch  = 60*H;
      ll pl = H-1;
      mod *= pl;
      hang *= pl;
      mang *= pl;
      sang *= pl;

      rep(i,0,120*pl){

        if (modplus(sang-mang, mod) == modplus(sang-hang, mod) || modplus(sang-mang+sang-hang, mod) == 0){


          if ( sang % mod != mang % mod && mang % mod != hang % mod && sang % mod != hang % mod ){
            pair<ll,ll> nans = {i,pl};
            ans = vmin(ans,nans);
          }

        }
        sang += sch ; sang %= mod;
        mang += mch ; mang %= mod;
        hang += hch ; hang %= mod;
      }
    }



    { // 119H-1
      ll mod = 3600 * H;
      ll hang = ( (3600*h + 60*m + s) * 1) % mod;
      ll mang = ( (60*m+s) * H ) % mod;
      ll sang = ( s * (60*H)   ) % mod;
      ll hch  = 1;
      ll mch  = H;
      ll sch  = 60*H;
      ll pl = 119*H-1;
      mod *= pl;
      hang *= pl;
      mang *= pl;
      sang *= pl;

      rep(i,0,120*pl){

//        if (i == 43200) {
//          cout << mod << endl;
//          cout << i << " " << pl << " " << sang << " " << mang << " " << hang << endl;
//          cout << sang - mang << " " << sang - hang << endl;
//        }
        if ( modplus(sang-mang, mod) == modplus(sang-hang, mod) ||  modplus(sang-mang+sang-hang, mod) == 0){

          if ( (sang % mod != mang % mod && mang % mod != hang % mod && sang % mod != hang % mod) ){
            pair<ll,ll> nans = {i,pl};
            ans = vmin(ans,nans);
          }


        }
        sang += sch ; sang %= mod;
        mang += mch ; mang %= mod;
        hang += hch ; hang %= mod;
      }
    }

    ll su = s*ans.second + ans.first;
    ll sd = ans.second;

    //cerr << ans.first << " " << ans.second << endl;

    ll plusm = (su/sd)/60;
    su -= plusm * 60 * sd;

    m += plusm;


    while (m >= 60){
      m -= 60;
      h += 1;
    }
    h %= H;

    ll g = __gcd(su,sd);
    su /= g;
    sd /= g;

    assert (INT_MAX-1000 >= ans.first);
    printf ("%lld %lld %lld %lld\n",h,m,su,sd);

  }


}
accuracy: 1
time_ms: 7330
memory_kb: 3496
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------ End of template --------------

#define endl "\n"

bool solve() {
  ll H, h0, m0, s0;
  cin >> H >> h0 >> m0 >> s0;
  if ((H | h0 | m0 | s0) == 0) return false;

  ll X = 119 * H - 1;
  ll T = (60ll * 60 * h0 + 60 * m0 + s0) * X;
  ll round = X * 60ll * 60 * H;

  auto check = [&](ll t) {
    ll x = t % round;
    ll y = (t * H) % round;
    ll z = (t * 60 * H) % round;
    if (x == y) return false;
    if ((x + y) % round == (z * 2 % round)) return true;
    return false;
  };

  while (!check(T)) { T++; }

  dmp(T);

  ll h = (T / (X * 60 * 60)) % H;
  ll m = (T / (X * 60)) % 60;
  ll n = T % (X * 60);
  ll d = X;

  ll g = gcd(n, d);
  n /= g;
  d /= g;

  cout << h << ' ' << m << ' ' << n << ' ' << d << endl;

  return true;
}

int main() {
  fastio();
  while (solve()) {}
  // int t;
  // cin >> t;
  // while (t--) solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 3376
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

// BEGIN CUT
// 分数ライブラリ
// 常に約分されているとする
// 負のときは常にaを負にする
struct fraction {
    ll a, b;
    fraction(ll x = 0, ll y = 1) : a(x), b(y) {
        ll g = __gcd(a, b);
        a /= g, b /= g;
        if (b < 0)
            a *= -1, b *= -1;
    }
    // comparator
    bool operator<(fraction r) const { return a * r.b < b * r.a; }
    bool operator>(fraction r) const { return a * r.b > b * r.a; }
    bool operator<=(fraction r) const { return a * r.b <= b * r.a; }
    bool operator>=(fraction r) const { return a * r.b >= b * r.a; }
    bool operator==(fraction r) const { return a * r.b == b * r.a; }
    bool operator!=(fraction r) const { return a * r.b != b * r.a; }
    // Basic Operations
    fraction operator+(fraction r) const { return fraction(*this) += r; }
    fraction operator-(fraction r) const { return fraction(*this) -= r; }
    fraction operator*(fraction r) const { return fraction(*this) *= r; }
    fraction operator/(fraction r) const { return fraction(*this) /= r; }
    fraction &operator+=(fraction r) {
        ll g = __gcd(abs(a * r.b + b * r.a), b * r.b);
        ll na = (a * r.b + b * r.a) / g, nb = (b * r.b) / g;
        a = na, b = nb;
        return *this;
    }
    fraction &operator-=(fraction r) {
        ll g = __gcd(abs(a * r.b - b * r.a), b * r.b);
        ll na = (a * r.b - b * r.a) / g, nb = (b * r.b) / g;
        a = na, b = nb;
        return *this;
    }
    fraction &operator*=(fraction r) {
        ll g = __gcd(a * r.a, b * r.b);
        a = a * r.a / g, b = b * r.b / g;
        return *this;
    }
    fraction &operator/=(fraction r) {
        ll g = __gcd(a * r.b, b * r.a);
        a = a * r.b / g, b = b * r.a / g;
        if (b < 0)
            a *= -1, b *= -1;
        return *this;
    }
    friend fraction abs(fraction a) {
        a.a = abs(a.a);
        return a;
    }
    // output
    friend ostream &operator<<(ostream &os, fraction a) {
        return os << a.a << "/" << a.b;
    }
};
// END CUT

using P = pair<fraction, bool>;

int main() {
    while (1) {
        int H, h, m, s;
        cin >> H >> h >> m >> s;
        if (H == 0)
            break;
        int day = H * 3600;
        fraction vh = fraction(1, 10 * H);
        fraction vm = fraction(1, 10);
        fraction vs = fraction(6);
        vector<P> vec;
        //秒針と分針
        auto diff = fraction(m * 60 + s, 10) - fraction(s * 6);
        if (diff <= 0)
            diff += 360;
        auto t = diff / (vs - vm);
        while (t <= day) {
            vec.emplace_back(t, 0);
            t += fraction(360) / (vs - vm);
        }
        diff += 180;
        if (diff > 360)
            diff -= 360;
        t = diff / (vs - vm);
        while (t <= day) {
            vec.emplace_back(t, 0);
            t += fraction(360) / (vs - vm);
        }
        //秒針と時針
        diff = fraction(h * 3600 + m * 60 + s, 10 * H) - fraction(s * 6);
        if (diff <= 0)
            diff += 360;
        t = diff / (vs - vh);
        while (t <= day) {
            vec.emplace_back(t, 1);
            t += fraction(360) / (vs - vh);
        }
        diff += 180;
        if (diff > 360)
            diff -= 360;
        t = diff / (vs - vh);
        while (t <= day) {
            vec.emplace_back(t, 1);
            t += fraction(360) / (vs - vh);
        }
        sort(ALL(vec));
        auto diff_m = fraction(m * 60 + s, 10) - fraction(s * 6);
        vm = vs - vm;
        if (diff_m < 0)
            diff_m += 360;
        if (diff_m > 180) {
            diff_m = fraction(360) - diff_m;
        } else {
            vm *= -1;
        }
        auto diff_h = fraction(h * 3600 + m * 60 + s, 10 * H) - fraction(s * 6);
        vh = vs - vh;
        if (diff_h < 0)
            diff_h += 360;
        if (diff_h > 180) {
            diff_h = fraction(360) - diff_h;
        } else {
            vh *= -1;
        }
        fraction prev = 0;
        fraction ans;
        for (P p : vec) {
            auto T = p.first - prev;
            if ((vm < 0 && vh > 0) || (vm > 0 && vh < 0)) {
                auto x = (diff_m - diff_h) / (vh - vm);
                if (diff_m + vm * x != 0 && x >= 0 && x <= T) {
                    ans = prev + x;
                    break;
                }
            }
            diff_m += vm * T;
            diff_h += vh * T;
            if (p.second)
                vh *= -1;
            else
                vm *= -1;
            prev = p.first;
        }
        ans += 3600 * h + 60 * m + s;
        if (ans >= day)
            ans -= day;
        ll sec = ans.a / ans.b;
        ans -= sec;
        ans += sec % 60;
        cout << sec / 3600 << " " << (sec % 3600) / 60 << " " << ans.a << " "
             << ans.b << endl;
    }
}
accuracy: 1
time_ms: 410
memory_kb: 4356
final_rank: 18

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

long long int dif(long long int a, long long int b, long long int mx) {
	return min(abs(a - b), mx - abs(a - b));
}

long long int gcd(long long int a, long long int b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

void Output(long long int p) {
	cout << p / (119 * H - 1)/3600 << " ";
	p %= (119 * H - 1) * 3600;
	cout << p / (119 * H - 1) / 60 << " ";
	p %= (119 * H - 1) * 60;
	long long int g = gcd(p, 119 * H - 1);
	cout << p / g << " " << (119 * H - 1) / g << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int h, m, s;
	while (cin >> H >> h >> m >> s, H) {
		int by = 119 * H - 1;
		long long int st = h * 3600 + m * 60 + s;
		long long int mod = by * H * 60 * 60;
		st *= by;
		long long int hp = st;
		long long int mp = hp * H;
		long long int sp = mp * 60;
		mp %= mod;
		sp %= mod;
		do {
		//	cout << st << " " << hp << " " << mp << " " << sp << endl;
			if (dif(hp, sp, mod) == dif(mp, sp, mod) && dif(hp, mp, mod)) {
				Output(st);
				break;
			}
			st++;
			st %= mod;
			hp++;
			hp %= mod;
			mp += H;
			mp %= mod;
			sp += 60 * H;
			sp %= mod;
		} while (1);
	}
}
accuracy: 1
time_ms: 210
memory_kb: 3096
final_rank: 3

code_snippet: // verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1338

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T>
struct fraction{
  T num,dom;
  fraction(T n,T d):num(n),dom(d){
    assert(dom!=0);
    if(dom<0) num*=-1,dom*=-1;
    T tmp=__gcd(abs(num),abs(dom));
    num/=tmp;
    dom/=tmp;
  }
  fraction operator+(const fraction a) const{
    return fraction(num*a.dom+a.num*dom,dom*a.dom);
  }
  fraction operator-(const fraction a) const{
    return fraction(num*a.dom-a.num*dom,dom*a.dom);
  }
  fraction operator*(const fraction a) const{
    return fraction(num*a.num,dom*a.dom);
  }
  fraction operator/(const fraction a){
    return fraction(num*a.dom,dom*a.num);
  }
  fraction operator*(T k) const{return fraction(num*k,dom);}
  fraction operator/(T k) const{return fraction(num,dom*k);}
#define define_cmp(op) \
  bool operator op (const fraction a)const{return num*a.dom op a.num*dom;}
  define_cmp(==)
  define_cmp(!=)
  define_cmp(<)
  define_cmp(>)
  define_cmp(<=)
  define_cmp(>=)
#undef define_cmp
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#undef call_from_test

using ll = long long;
using frac = fraction<ll>;

int H,h,m,s;
void print(frac f){
  int t=f.num/(f.dom*60);
  cout<<(t%(60*H))/60<<" ";
  cout<<(t%60)<<" ";
  cout<<(f.num)%(f.dom*60)<<" "<<f.dom<<endl;
}

frac norm2(frac a){
  if(a.num==0) return frac(0,1);
  while(a.num<0) a.num+=a.dom;
  while(a.num>=a.dom) a.num-=a.dom;
  ll tmp=__gcd(a.num,a.dom);
  return frac(a.num/tmp,a.dom/tmp);
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  while(cin>>H>>h>>m>>s,H){
    const frac base(h*3600+m*60+s,1);
    const frac vh(1,3600*H),vm(1,3600),vs(1,60);
    frac t(max(h*3600+m*60+s-100,0),1);
    while(1){
      frac x=norm2(t*vh);
      frac y=norm2(t*vm);
      frac z=norm2(t*vs);
      if(y<x) y=y+frac(1,1);
      if(z<x) z=z+frac(1,1);
      frac ans(10000000,1);
      {
        frac t1=frac(1,1)-(z-x);
        frac t2=z-y;
        frac tmp=t+(t1-t2)/(vs*2-(vh+vm));
        if(base<=tmp){
          frac a=norm2(tmp*vh);
          frac b=norm2(tmp*vm);
          frac c=norm2(tmp*vs);
          if(b<a) b=b+frac(1,1);
          if(c<a) c=c+frac(1,1);
          if(b<c){
            t1=frac(1,1)-(c-a);
            t2=c-b;
            if(a!=b&&b!=c&&c!=a&&t1==t2)
              ans=min(ans,tmp);
          }
        }
      }
      {
        frac t1=z-x;
        frac t2=y-z;
        frac tmp=t+(t2-t1)/(vs*2-(vh+vm));
        if(base<=tmp){
          frac a=norm2(tmp*vh);
          frac b=norm2(tmp*vm);
          frac c=norm2(tmp*vs);
          if(b<a) b=b+frac(1,1);
          if(c<a) c=c+frac(1,1);
          if(b>c){
            t1=c-a;
            t2=b-c;
            if(a!=b&&b!=c&&c!=a&&t1==t2)
              ans=min(ans,tmp);
          }
        }
      }
      if(ans!=frac(10000000,1)){
        print(ans);
        break;
      }
      t=t+frac(1,1);
    }
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3224
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=100005,INF=1<<30;

ll gcd(ll a,ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        ll H,h,m,s;cin>>H>>h>>m>>s;
        
        if(H==0) break;
        
        ll d=119*H-1,l=3600*H*d;
        ll nowh=(3600*h+60*m+s)*d,nowm=(60*m*H+s*H)*d,nows=(60*s*H)*d;
        for(int t=0;;t++){
            if(t){
                nowh+=1;
                nowm+=H;
                nows+=60*H;
                
                if(nowh>=l) nowh-=l;
                if(nowm>=l) nowm-=l;
                if(nows>=l) nows-=l;
            }
            
            ll d1=abs(nowh-nows);
            if(d1>l/2) d1=l-d1;
            
            ll d2=abs(nowm-nows);
            if(d2>l/2) d2=l-d2;
            
            if(nowh==nowm) continue;
            if(nowm==nows) continue;
            if(nows==nowh) continue;
            
            if(d1==d2){
                ll x=t/d;
                s+=x;
                m+=s/60;
                s%=60;
                
                h+=m/60;
                m%=60;
                
                h%=H;
                
                t%=d;
                
                ll g=gcd(t,d);
                t/=g;
                d/=g;
                
                cout<<h<<" "<<m<<" "<<s*d+t<<" "<<d<<endl;
                break;
            }
        }
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3140
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
int H, ha, ma, sa;

int gcd(int a, int b) {
    if(a == 0) return b;
    if(b == 0) return a;
    if(a < b) swap(a, b);
    if(a % b == 0) return b;
    return gcd(b, a % b);
}

struct Clock {
    int h, m, n, d;
    Clock(int _h, int _m, int _n, int _d) {
        h = _h;
        m = _m;
        n = _n;
        d = _d;
        int g = gcd(n, d);
        n /= g;
        d /= g;
    }
};

bool operator <(Clock a, Clock b) {
    if(a.h != b.h) return a.h < b.h;
    if(a.m != b.m) return a.m < b.m;
    return a.n * b.d < b.n * a.d;
}

bool operator <=(Clock a, Clock b) {
    if(a.h != b.h) return a.h < b.h;
    if(a.m != b.m) return a.m < b.m;
    return a.n * b.d <= b.n * a.d;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    while(true) {
        cin >> H >> ha >> ma >> sa;
        if(H == 0 && ha == 0 && ma == 0 && sa == 0) break;
        Clock a(ha, ma, sa, 1);
        Clock ans(-1, -1, 0, 1);
        for(int h = 0; h < H; h++) {
            for(int m = 0; m < 60; m++) {
                for(int delta = -1; delta <= 1; delta++) {
                    int Left = delta * 3600 * H + 60 * 60 * h + 60 * m * (H + 1);
                    if(Left < 0) continue;
                    if(Left >= 60 * (H * 120 - H - 1)) continue;
                    Clock now(h, m, Left, H * 120 - H - 1);
                    if(60 * now.m * now.d == 59 * now.n) continue;
                    if(ans.h == -1) {
                        ans = now;
                    } else if(ans < a && a <= now) {
                        ans = now;
                    } else if(a <= now && now < ans) {
                        ans = now;
                    } else if(now <= ans && ans < a) {
                        ans = now;
                    }
                }
            }
        }
        cout << ans.h << " " << ans.m << " " << ans.n << " " << ans.d << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3200
final_rank: 7

code_snippet: #include<iostream>
using namespace std;
long gcd(long a,long b){return b?gcd(b,a%b):a;}
int H,h,m,s;
bool check(long H,long h,long m,long s)
{
	for(int r=-360;r<=360;r++)
	{
		long A=r*360*10*H+60*m*(H+1)+3600*h;
		long B=119*H-1;
		long d=gcd(A,B);
		A/=d;
		B/=d;
		if(!(A>=60*B||s*B>A)&&!(h==0&&m==0&&A==0))
		{
			cout<<h<<" "<<m<<" "<<A<<" "<<B<<endl;
			return true;
		}
	}
	return false;
}
main()
{
	while(cin>>H>>h>>m>>s,H)
	{
		while(!check(H,h,m,s))
		{
			s++;
			if(s==60)m++,s=0;
			if(m==60)h++,m=0;
			if(h==H)h=0;
		}
	}
}
accuracy: 1
time_ms: 30
memory_kb: 3008
final_rank: 1

code_snippet: #include<iostream>
using namespace std;
long gcd(long a,long b){return b?gcd(b,a%b):a;}
int H,h,m,s;
bool check(long H,long h,long m,long s)
{
	for(int r=-360;r<=360;r++)
	{
		long A=r*360*10*H+60*m*(H+1)+3600*h;
		long B=119*H-1;
		long d=gcd(A,B);
		A/=d;
		B/=d;
		if(!(A>=60*B||s*B>A)&&!(h==0&&m==0&&A==0))
		{
			cout<<h<<" "<<m<<" "<<A<<" "<<B<<endl;
			return true;
		}
	}
	return false;
}
main()
{
	while(cin>>H>>h>>m>>s,H)
	{
		while(!check(H,h,m,s))
		{
			s++;
			if(s==60)m++,s=0;
			if(m==60)h++,m=0;
			if(h==H)h=0;
		}
	}
}
accuracy: 1
time_ms: 30
memory_kb: 3072
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16

ll t;
ll H,h0,m0,s0;

int main(){
  while(1){
    cin>>H>>h0>>m0>>s0;
    if(H==0)break;
    ll d=119*H-1;
    ll n=s0*d;
    ll h=h0,m=m0;
    ll mod=360*10*H*d;
    while(1){
      ll ang_h=(3600*h*d+60*m*d+n)%mod;
      ll ang_m=(60*H*m*d+H*n)%mod;
      ll ang_s=(60*H*n)%mod;
      ang_h=(ang_h-ang_s+mod)%mod;
      ang_m=(ang_m-ang_s+mod)%mod;
      if(ang_m!=ang_h&&min(ang_m,ang_h)==mod-max(ang_m,ang_h))break;
      n++;
      if(n==60*d)n=0,m++;
      if(m==60)m=0,h++;
      if(h==H)h=0;
    }
    ll g=__gcd(n,d);
    cout<<h<<" "<<m<<" "<<n/g<<" "<<d/g<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 3080
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pl = pair<ll,ll>;

struct TIME{
    ll h,m;
    pl s;
};

void PRINT_T(TIME t){
    printf("%lld %lld %lld %lld\n", t.h, t.m, t.s.fi, t.s.se);
}

const ll INF = 60*60*100*10;

bool big(pl a, pl b){
    return a.fi*b.se < b.fi*a.se;
}

pl norm(pl a){
    ll G = __gcd(a.fi, a.se);
    a.fi /= G;
    a.se /= G;
    return a;
}

int H,x,y,z;

// a から b
pl sub(TIME a, TIME b){
    pl p = a.s;
    p.fi += p.se*60*(60*a.h+a.m);

    pl q = b.s;
    q.fi += q.se*60*(60*b.h+b.m);

    while(big(q,p)) q.fi += H*3600*q.se;
    p = norm(p);
    q = norm(q);

    ll X = q.fi*p.se - p.fi*q.se;
    ll Y = q.se*p.se;
    pl res(X,Y);
    res = norm(res);

    // printf(" DIFF:\n");
    // PRINT_T(a);
    // PRINT_T(b);
    // dbg(res);
    return res;
}

int main(){
    while(cin >>H >>x >>y >>z,H){
        ll T = 60*H;

        TIME base = {x,y,{z,1}};

        pl diff(INF,1);
        TIME ans;

        auto upd = [&](ll p, ll q, int i, int j){
            ll t = 60*i+j;
            if(p>=0 && p/q<60){
                pl s = norm({p,q});
                // printf(" CAND %d %d %lld/%lld :", i,j,s.fi,s.se);

                pl th(60*t*s.se+s.fi, 60*T*s.se);
                pl tm(60*j*s.se+s.fi, 3600*s.se);
                pl ts(s.fi, s.se*60);
                th = norm(th);
                tm = norm(tm);
                ts = norm(ts);

                if( th!=tm && th!=ts && tm!=ts ){
                    TIME tmp = {i,j,s};
                    pl td = sub(base, tmp);
                    if(big(td,diff)){
                        diff = td;
                        ans = tmp;
                    }
                    // printf("OK");
                }
                // printf("\n");
            }
        };

        rep(i,H)rep(j,60){
            ll t = 60*i+j;

            // 1つめ
            ll p = 3600*t - 60*T*j;
            ll q = T - 60;
            upd(p,q,i,j);

            // 2つめ
            p = 3600*t + 60*T*j;
            q = 119*T - 60;
            upd(p,q,i,j);

            // 3つめ
            p = 3600*(T+t) + 60*T*j;
            q = 119*T - 60;
            upd(p,q,i,j);

            // 4つめ
            p = 3600*(t-T) + 60*T*j;
            q = 119*T - 60;
            upd(p,q,i,j);
        }

        PRINT_T(ans);
    }
    return 0;
}
accuracy: 1
time_ms: 680
memory_kb: 3092
final_rank: 6

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


ll calc(ll x,ll y){

	if(x < y)swap(x,y);

	if(y == 0){
		return x;
	}else{
		return calc(y,x%y);
	}
}


int main(){

	int H,h,m,s;

	while(true){
		scanf("%d %d %d %d",&H,&h,&m,&s);
		if(H == 0 && h == 0 && m == 0 && s == 0)break;

		ll bunbo = 119*H-1;
		ll bunshi = s*bunbo;
		ll h0 = h;
		ll m0 = m;
		ll deg_h,deg_m,deg_s,diff_1,diff_2;
		ll base;

		while(true){

			base = 3600*H*bunbo;

			deg_h = (3600*h0*bunbo+60*m0*bunbo+bunshi)%base;
			deg_m = (60*H*m0*bunbo+bunshi*H)%base;
			deg_s = (60*H*bunshi)%base;

			if(deg_h != deg_m){
				diff_1 = (deg_s-deg_h+base)%base;
				diff_2 = (deg_m-deg_s+base)%base;
				if(diff_1 == diff_2)break;
			}

			bunshi++;
			if(bunshi/60 == bunbo) {
				bunshi = 0;
				m0++;
			}
			if(m0 == 60) {
				m0 = 0;
				h0++;
			}
			if(h0 == H) {
				h0 = 0;
			}
		}
		ll common = calc(bunshi,bunbo);
		bunshi /= common;
		bunbo /= common;

		printf("%lld %lld %lld %lld\n",h0,m0,bunshi,bunbo);
	}
	return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 3176
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);

int H, h, m, s;

//! x / y
class Frac{
public:
  using LL = long long;
  LL x, y;

  Frac(LL x = 0, LL y = 1) : x(x), y(y) { red(); }

  LL gcd(LL x, LL y){
	if(y == 0) return x;
	return gcd(y, x%y);
  }
  
  void red(){
	if(x == 0){
	  y = 1;
	  return;
	}
	if(y < 0){
	  x *= -1;
	  y *= -1;
	}
	
	LL g = gcd(abs(x), y);
	x /= g;
	y /= g;
  }

  Frac& operator += (const Frac& p){ x = x * p.y + y * p.x; y *= p.y; red(); return *this;}
  Frac operator + (const Frac& p) const{ Frac q = *this;return q += p;}
  Frac operator - () const { Frac q = *this; q.x *= -1; return q; }
  Frac& operator -= (const Frac& p){ Frac q = -p; *this += q; return *this;}
  Frac operator - (const Frac& p) const{ Frac q = *this; return q -= p;}
  Frac& operator *= (const Frac& a){ x *= a.x; y *= a.y; red(); return *this;}
  Frac operator * (Frac a) const{ Frac q = *this; return q *= a;}
  Frac& operator /= (const Frac& a){ x *= a.y; y *= a.x; red(); return *this;}
  Frac operator / (Frac a) const{ Frac q = *this; return q /= a;}

  bool operator < (const Frac &p) const{
	return x * p.y < y * p.x;
  }
  bool operator <= (const Frac &p) const{
	return *this < p || *this == p;
  }
  bool operator > (const Frac &p) const{
	return p < *this;
  }
  bool operator >= (const Frac &p) const{
	return *this > p || *this == p;
  }
  bool operator == (const Frac &p) const {
	return x == p.x && y == p.y;
  }
  double getR() const { return x * 1. / y; }
  friend ostream& operator <<(ostream& os, const Frac& p);
};
Frac abs(const Frac& q){ return Frac(abs(q.x), q.y); }
ostream& operator <<(ostream& os, const Frac& p){
  return os << p.x << "/" << p.y;
}

void norm(Frac& t){
  if(t.x < 0) t += 3600*H;
  while(t >= 60 * 60 * H)
	t -= 60 * 60 * H;
}

void normR(Frac& t){
  if(t.x < 0) t += 360;
  while(t >= 360)
	t -= 360;
}

Frac dist(Frac& now, Frac& dst){
  if(now <= dst) return dst - now;
  return dst + 3600*H - now;
}

void print(Frac& ans){
  LL ah = ans.x / ans.y / 60 / 60;
  LL am = ans.x / ans.y / 60 % 60;
  Frac as = ans - ah*3600 - am*60;
  cout << ah << " " << am << " " << as.x << " " << as.y << endl;
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while(cin>>H>>h>>m>>s,H){
	Frac now(h*3600+m*60+s);
	
	Frac ans(1e9, 1), ans_dt(1e9, 1);
	REP(th,H) REP(tm,60){
	  for(int s1=-1;s1<=1;s1+=2){
		for(int s2=-1;s2<=1;s2+=2){
		  Frac c1(3600*th + 60*tm, 10*H);
		  Frac a1 = Frac(1, 10*H) - 6;
		  Frac c2(6*tm);
		  Frac a2 = Frac(1, 10) - 6;

		  for(int ang=0;ang<=360;ang+=360){
			Frac ss = (c2 * s2 - c1 * s1 + ang) / (a1 * s1 - a2 * s2);

			Frac rh = c1 + ss / (10*H);
			normR(rh);
			Frac rm = c2 + ss / 10;
			normR(rm);
			Frac rs = ss * 6;
			normR(rs);
			if(rh == rm || rh == rs || rm == rs) continue;
			Frac nxt = ss + 3600*th + 60*tm;
			norm(nxt);
			Frac dd = dist(now, nxt);
			if(dd < ans_dt){
			  ans_dt = dd;
			  ans = nxt;
			}
		  }
		}
	  }
	}
	print(ans);
  }

  return 0;
}
accuracy: 1
time_ms: 7030
memory_kb: 3124
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);

int H, h, m, s;

//! x / y
class Frac{
public:
  using LL = long long;
  LL x, y;

  Frac(LL x = 0, LL y = 1) : x(x), y(y) { red(); }

  LL gcd(LL x, LL y){
	if(y == 0) return x;
	return gcd(y, x%y);
  }
  
  void red(){
	if(x == 0){
	  y = 1;
	  return;
	}
	if(y < 0){
	  x *= -1;
	  y *= -1;
	}
	
	LL g = gcd(abs(x), y);
	x /= g;
	y /= g;
  }

  Frac& operator += (const Frac& p){ x = x * p.y + y * p.x; y *= p.y; red(); return *this;}
  Frac operator + (const Frac& p) const{ Frac q = *this;return q += p;}
  Frac operator - () const { Frac q = *this; q.x *= -1; return q; }
  Frac& operator -= (const Frac& p){ Frac q = -p; *this += q; return *this;}
  Frac operator - (const Frac& p) const{ Frac q = *this; return q -= p;}
  Frac& operator *= (const Frac& a){ x *= a.x; y *= a.y; red(); return *this;}
  Frac operator * (Frac a) const{ Frac q = *this; return q *= a;}
  Frac& operator /= (const Frac& a){ x *= a.y; y *= a.x; red(); return *this;}
  Frac operator / (Frac a) const{ Frac q = *this; return q /= a;}

  bool operator < (const Frac &p) const{
	return x * p.y < y * p.x;
  }
  bool operator <= (const Frac &p) const{
	return *this < p || *this == p;
  }
  bool operator > (const Frac &p) const{
	return p < *this;
  }
  bool operator >= (const Frac &p) const{
	return *this > p || *this == p;
  }
  bool operator == (const Frac &p) const {
	return x == p.x && y == p.y;
  }
  double getR() const { return x * 1. / y; }
  friend ostream& operator <<(ostream& os, const Frac& p);
};
Frac abs(const Frac& q){ return Frac(abs(q.x), q.y); }
ostream& operator <<(ostream& os, const Frac& p){
  return os << p.x << "/" << p.y;
}

void norm(Frac& t){
  if(t.x < 0) t += 3600*H;
  while(t >= 60 * 60 * H)
	t -= 60 * 60 * H;
}

void normR(Frac& t){
  if(t.x < 0) t += 360;
  while(t >= 360)
	t -= 360;
}

Frac dist(Frac& now, Frac& dst){
  if(now <= dst) return dst - now;
  return dst + 3600*H - now;
}

void print(Frac& ans){
  LL ah = ans.x / ans.y / 60 / 60;
  LL am = ans.x / ans.y / 60 % 60;
  Frac as = ans - ah*3600 - am*60;
  cout << ah << " " << am << " " << as.x << " " << as.y << endl;
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while(cin>>H>>h>>m>>s,H){
	Frac now(h*3600+m*60+s);
	
	Frac ans(1e9, 1), ans_dt(1e9, 1);
	REP(th,H) REP(tm,60){
	  for(int s1=-1;s1<=1;s1+=2){
		for(int s2=-1;s2<=1;s2+=2){
		  Frac c1(3600*th + 60*tm, 10*H);
		  Frac a1 = Frac(1, 10*H) - 6;
		  Frac c2(6*tm);
		  Frac a2 = Frac(1, 10) - 6;

		  for(int ang=0;ang<=360;ang+=360){
			Frac ss = (c2 * s2 - c1 * s1 + ang) / (a1 * s1 - a2 * s2);

			Frac rh = c1 + ss / (10*H);
			normR(rh);
			Frac rm = c2 + ss / 10;
			normR(rm);
			Frac rs = ss * 6;
			normR(rs);
			if(rh == rm || rh == rs || rm == rs) continue;
			Frac nxt = ss + 3600*th + 60*tm;
			norm(nxt);
			Frac dd = dist(now, nxt);
			if(dd < ans_dt){
			  ans_dt = dd;
			  ans = nxt;
			}
		  }
		}
	  }
	}
	LL ah = ans.x / ans.y / 60 / 60;
	LL am = ans.x / ans.y / 60 % 60;
	Frac as = ans - ah*3600 - am*60;
	cout << ah << " " << am << " " << as.x << " " << as.y << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 7010
memory_kb: 3160
final_rank: 19