結果

問題 No.3356 A÷B問題
コンテスト
ユーザー SnowBeenDiding
提出日時 2025-11-14 22:10:42
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,016 bytes
コンパイル時間 9,588 ms
コンパイル使用メモリ 520,808 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-11-14 22:10:54
合計ジャッジ時間 8,973 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/all>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace atcoder;
using namespace std;

typedef long long ll;

#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

cpp_int f(int k) {
    cpp_int x = 1;
    rep(i, 0, k) x *= 10;
    return x;
}

string g(cpp_int x) {
    string s;
    while (x) {
        s += (char)('0' + (x % 10));
        x /= 10;
    }
    reverse(s.begin(), s.end());
    return s;
}

void solve(ll na, ll nb) {
    // double d = (double)na / nb;
    // cout << d << endl;
    if (nb < 0) {
        na *= -1;
        nb *= -1;
    }
    bool dec = false;
    if (na < 0) {
        dec = true;
        na *= -1;
    }
    cpp_int a = na, b = nb;
    int di = 20;
    a *= f(di);
    auto check = [&](cpp_int x) {
        if (a >= b * x)
            return 1;
        else
            return 0;
    };
    auto binary = [&]() {
        cpp_int ac = 0, wa = +f(30);
        cpp_int mid = ac + (wa - ac) / 2;
        while (wa - ac > 1) {
            if (check(mid))
                ac = mid;
            else
                wa = mid;
            mid = ac + (wa - ac) / 2;
        }
        return ac;
    };
    auto ans_num = binary();
    if (ans_num == 0) {
        cout << "0\n";
        return;
    }
    string ans = g(ans_num);
    while (ans.size() < di) ans = "0" + ans;
    string pre, suf;
    pre = ans;
    while (suf.size() < di) {
        suf += pre.back();
        pre.pop_back();
    }
    reverse(suf.begin(), suf.end());
    if (pre == "") pre = "0";
    if (dec) {
        cout << '-';
    }
    cout << pre << '.' << suf << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
    // rep(i, -10, 10) rep(j, -10, 10) {
    //     if (j == 0) continue;
    //     cout << i << '/' << j << endl;
    //     solve(i * 1e9, j * 1e9);
    //     cout << endl;
    // }
    ll a, b;
    cin >> a >> b;
    solve(a, b);
}
0