#![allow(dead_code, unused_imports, unused_macros, non_snake_case)] fn main() { let max = 81181819; let mut yiwiy9_numbers = vec![]; for i in 1 .. { let mut x = i; let mut digits = vec![]; while x > 0 { digits.push([0, 1, 8][x % 3]); x /= 3; } let mut y = 0; for &d in digits.iter().rev() { y = y * 10 + d; } if y > max { break; } yiwiy9_numbers.push(y); } input! { T: usize, } for _ in 0 .. T { input! { N: usize, } // let mut p = vec![]; // { // let mut n = max - N; // if n == 0 { // p.push(0); // } // // greedy // while n > 0 { // let y = yiwiy9_numbers[yiwiy9_numbers.partition_point(|&y| y <= n ) - 1]; // p.push(y); // n -= y; // } // } // dp let mut dp = HashMap::new(); dp.insert(0, (0, 0)); for &y in &yiwiy9_numbers { dp.insert(y, (1, y)); } fn rec(yiwiy9_numbers: &[usize], dp: &mut HashMap, n: usize) -> usize { if let Some(&ans) = dp.get(&n) { return ans.0; } let mut ans = (INF as usize, 0); for &y in yiwiy9_numbers { if y > n { break; } ans.chmin((rec(yiwiy9_numbers, dp, n - y) + 1, y)); if ans.0 <= 2 { break; } } dp.insert(n, ans); ans.0 } rec(&yiwiy9_numbers, &mut dp, max - N); let mut p = vec![]; let mut n = max - N; if n == 0 { p.push(0); } while n > 0 { let (_, y) = dp[&n]; p.push(y); n -= y; } say(p.len()); say(p.iter().implode("\n")); } } type Int = i64; const MOD: Int = 998244353; // const MOD: Int = 1_000_000_007; const INF: Int = 1_000_000_000_000_000_000; const YESNO: [&'static str; 2] = ["Yes", "No"]; use proconio::{input, input_interactive, marker::{Chars, Bytes, Usize1}}; use std::*; use std::ops::*; use collections::*; // (BTree|Hash)(Set|Map), BinaryHeap, VecDeque, LinkedList use cmp::{self, Reverse}; // cmp::{min, max} fn yes() { println!("{}", YESNO[0]); } fn no() { println!("{}", YESNO[1]); } fn yesno(c: bool) { println!("{}", if c { YESNO[0] } else { YESNO[1] }); } fn say(x: T) -> T { println!("{}", x); x } fn neighbor4(i: usize, j: usize, h: usize, w: usize, mut f: F) { if i > 0 { (f)(i - 1, j); } if i < h - 1 { (f)(i + 1, j); } if j > 0 { (f)(i, j - 1); } if j < w - 1 { (f)(i, j + 1); } } trait MyItertools : Iterator + Sized { fn to_vec(self) -> Vec { self.collect::>() } fn to_vec_rev(self) -> Vec { let mut v = self.collect::>(); v.reverse(); v } fn tally(self) -> HashMap where Self::Item: Copy + Eq + hash::Hash { let mut counts = HashMap::new(); self.for_each(|item| *counts.entry(item).or_default() += 1 ); counts } fn count_if bool>(self, predicate: P) -> usize { self.map(predicate).filter(|&x| x ).count() } fn implode(self, sep: &str) -> String where Self::Item: std::string::ToString { self.map(|x| x.to_string()).to_vec().join(sep) } fn mex(self, gen: impl IntoIterator) -> Self::Item where Self::Item: Ord { let mut v = self.collect::>(); v.sort(); v.dedup(); let mut it = v.into_iter(); gen.into_iter().find(|a| if let Some(x) = it.next() { a != &x } else { true }).unwrap() } } impl MyItertools for T where T: Iterator + Sized {} trait MyOrd : PartialOrd + Sized { fn chmax(&mut self, mut rhs: Self) -> bool { if self < &mut rhs { *self = rhs; true } else { false } } fn chmin(&mut self, mut rhs: Self) -> bool { if self > &mut rhs { *self = rhs; true } else { false } } } impl MyOrd for T {}