結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 Mizar
提出日時 2023-09-02 18:55:15
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,314 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,985 ms
コンパイル使用メモリ 364,984 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2026-03-05 05:37:06
合計ジャッジ時間 6,535 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from main.cpp:4:
In member function 'void boost::multiprecision::backends::cpp_int_base<MinBits, MaxBits, boost::multiprecision::signed_magnitude, Checked, Allocator, false>::resize(std::size_t, std::size_t) [with long unsigned int MinBits = 0; long unsigned int MaxBits = 18446744073709551615; boost::multiprecision::cpp_int_check_type Checked = boost::multiprecision::unchecked; Allocator = std::allocator<long long unsigned int>]',
    inlined from 'constexpr typename std::enable_if<(std::is_same<A, long long unsigned int>::value && (! std::integral_constant<bool, boost::multiprecision::backends::is_trivial_cpp_int<boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value>::value)), boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>&>::type boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>::operator=(A) [with A = long long unsigned int; long unsigned int MinBits = 0; long unsigned int MaxBits = 0; boost::multiprecision::cpp_integer_type SignType = boost::multiprecision::signed_magnitude; boost::multiprecision::cpp_int_check_type Checked = boost::multiprecision::unchecked; Allocator = std::allocator<long long unsigned int>]' at /usr/local/include/boost/multiprecision/cpp_int.hpp:1563:19,
    inlined from 'void boost::multiprecision::backends::left_shift_byte(Int&, boost::multiprecision::double_limb_type) [with Int = cpp_int_backend<>]' at /usr/local/include/boost/multiprecision/cpp_int/bitwise.hpp:347:14,
    inlined from 'constexpr typename std::enable_if<(! boost::multiprecision::backends::is_trivial_cpp_int<boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value)>::type boost::multiprecision::backends::eval_left_shift(cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>&, boost::multiprecision::double_limb_type) [with long unsigned int MinBits1 = 0; 

ソースコード

diff #
raw source code

#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);
    bigint x = 0, q;
    if(a < 0) {
        mp::divide_qr(a, m, q, a);
        if(a < 0) {
            q -= 1;
            a += m;
        }
        x += q * (n * (n - 1) >> 1);
    }
    if(b < 0) {
        mp::divide_qr(b, m, q, b);
        if(b < 0) {
            q -= 1;
            b += m;
        }
        x += q * n;
    }
    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