結果

問題 No.782 マイナス進数
ユーザー cympfhcympfh
提出日時 2019-01-15 17:16:11
言語 Rust
(1.77.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,818 bytes
コンパイル時間 1,340 ms
コンパイル使用メモリ 150,916 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 12:26:14
合計ジャッジ時間 3,277 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 20 ms
4,376 KB
testcase_03 AC 22 ms
4,380 KB
testcase_04 AC 23 ms
4,376 KB
testcase_05 AC 20 ms
4,380 KB
testcase_06 AC 19 ms
4,380 KB
testcase_07 AC 29 ms
4,380 KB
testcase_08 AC 21 ms
4,380 KB
testcase_09 AC 20 ms
4,376 KB
testcase_10 AC 20 ms
4,380 KB
testcase_11 AC 27 ms
4,380 KB
testcase_12 AC 26 ms
4,380 KB
testcase_13 AC 43 ms
4,376 KB
testcase_14 AC 26 ms
4,376 KB
testcase_15 AC 27 ms
4,380 KB
testcase_16 AC 26 ms
4,380 KB
testcase_17 AC 34 ms
4,380 KB
testcase_18 AC 25 ms
4,380 KB
testcase_19 AC 31 ms
4,380 KB
testcase_20 AC 26 ms
4,380 KB
testcase_21 AC 34 ms
4,380 KB
testcase_22 AC 27 ms
4,376 KB
testcase_23 AC 24 ms
4,380 KB
testcase_24 AC 43 ms
4,380 KB
testcase_25 AC 26 ms
4,380 KB
testcase_26 AC 29 ms
4,380 KB
testcase_27 AC 28 ms
4,376 KB
testcase_28 AC 25 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,384 KB
testcase_37 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(unused_macros)]
use std::cmp::{ min, max };

macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

fn solve(m: u64, b: u64) -> Vec<u64> {
    if m == 0 {
        return vec![0];
    }
    let mut n = m;
    let mut k = vec![];
    for i in 0.. {
        // trace!(i, n);
        if n == 0 { break }
        let d = n % b;
        if d == 0 {
            k.push(0);
            n /= b;
        } else if i % 2 == 0 {
            k.push(d);
            n = (n - d) / b;
        } else {
            k.push(b - d);
            n = (n + b - d) / b;
        }
    }
    k
}

fn main() {
    let mut sc = Scanner::new();
    let n: u64 = sc.cin();
    let b: u64 = (sc.cin::<i64>() * -1) as u64;
    for _ in 0..n {
        let m: u64 = sc.cin();
        for &c in solve(m, b).iter().rev() { print!("{}", c); }
        put!("");
    }
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}
0