-
Notifications
You must be signed in to change notification settings - Fork 2
/
given_length_and_sum_of_digits_489C.rs
83 lines (67 loc) · 1.62 KB
/
given_length_and_sum_of_digits_489C.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// https://codeforces.com/problemset/problem/489/C
// greedy, implementation
use std::io;
use std::char;
fn reverse(cadena: &String) -> String {
let mut ans = String::from("");
for ch in cadena.chars().rev() {
ans.push(ch);
}
ans
}
fn main() {
let mut line = String::new();
io::stdin()
.read_line(&mut line)
.unwrap();
let words: Vec<i64> =
line
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
let m = words[0] as usize;
let mut s = words[1];
let mut _t = words[1];
if s == 0 && m != 1 {
println!("-1 -1");
return;
}
if s == 0 && m == 1 {
println!("0 0");
return;
}
// lets construct maxima
let mut maxima = String::from("");
while s > 0 {
if s >= 9 {
maxima = format!("9{}", maxima);
s -= 9;
} else {
maxima = format!("{}{}", maxima, s);
s -= s;
}
}
if maxima.len() > m {
println!("-1 -1");
return;
}
while maxima.len() < m {
maxima = format!("{}0", maxima);
}
// lets construct minima
let mut chars: Vec<char> = reverse(&maxima).chars().collect();
let mut minima = String::from("");
if chars[0] == '0' {
chars[0] = '1';
for i in 1..chars.len() {
if chars[i] != '0' {
chars[i] = char::from_digit(chars[i].to_digit(10).unwrap()-1,10).unwrap();
break;
}
}
}
for ch in chars {
minima.push(ch);
}
println!("{} {}", minima, maxima);
}