Problem p00917 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct frac {
  int num, dom;
  frac() {}
  frac(int num, int dom) : num(num), dom(dom) {}
  frac norm() {
    if (num == 0)
      return frac(0, 1);
    int tmp = __gcd(num, dom);
    return frac(num / tmp, dom / tmp);
  }
  frac norm2() {
    if (num == 0)
      return frac(0, 1);
    while (num < 0)
      num += dom;
    while (num >= dom)
      num -= dom;
    int tmp = __gcd(num, dom);
    return frac(num / tmp, dom / tmp);
  }
  frac operator+(frac a) {
    return frac(num * a.dom + a.num * dom, dom * a.dom).norm();
  }
  frac operator-(frac a) {
    return frac(num * a.dom - a.num * dom, dom * a.dom).norm();
  }
  frac operator*(frac a) { return frac(num * a.num, dom * a.dom).norm(); }
  frac operator*(int k) { return frac(num * k, dom).norm(); }
  frac operator/(frac a) { return frac(num * a.dom, dom * a.num).norm(); }
  bool operator<(const frac a) const { return num * a.dom < a.num * dom; }
  bool operator>(const frac a) const { return num * a.dom > a.num * dom; }
  bool operator==(const frac a) const { return num * a.dom == a.num * dom; }
  bool operator!=(const frac a) const { return num * a.dom != a.num * dom; }
  bool operator<=(const frac a) const { return num * a.dom <= a.num * dom; }
  bool operator>=(const frac a) const { return num * a.dom >= a.num * dom; }
};

int H, h, m, s;
void print(frac f) {
  // cout<<f.num<<":"<<f.dom<<endl;
  int t = f.num / (f.dom * 60);
  cout << (t % (60 * H)) / 60 << " ";
  cout << (t % 60) << " ";
  cout << (f.num) % (f.dom * 60) << " " << f.dom << endl;
}
signed main() {
  while (cin >> H >> h >> m >> s, H) {
    frac base(h * 3600 + m * 60 + s, 1),
        t(max(h * 3600 + m * 60 + s - 100, 0LL), 1);
    frac x, y, z;
    frac vh(1, 3600 * H), vm(1, 3600), vs(1, 60);
    while (1) {
      x = (t * vh).norm2();
      y = (t * vm).norm2();
      z = (t * vs).norm2();
      if (y < x)
        y = y + frac(1, 1);
      if (z < x)
        z = z + frac(1, 1);
      frac ans(10000000, 1), t1, t2;
      if (1) {
        t1 = frac(1, 1) - (z - x);
        t2 = z - y;
        if (1) {
          frac tmp = t + (t1 - t2) / (vs * 2 - (vh + vm));
          if (base <= tmp) {
            frac a, b, c;
            a = (tmp * vh).norm2();
            b = (tmp * vm).norm2();
            c = (tmp * vs).norm2();
            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);
            }
          }
        }
      }
      if (1) {
        t1 = z - x;
        t2 = y - z;
        if (1) {
          frac tmp = t + (t2 - t1) / (vs * 2 - (vh + vm));
          if (base <= tmp) {
            frac a, b, c;
            a = (tmp * vh).norm2();
            b = (tmp * vm).norm2();
            c = (tmp * vs).norm2();
            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);
            }
          }
        }
      }
      // assert(ans!=frac(10000000,1));
      if (ans != frac(10000000, 1)) {
        print(ans);
        break;
      }
      t = t + frac(1, 1);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int __int128
struct frac {
  int num, dom;
  frac() {}
  frac(int num, int dom) : num(num), dom(dom) {}
  frac norm() {
    if (num == 0)
      return frac(0, 1);
    int tmp = __gcd(num, dom);
    return frac(num / tmp, dom / tmp);
  }
  frac norm2() {
    if (num == 0)
      return frac(0, 1);
    while (num < 0)
      num += dom;
    while (num >= dom)
      num -= dom;
    int tmp = __gcd(num, dom);
    return frac(num / tmp, dom / tmp);
  }
  frac operator+(frac a) {
    return frac(num * a.dom + a.num * dom, dom * a.dom);
  }
  frac operator-(frac a) {
    return frac(num * a.dom - a.num * dom, dom * a.dom);
  }
  frac operator*(frac a) { return frac(num * a.num, dom * a.dom); }
  frac operator/(frac a) { return frac(num * a.dom, dom * a.num); }
  bool operator<(const frac a) const { return num * a.dom < a.num * dom; }
  bool operator>(const frac a) const { return num * a.dom > a.num * dom; }
  bool operator==(const frac a) const { return num * a.dom == a.num * dom; }
  bool operator!=(const frac a) const { return num * a.dom != a.num * dom; }
  bool operator<=(const frac a) const { return num * a.dom <= a.num * dom; }
  bool operator>=(const frac a) const { return num * a.dom >= a.num * dom; }
};

istream &operator>>(istream &is, int &x) {
  long long tmp;
  is >> tmp;
  x = tmp;
  return is;
}
ostream &operator<<(ostream &os, int x) {
  long long tmp = x;
  os << tmp;
  return os;
}

ostream &operator<<(ostream &os, frac x) {
  os << x.num << " " << x.dom;
  return os;
}

int H, h, m, s;
void print(frac f) {
  // cout<<f.num<<":"<<f.dom<<endl;

  int t = f.num / (f.dom * 60);

  cout << (t % (60 * H)) / 60 << " ";
  cout << (t % 60) << " ";
  cout << (f.num) % (f.dom * 60) << " " << f.dom << endl;
}
int MAX = 1500;
signed main() {
  vector<frac> v;
  v.push_back(frac(0, 1));
  for (int i = 1; i < MAX; i++) {
    for (int j = 1; j < i; j++) {
      if (__gcd(i, j) != 1)
        continue;
      v.push_back(frac(j, i));
    }
  }
  // cout<<v.size()<<endl;
  sort(v.begin(), v.end());

  while (cin >> H >> h >> m >> s, H) {
    frac t(h * 3600 + m * 60 + s, 1);
    frac x, y, z;
    bool flg = 0;
    // int cnt=0;
    // t=t+frac(30,1);
    // cnt=30;
    while (1) {
      if (1) {
        x = (t * frac(1, 3600 * H)).norm2();
        y = (t * frac(1, 3600)).norm2();
        z = (t * frac(1, 60)).norm2();
        if (y < x)
          y = y + frac(1, 1);
        if (z < x)
          z = z + frac(1, 1);
        if (y < z) {
          frac k = ((y - x).norm() + ((z - y).norm() * frac(2, 1)).norm());
          if (k == frac(1, 1)) {
            print(t);
            flg = 1;
          }
        }
        if (y > z) {
          if ((z - x) * frac(2, 1) == (y - x)) {
            print(t);
            flg = 1;
          }
        }
        if (flg)
          break;

        x = (t * frac(1, 3600 * H)).norm2();
        y = (t * frac(1, 3600)).norm2();
        frac p = t + frac(1, 1), a, b;
        a = (p * frac(1, 3600 * H)).norm2();
        b = (p * frac(1, 3600)).norm2();

        if ((x < y && a < b) || (x > y && a > b)) {
          int l = 0, r = v.size() + 1;
          // cout<<l<<" "<<r<<endl;
          while (l + 1 < r) {
            int m = (l + r) / 2;
            // cout<<"v[m] "<<v[m]<<endl;
            // cout<<"x "<<((t+v[m]).norm()*frac(1,3600*H))<<endl;
            // cout<<"y "<<((t+v[m]).norm()*frac(1,3600))<<endl;
            // cout<<"z "<<((t+v[m]).norm()*frac(1,60))<<endl;
            x = ((t + v[m]).norm() * frac(1, 3600 * H)).norm2();
            y = ((t + v[m]).norm() * frac(1, 3600)).norm2();
            z = ((t + v[m]).norm() * frac(1, 60)).norm2();
            if (y < x)
              y = y + frac(1, 1);
            if (z < x)
              z = z + frac(1, 1);
            if (z <= y) {
              l = m;
              continue;
            }
            frac k = ((y - x).norm() + ((z - y).norm() * frac(2, 1)).norm());
            // cout<<m<<":"<<(k<frac(1,1))<<endl;
            // cout<<k.num<<" "<<k.dom<<endl;
            if (k < frac(1, 1))
              l = m;
            else
              r = m;
          }
          if (r != (int)v.size() + 1) {
            // cout<<r<<endl;
            // cout<<v[r].num<<" "<<v[r].dom<<endl;
            x = ((t + v[r]).norm() * frac(1, 3600 * H)).norm2();
            y = ((t + v[r]).norm() * frac(1, 3600)).norm2();
            z = ((t + v[r]).norm() * frac(1, 60)).norm2();
            if (y < x)
              y = y + frac(1, 1);
            if (z < x)
              z = z + frac(1, 1);
            frac k = ((y - x).norm() + ((z - y).norm() * frac(2, 1)).norm());
            // cout<<k.num<<" "<<k.dom<<endl;
            if (x != y && y != z && z != x && k == frac(1, 1)) {
              print(t + v[r]);
              flg = 1;
            }
          }

          l = 0, r = v.size() + 1;
          while (l + 1 < r) {
            int m = (l + r) / 2;
            x = ((t + v[m]).norm() * frac(1, 3600 * H)).norm2();
            y = ((t + v[m]).norm() * frac(1, 3600)).norm2();
            z = ((t + v[m]).norm() * frac(1, 60)).norm2();
            if (y < x)
              y = y + frac(1, 1);
            if (z < x)
              z = z + frac(1, 1);
            if (y <= z) {
              r = m;
              continue;
            }
            if (((z - x) * frac(2, 1)).norm() <= (y - x).norm())
              l = m;
            else
              r = m;
          }
          if (l != 0) {
            x = ((t + v[l]).norm() * frac(1, 3600 * H)).norm2();
            y = ((t + v[l]).norm() * frac(1, 3600)).norm2();
            z = ((t + v[l]).norm() * frac(1, 60)).norm2();
            if (y < x)
              y = y + frac(1, 1);
            if (z < x)
              z = z + frac(1, 1);
            if (x != y && y != z && z != x &&
                ((z - x) * frac(2, 1)).norm() == (y - x).norm()) {
              print(t + v[l]);
              flg = 1;
            }
          }

          if (flg)
            break;
          t = t + frac(1, 1);
          // cout<<++cnt<<endl;
          continue;
        }
        t = t + frac(1, 1);
      }
      /*
      for(int i=0;i<(int)v.size();i++){
        x=((t+v[i]).norm()*frac(1,3600*H)).norm2();
        y=((t+v[i]).norm()*frac(1,3600)).norm2();
        z=((t+v[i]).norm()*frac(1,60)).norm2();
        //x<=y,x<=z
        if(y<x) y=y+frac(1,1);
        if(z<x) z=z+frac(1,1);

        if(x==y||y==z||z==x) continue;
        if(y<z){
          frac k=((y-x).norm()+((z-y).norm()*frac(2,1)).norm());
          if(k==frac(1,1)){
            print(t+v[i]);
            flg=1;
          }
        }
        if(y>z){
          if(((z-x)*frac(2,1)).norm()==(y-x).norm()){
            print(t+v[i]);
            flg=1;
          }
        }
        if(flg) break;
      }
      if(flg) break;


      t=t+frac(1,1);
      //cout<<++cnt<<endl;
      */
    }
  }
  return 0;
}