結果

問題 No.782 マイナス進数
ユーザー outlineoutline
提出日時 2020-12-20 21:38:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 133,856 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 11:04:33
合計ジャッジ時間 3,699 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 7 ms
4,348 KB
testcase_03 AC 8 ms
4,348 KB
testcase_04 AC 10 ms
4,348 KB
testcase_05 AC 7 ms
4,348 KB
testcase_06 AC 7 ms
4,348 KB
testcase_07 AC 13 ms
4,348 KB
testcase_08 AC 8 ms
4,348 KB
testcase_09 AC 7 ms
4,348 KB
testcase_10 AC 7 ms
4,348 KB
testcase_11 AC 13 ms
4,348 KB
testcase_12 AC 12 ms
4,348 KB
testcase_13 AC 26 ms
4,348 KB
testcase_14 AC 12 ms
4,348 KB
testcase_15 AC 13 ms
4,348 KB
testcase_16 AC 12 ms
4,348 KB
testcase_17 AC 19 ms
4,348 KB
testcase_18 AC 11 ms
4,348 KB
testcase_19 AC 16 ms
4,348 KB
testcase_20 AC 12 ms
4,348 KB
testcase_21 AC 17 ms
4,348 KB
testcase_22 AC 13 ms
4,348 KB
testcase_23 AC 10 ms
4,348 KB
testcase_24 AC 26 ms
4,348 KB
testcase_25 AC 11 ms
4,348 KB
testcase_26 AC 15 ms
4,348 KB
testcase_27 AC 13 ms
4,348 KB
testcase_28 AC 12 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template<typename T>
vector<T> convert_base(T x, T b){
    vector<T> ret;
    T t = 1;        // 正負フラグ
    T k = abs(b);   // 基数の絶対値を取る
    while(x){
        ret.emplace_back((x * t) % k);          // 見ている桁の変換後の数を求める (mod(abs(b)))
        if(ret.back() < 0)  ret.back() += k;    // 負であれば正にする
        x -= ret.back() * t;    // 余りに正負フラグを掛けたものを減算してから
        x /= k;         // 除算処理をする
        t *= b / k;     // 負の進数へ変換する場合はこれが必要 (偶数桁目を調べているときは、t = -1 になる)
    }
    if(ret.empty()) ret.emplace_back(0);
    reverse(begin(ret), end(ret));
    return ret;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T, B, N;
    cin >> T >> B;
    for(int i = 0; i < T; ++i){
        cin >> N;
        auto ans = convert_base(N, B);
        for(auto c : ans)   cout << c;
        cout << '\n';
    }

    return 0;
}
0