結果

問題 No.782 マイナス進数
ユーザー iicafiaxusiicafiaxus
提出日時 2019-01-11 22:54:43
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,198 bytes
コンパイル時間 714 ms
コンパイル使用メモリ 116,472 KB
実行使用メモリ 5,000 KB
最終ジャッジ日時 2023-09-03 22:01:16
合計ジャッジ時間 3,859 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 23 ms
4,376 KB
testcase_03 AC 27 ms
4,380 KB
testcase_04 AC 32 ms
4,380 KB
testcase_05 AC 25 ms
4,380 KB
testcase_06 AC 23 ms
4,380 KB
testcase_07 AC 45 ms
4,380 KB
testcase_08 AC 27 ms
4,376 KB
testcase_09 AC 23 ms
4,376 KB
testcase_10 AC 24 ms
4,380 KB
testcase_11 AC 44 ms
4,380 KB
testcase_12 AC 38 ms
4,380 KB
testcase_13 AC 87 ms
4,380 KB
testcase_14 AC 38 ms
4,384 KB
testcase_15 AC 44 ms
4,380 KB
testcase_16 AC 38 ms
4,376 KB
testcase_17 AC 63 ms
4,380 KB
testcase_18 AC 38 ms
4,376 KB
testcase_19 AC 55 ms
4,380 KB
testcase_20 AC 38 ms
5,000 KB
testcase_21 AC 60 ms
4,376 KB
testcase_22 AC 42 ms
4,384 KB
testcase_23 AC 34 ms
4,380 KB
testcase_24 AC 87 ms
4,380 KB
testcase_25 AC 38 ms
4,504 KB
testcase_26 AC 50 ms
4,380 KB
testcase_27 AC 44 ms
4,380 KB
testcase_28 AC 37 ms
4,384 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,384 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

void main(){
	int t = read.to!int;
	int b = read.to!int;
	
	long[] ns;
	foreach(i; 0 .. t) ns ~= read.to!long;
	
	foreach(n; ns){
		int[] xs = f(n, -b);
		for(int i = 0; i < xs.length; i ++){
			debug writeln("xs:", xs);
			if(i % 2 == 0) continue;
			else{
				// [..., x, ...] を [..., (-b - x), 1, ...] に変換
				int x = xs[i];
				if(x == 0) continue;
				xs[i] = -b - x;
				for(int j = i + 1; j <= n; j ++){
					if(j == xs.length){
						xs ~= 1;
						break;
					}
					else if(xs[j] < -b - 1){
						xs[j] += 1;
						break;
					}
					else{
						xs[j] = 0;
						continue;
					}
				}
			}
		}
		string ans = "";
		foreach_reverse(x; xs) ans ~= x.to!string;
		
		if(ans == "") ans = "0";
		
		ans.writeln;
	}
}

// nを通常のb進で表したもの(1のけたから順に)
int[] f(long n, int b){
	int[] res;
	long x = n;
	while(x > 0){
		res ~= x % b;
		x /= b;
	}
	return res;
}
0