結果

問題 No.2280 FizzBuzz Difference
ユーザー jianglyjiangly
提出日時 2023-04-21 22:03:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,645 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 195,696 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 08:11:35
合計ジャッジ時間 2,836 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;

i64 ceilDiv(i64 a, i64 b) {
    if (a >= 0) {
        return (a + b - 1) / b;
    } else {
        return a / b;
    }
}

i64 floorDiv(i64 a, i64 b) {
    if (a >= 0) {
        return a / b;
    } else {
        return (a - b + 1) / b;
    }
}
i64 exgcd(i64 a, i64 b, i64 &x, i64 &y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    i64 g = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
}

void solve() {
    i64 M, A, B, K;
    std::cin >> M >> A >> B >> K;
    
    if (K > A) {
        std::cout << "0\n";
        return;
    }
    
    if (K == A) {
        i64 ans = M / A - 1;
        ans -= M / B;
        ans += M / std::lcm(A, B);
        ans += M - M % B > M - M % A;
        std::cout << ans << "\n";
        return;
    }
    
    i64 x, y;
    i64 g = exgcd(A, B, x, y);
    if (K % g != 0) {
        std::cout << 0 << "\n";
        return;
    }
    
    A /= g, B /= g;
    K /= g;
    M /= g;
    
    x = 1LL * x * K % B;
    y = (K - A * x) / B;
    i64 L = A * B;
    
    i64 ans = 0;
    i64 l = std::max(ceilDiv(1 - A * x, L), ceilDiv(1 + B * y, L));
    i64 r = std::min(floorDiv(M - A * x, L), floorDiv(M + B * y, L));
    ans += std::max(0LL, r - l + 1);
    l = std::max(ceilDiv(1 + A * x, L), ceilDiv(1 - B * y, L));
    r = std::min(floorDiv(M + A * x, L), floorDiv(M - B * y, L));
    ans += std::max(0LL, r - l + 1);
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int T;
    std::cin >> T;
    
    while (T--) {
        solve();
    }
    
    return 0;
}
0