結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-09-02 19:58:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,023 bytes
コンパイル時間 11,831 ms
コンパイル使用メモリ 363,036 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-09-02 19:58:56
合計ジャッジ時間 7,740 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 44 ms
4,368 KB
testcase_02 AC 43 ms
4,368 KB
testcase_03 AC 43 ms
4,368 KB
testcase_04 AC 43 ms
4,372 KB
testcase_05 AC 44 ms
4,372 KB
testcase_06 AC 35 ms
4,368 KB
testcase_07 AC 35 ms
4,368 KB
testcase_08 AC 36 ms
4,372 KB
testcase_09 AC 35 ms
4,368 KB
testcase_10 AC 35 ms
4,372 KB
testcase_11 AC 45 ms
4,372 KB
testcase_12 AC 45 ms
4,368 KB
testcase_13 AC 45 ms
4,368 KB
testcase_14 AC 44 ms
4,368 KB
testcase_15 AC 46 ms
4,372 KB
testcase_16 AC 25 ms
4,372 KB
testcase_17 AC 26 ms
4,372 KB
testcase_18 AC 25 ms
4,368 KB
testcase_19 AC 25 ms
4,372 KB
testcase_20 AC 25 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

bigint floor_sum(bigint n, bigint m, bigint a, bigint b) {
    assert(n >= 0 && m > 0 && a >= 0 && b >= 0);
    bigint x = 0, q;
    while(n > 0) {
        mp::divide_qr(a, m, q, a);
        x += q * (n * (n - 1) >> 1);
        mp::divide_qr(b, m, q, b);
        x += q * n;
        mp::divide_qr(a * n + b, m, n, b);
        std::swap(a, m);
    }
    return x;
}

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