#![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 { 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::() * -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, } impl Scanner { fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } } fn cin(&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::().ok().unwrap() } }