結果

問題 No.782 マイナス進数
ユーザー しらっ亭しらっ亭
提出日時 2017-05-13 00:29:56
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 977 bytes
コンパイル時間 1,171 ms
使用メモリ 6,280 KB
最終ジャッジ日時 2023-02-28 07:15:55
合計ジャッジ時間 3,523 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
6,268 KB
testcase_01 AC 1 ms
6,208 KB
testcase_02 AC 14 ms
6,280 KB
testcase_03 AC 13 ms
6,252 KB
testcase_04 AC 14 ms
6,196 KB
testcase_05 AC 13 ms
6,280 KB
testcase_06 AC 14 ms
6,192 KB
testcase_07 AC 15 ms
6,256 KB
testcase_08 AC 13 ms
6,196 KB
testcase_09 AC 13 ms
6,184 KB
testcase_10 AC 13 ms
6,276 KB
testcase_11 AC 15 ms
6,192 KB
testcase_12 AC 16 ms
6,204 KB
testcase_13 AC 19 ms
6,256 KB
testcase_14 AC 15 ms
6,184 KB
testcase_15 AC 14 ms
6,192 KB
testcase_16 AC 14 ms
6,176 KB
testcase_17 AC 16 ms
6,180 KB
testcase_18 AC 14 ms
6,280 KB
testcase_19 AC 15 ms
6,256 KB
testcase_20 AC 14 ms
6,184 KB
testcase_21 AC 16 ms
6,264 KB
testcase_22 AC 14 ms
6,268 KB
testcase_23 AC 15 ms
6,196 KB
testcase_24 AC 18 ms
6,280 KB
testcase_25 AC 14 ms
6,208 KB
testcase_26 AC 15 ms
6,208 KB
testcase_27 AC 15 ms
6,200 KB
testcase_28 AC 14 ms
6,204 KB
testcase_29 AC 1 ms
6,188 KB
testcase_30 AC 1 ms
6,192 KB
testcase_31 AC 2 ms
6,204 KB
testcase_32 AC 2 ms
6,184 KB
testcase_33 AC 1 ms
6,176 KB
testcase_34 AC 2 ms
6,196 KB
testcase_35 AC 1 ms
6,216 KB
testcase_36 AC 2 ms
6,188 KB
testcase_37 AC 2 ms
6,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;

void verify(const string &ans, int n, int b) {
  int m = (int) ans.size();

  long long v = 0;
  for (int i = 0; i < m; i++) {
    int d = ans[i] - '0';
    assert(d >= 0);
    assert(d < -b);
    v *= b;
    v += d;
  }
  assert(v == n);
  assert(n == 0 || ans[0] != '0');
}

string solve(int n, int b) {
  string ans;
  while (n) {
    int m = n % b;
    n /= b;
    if (m < 0) {
      m -= b;
      n++;
    }
    ans += '0' + m;
  }
  reverse(ans.begin(), ans.end());

  if (ans == "") ans += '0';
  return ans;
}

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

  int t, b, n;
  
  cin >> t >> b;

  assert(t >= 1);
  assert(t <= 100000);
  assert(b >= -10);
  assert(b <= -2);

  while (t--) {
    cin >> n;
    assert(n >= 0);
    assert(n <= 1000000000);
    string ans = solve(n, b);
    cout << ans << endl;
    verify(ans, n, b);
  }
  return 0;
}
0