結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-08-18 01:46:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,165 bytes
コンパイル時間 6,908 ms
コンパイル使用メモリ 313,328 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 06:20:58
合計ジャッジ時間 8,631 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream> // cin, cout, ios
#include <utility> // swap
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;

mp::cpp_int floor_sum_unsigned(mp::cpp_int n, mp::cpp_int m, mp::cpp_int a, mp::cpp_int b) {
    mp::cpp_int ans = 0;
    for(;;) {
        if (a >= m) {
            ans += (n * (n - 1) >> 1) * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }
        mp::cpp_int ymax = a * n + b;
        if (ymax < m) {
            return ans;
        }
        mp::divide_qr(ymax, m, n, b);
        std::swap(a, m);
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int q;
    std::cin >> q;
    for (int i = 0; i < q; i++) {
        mp::cpp_int n, d, m; int s;
        std::cin >> n >> d >> m >> s;
        mp::cpp_int pow2s = (mp::cpp_int)1 << s, dm = d * m;
        if (pow2s != dm) {
            n = mp::min(n, d * pow2s / mp::abs(dm - pow2s));
            n -= mp::abs(floor_sum_unsigned(n + 1, pow2s, m, 0) - floor_sum_unsigned(n + 1, d, 1, 0));
        }
        std::cout << n << '\n';
    }
    return 0;
}
0