[yukicoder] No. 1236 長針と短針

2020年12月12日

問題

方針

\( A \leftarrow A \bmod 12 \) と再定義して考えます。分針は \( 1 \) 分で \(6^\circ \) 動き、時針は \( 1 \) 時間に \( 30^\circ \) 動くので、分針は \( 1 \) 秒で、

\[\dfrac{6}{60}\]

動き、時針は、

\[\dfrac{30}{3600}\]

動きます。したがって差は \( 1 \) 秒で、

\[\dfrac{6}{60} – \dfrac{30}{3600} = \dfrac{11}{120} \]

となるので、分針が追いつくことになります。\( 12 \) を原点とすると、分針の初期値 \( \theta_1 \)は、

\[ \theta_1 = 6B\]

であり、時針の初期値 \( \theta_2 \) は

\[ \theta_2 = 30A + \dfrac{30B}{60}\]

となります。\( \theta_1 \geq \theta_2 \) のとき、追いつくまでにかかる時間は分針が \( 1 \) 周してから追いつくことになるので、

\[ (360 – (\theta_1 – \theta_2)) \dfrac{120}{11})\]

となり、\( \theta_1 < \theta_2 \) のとき、追いつくまでにかかる時間は

\[ (\theta_2 – \theta_1)\dfrac{120}{11}\]

となります。

コード

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

int main() {
    int A, B;
    cin >> A >> B;
    A %= 12;
    int f1 = 2 * 6 * B;
    int f2 = 2 * 30 * A + B;
    if (f1 > f2) {
        cout << (720 - (f1 - f2)) * 60 / 11 << "\n";
    } else {
        cout << (f2 - f1) * 60 / 11 << "\n";
    }
    return 0;
}

コードでは、\( 2\theta_1 \) と \( 2\theta_2 \) で比較を行っています。