結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-08-18 01:46:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,165 bytes
コンパイル時間 7,229 ms
コンパイル使用メモリ 312,980 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-02 09:58:50
合計ジャッジ時間 7,379 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 37 ms
5,248 KB
testcase_02 AC 38 ms
5,376 KB
testcase_03 AC 38 ms
5,376 KB
testcase_04 AC 38 ms
5,376 KB
testcase_05 AC 38 ms
5,376 KB
testcase_06 AC 30 ms
5,376 KB
testcase_07 AC 30 ms
5,376 KB
testcase_08 AC 30 ms
5,376 KB
testcase_09 AC 32 ms
5,376 KB
testcase_10 AC 32 ms
5,376 KB
testcase_11 AC 40 ms
5,376 KB
testcase_12 AC 40 ms
5,376 KB
testcase_13 AC 39 ms
5,376 KB
testcase_14 AC 40 ms
5,376 KB
testcase_15 AC 39 ms
5,376 KB
testcase_16 AC 21 ms
5,376 KB
testcase_17 AC 20 ms
5,376 KB
testcase_18 AC 21 ms
5,376 KB
testcase_19 AC 20 ms
5,376 KB
testcase_20 AC 21 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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