結果

問題 No.782 マイナス進数
ユーザー しらっ亭しらっ亭
提出日時 2017-05-13 00:29:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 977 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 62,568 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-13 14:02:34
合計ジャッジ時間 3,443 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 16 ms
4,348 KB
testcase_03 AC 16 ms
4,356 KB
testcase_04 AC 17 ms
4,348 KB
testcase_05 AC 16 ms
4,348 KB
testcase_06 AC 16 ms
4,352 KB
testcase_07 AC 18 ms
4,348 KB
testcase_08 AC 16 ms
4,348 KB
testcase_09 AC 15 ms
4,348 KB
testcase_10 AC 16 ms
4,348 KB
testcase_11 AC 17 ms
4,348 KB
testcase_12 AC 17 ms
4,348 KB
testcase_13 AC 21 ms
4,352 KB
testcase_14 AC 16 ms
4,348 KB
testcase_15 AC 17 ms
4,348 KB
testcase_16 AC 16 ms
4,348 KB
testcase_17 AC 19 ms
4,352 KB
testcase_18 AC 17 ms
4,348 KB
testcase_19 AC 18 ms
4,348 KB
testcase_20 AC 17 ms
4,348 KB
testcase_21 AC 19 ms
4,352 KB
testcase_22 AC 17 ms
4,352 KB
testcase_23 AC 16 ms
4,348 KB
testcase_24 AC 21 ms
4,352 KB
testcase_25 AC 16 ms
4,352 KB
testcase_26 AC 18 ms
4,352 KB
testcase_27 AC 17 ms
4,348 KB
testcase_28 AC 16 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 2 ms
4,348 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