I. Rise of Shadows
一天有HHH个小时,MMM分钟,问,有多少个整数分钟,满足时针与分针的角度≤α\le \alpha≤α,α=2πAHM\alpha = \frac{2 \pi A}{HM}α=HM2πA。
∑i=0H−1∑j=0M−1[∣2π(i×M+j)HM−2πjM∣≤2πAHM]∑i=0H−1∑j=0M−1[∣i×M+j−H×j∣≤A]H×M−∑i=0H−1∑j=0M−1[i×M+j−H×j>A]−∑i=0H−1∑j=0M−1[i×M+j−H×j<−A]\sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M - 1} [ \mid\frac{2 \pi(i \times M + j)}{HM} - \frac{2\pi j}{M} \ \mid \le \frac{2 \pi A}{HM}]\\ \sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M - 1} [\mid i \times M + j - H \times j \mid \le A]\\ H \times M - \sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M - 1} [i \times M + j - H \times j > A] - \sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M - 1} [i \times M + j - H \times j < -A]\\ i=0∑H−1j=0∑M−1[∣HM2π(i×M+j)−M2πj ∣≤HM2πA]i=0∑H−1j=0∑M−1[∣i×M+j−H×j∣≤A]H×M−i=0∑H−1j=0∑M−1[i×M+j−H×j>A]−i=0∑H−1j=0∑M−1[i×M+j−H×j<−A]
∑i=0H−1∑j=0M−1[i×M+j−H×j>A]∑i=0H−1∑j=0M−1[i>(H−1)×j+AM]∑j=0M−1(H−1−(H−1)×j+AM)M×(H−1)−∑i=0M−1(H−1)×i+AM\sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M - 1} [i \times M + j - H \times j > A]\\ \sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M - 1} [i > \frac{(H - 1) \times j + A}{M}]\\ \sum_{j = 0} ^{M - 1} \left(H - 1 - \frac{(H - 1) \times j + A}{M} \right)\\ M \times (H - 1) - \sum_{i = 0} ^{M - 1} \frac{(H - 1) \times i + A}{M}\\ i=0∑H−1j=0∑M−1[i×M+j−H×j>A]i=0∑H−1j=0∑M−1[i>M(H−1)×j+A]j=0∑M−1(H−1−M(H−1)×j+A)M×(H−1)−i=0∑M−1M(H−1)×i+A
∑i=0H−1∑j=0M−1[i×M+j−H×j<−A]∑i=0H−1(M−∑j=0M−1[i×M+j−H×j≥−A])H×M−∑i=0H−1∑j=0M−1[i×M+j−H×j>−A−1+(H+1)−(H+1)]H×M−∑j=0M−1∑i=0H−1[i>(H−1)×j+(H+1)M−A−1)M−(H+1)]H×M−∑j=0M−1H−1−(H−1)×j+(H+1)M−A−1M+H+1−HM+∑i=0M−1(H−1)×i+(H+1)M−A−1M\sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M - 1} [i \times M + j - H \times j < -A]\\ \sum_{i = 0} ^{H - 1} \left(M - \sum_{j = 0} ^{M - 1} [i \times M + j - H \times j \ge -A] \right)\\ H \times M - \sum_{i = 0} ^{H - 1} \sum_{j = 0} ^{M -1} [i \times M + j - H \times j > -A - 1 + (H + 1) - (H + 1)]\\ H \times M - \sum_{j = 0} ^{M - 1} \sum_{i = 0} ^{H - 1} [i > \frac{(H - 1) \times j +(H + 1)M - A - 1)}{M} - (H + 1)]\\ H \times M - \sum_{j = 0} ^{M - 1} H - 1 - \frac{(H - 1) \times j +(H + 1)M - A - 1}{M} + H + 1\\ -HM + \sum_{i = 0} ^{M - 1} \frac{(H - 1) \times i + (H + 1)M - A - 1}{M}\\ i=0∑H−1j=0∑M−1[i×M+j−H×j<−A]i=0∑H−1(M−j=0∑M−1[i×M+j−H×j≥−A])H×M−i=0∑H−1j=0∑M−1[i×M+j−H×j>−A−1+(H+1)−(H+1)]H×M−j=0∑M−1i=0∑H−1[i>M(H−1)×j+(H+1)M−A−1)−(H+1)]H×M−j=0∑M−1H−1−M(H−1)×j+(H+1)M−A−1+H+1−HM+i=0∑M−1M(H−1)×i+(H+1)M−A−1
综上,答案为:
M+HM+∑i=0M−1(H−1)×i+AM−∑i=0M−1(H−1)×i+(H+1)M−A+1MM + HM + \sum_{i = 0} ^{M - 1} \frac{(H - 1) \times i + A}{M} - \sum_{i = 0} ^{M - 1} \frac{(H - 1) \times i + (H + 1) M - A + 1}{M} M+HM+i=0∑M−1M(H−1)×i+A−i=0∑M−1M(H−1)×i+(H+1)M−A+1
#include <bits/stdc++.h>
#define int long longusing namespace std;long long f(long long a, long long b, long long c, long long n) {if (!a) {return (b / c) * (n + 1);}if (a >= c || b >= c) {return f(a % c, b % c, c, n) + (b / c) * (n + 1) + (a / c) * n * (n + 1) / 2;}long long m = (a * n + b) / c;return n * m - f(c, c - b - 1, a, m - 1);
}signed main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);int h, m, a;cin >> h >> m >> a;cout << min(m + h * m + f(h - 1, a, m, m - 1) - f(h - 1, (h + 1) * m - a - 1, m, m - 1), h * m) << "\n";return 0;
}