結果

問題 No.2280 FizzBuzz Difference
ユーザー Yaowei LyuYaowei Lyu
提出日時 2023-04-21 22:37:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,591 bytes
コンパイル時間 3,806 ms
コンパイル使用メモリ 274,720 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 08:44:35
合計ジャッジ時間 3,794 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 10 ms
5,376 KB
testcase_02 AC 14 ms
5,376 KB
testcase_03 AC 9 ms
5,376 KB
testcase_04 AC 15 ms
5,376 KB
testcase_05 AC 12 ms
5,376 KB
testcase_06 AC 12 ms
5,376 KB
testcase_07 AC 14 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
    }
}
0