結果
| 問題 |
No.2280 FizzBuzz Difference
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-04-21 22:37:47 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 2,000 ms |
| コード長 | 1,591 bytes |
| コンパイル時間 | 3,111 ms |
| コンパイル使用メモリ | 245,596 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-06 16:08:14 |
| 合計ジャッジ時間 | 3,744 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 7 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL exgcd(LL a, LL b, LL &x, LL &y) {
if (not b)
return x = 1, y = 0, a;
LL d = exgcd(b, a % b, x, y), t = x;
return x = y, y = t - a / b * y, d;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
for (cin >> t; t; t -= 1) {
LL M, A, B, K;
cin >> M >> A >> B >> K;
cout << [&]() {
if (K > A) {
return 0LL;
}
if (K == A) {
M = M / A * A;
return M / A - 1 - M / B + M / lcm(A, B);
}
LL x, y, ans = 0;
LL d = exgcd(B, A, x, y);
if (K % d == 0) {
LL rm = lcm(A, B) / B;
LL m = ((K / d) * x % rm + rm) % rm;
ans += M / B / rm + (m <= M / B % rm);
}
if ((A - K) % d == 0) {
LL rm = lcm(A, B) / B;
LL m = (((A - K) / d) * x % rm + rm) % rm;
ans += M / B / rm + (m <= M / B % rm);
if (M / B / rm + (m <= M / B % rm)) {
LL z = M / B;
if (z % rm >= m) {
z = z / rm * rm + m;
} else {
z = (z / rm - 1) * rm + m;
}
z *= B;
z = (z + A - 1) / A * A;
if (z > M) {
ans -= 1;
}
}
}
return ans;
}() << "\n";
}
}