-
Notifications
You must be signed in to change notification settings - Fork 0
/
walnut_3540.rs
41 lines (36 loc) · 962 Bytes
/
walnut_3540.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
use std::cmp::min;
pub fn main() {
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
let numbers: Vec<i32> = line.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();
let n = numbers[0];
let x = numbers[1];
let y = numbers[2];
let output = steps(n, x, y);
if output.0 >= 0 && output.1 >= 0 {
println!("{} {}", output.0, output.1)
} else {
println!("{}", -1)
}
}
fn steps(n: i32, x: i32, y: i32) -> (i32, i32) {
if n % x == 0 {
(n / x, 0)
} else if n % y == 0 {
(0, n / y)
} else {
let min = min(x, y);
if n - min > 0 {
let r = steps(n - min, x, y);
if r.0 < 0 || r.1 < 0 { (-1, -1) } else {
if min == x {
(1 + r.0, r.1)
} else {
(r.0, 1 + r.1)
}
}
} else { (-1, -1) }
}
}