結果

問題 No.1236 長針と短針
ユーザー bokusunnybokusunny
提出日時 2022-03-06 14:11:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,651 bytes
コンパイル時間 2,111 ms
コンパイル使用メモリ 201,564 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 05:09:39
合計ジャッジ時間 4,076 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);

// 有理数クラス
// 初期化 O(logN) (NOTE:重い場合、約分の必要がないのであればlogを取れる)
// 分母分子が両方とも0でない → マイナスは分子に寄せる&約分
// 分母が0 → 未定義
// 分子が0 → 分母は1で統一
struct Rational {
 private:
  long long NUM;   // numerator, 分子
  long long DENO;  // denominator, 分母

  void normalize() {
    if (NUM == 0) {
      DENO = 1;
    } else {
      // ---必要に応じてコメントアウトするとlogが落ちる---
      auto g = gcd(NUM, DENO);
      NUM /= g;
      DENO /= g;
      // ------------------------------------------

      if (DENO < 0) {
        NUM *= -1;
        DENO *= -1;
      }
    }
  }

 public:
  Rational(long long num, long long deno) : NUM(num), DENO(deno) {
    normalize();
    // NOTE: num, deno共にintの範囲に収めないと、比較の際にオーバーフローする
    //       llを使いたい &&
    //       setやmapを使ってるだけの場合、unorderedに変えると比較演算子を使わないのでオーバーフローしない
    assert(NUM < INT_MAX);
    assert(DENO < INT_MAX);
  }
  Rational(long long n) : NUM(n), DENO(1){};

  long long num() const { return NUM; }
  long long deno() const { return DENO; }
  double to_d() { return (double)NUM / DENO; }

  bool operator<(const Rational &b) const { return NUM * b.DENO < b.NUM * DENO; }
  bool operator>(const Rational &b) const { return NUM * b.DENO > b.NUM * DENO; }
  bool operator<=(const Rational &b) const { return NUM * b.DENO <= b.NUM * DENO; }
  bool operator>=(const Rational &b) const { return NUM * b.DENO >= b.NUM * DENO; }
  bool operator==(const Rational &b) const { return NUM == b.NUM && DENO == b.DENO; }
  bool operator!=(const Rational &b) const { return !(*this == b); }
  Rational operator+=(const Rational &x);
  Rational operator+=(long long x);
  Rational operator-=(const Rational &x);
  Rational operator-=(long long x);
  Rational operator*=(const Rational &x);
  Rational operator*=(long long x);
  Rational operator/=(const Rational &x);
  Rational operator/=(long long x);
};
inline Rational operator+(const Rational &a, const Rational &b) {
  long long num = a.num() * b.deno() + a.deno() * b.num();
  long long deno = a.deno() * b.deno();
  return Rational(num, deno);
}
inline Rational operator-(const Rational &a, const Rational &b) {
  long long num = a.num() * b.deno() - a.deno() * b.num();
  long long deno = a.deno() * b.deno();
  return Rational(num, deno);
}
inline Rational operator*(const Rational &a, const Rational &b) {
  long long num = a.num() * b.num();
  long long deno = a.deno() * b.deno();
  return Rational(num, deno);
}
inline Rational operator/(const Rational &a, const Rational &b) {
  long long num = a.num() * b.deno();
  long long deno = a.deno() * b.num();
  return Rational(num, deno);
}
inline Rational Rational::operator+=(const Rational &x) {
  *this = *this + x;
  return *this;
}
inline Rational Rational::operator+=(long long x) {
  *this = *this + Rational(x);
  return *this;
}
inline Rational Rational::operator-=(const Rational &x) {
  *this = *this - x;
  return *this;
}
inline Rational Rational::operator-=(long long x) {
  *this = *this - Rational(x);
  return *this;
}
inline Rational Rational::operator*=(const Rational &x) {
  *this = *this * x;
  return *this;
}
inline Rational Rational::operator*=(long long x) {
  *this = *this * Rational(x);
  return *this;
}
inline Rational Rational::operator/=(const Rational &x) {
  *this = *this / x;
  return *this;
}
inline Rational Rational::operator/=(long long x) {
  *this = *this / Rational(x);
  return *this;
}
inline ostream &operator<<(ostream &os, const Rational &f) {
  os << f.num();
  if (f.deno() != 1) os << "/" << f.deno();
  return os;
}

namespace std {
template <>
struct hash<Rational> {
  size_t operator()(const Rational &f) const {
    size_t seed = 0;
    // メンバ変数の数だけ行う
    auto num_hash = hash<long long>()(f.num());
    seed ^= num_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    auto deno_hash = hash<long long>()(f.deno());
    seed ^= deno_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);

    return seed;
  }
};
}  // namespace std

void solve() {
  int A, B;
  cin >> A >> B;
  A %= 12;
  Rational Long(60 * A + B, 12);
  Rational Short(B);

  if (Short > Long) {
    Long += 60;
  }

  auto x = (12 * (Long - Short)) * 60 / 11;
  cout << x.num() / x.deno() << endl;
}

int main() {
  bokusunny;
  solve();

  return 0;
}
0