結果

問題 No.782 マイナス進数
ユーザー outline
提出日時 2020-12-20 21:38:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 1,655 ms
コンパイル使用メモリ 129,540 KB
最終ジャッジ日時 2025-01-17 04:59:41
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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