-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_1.rs
150 lines (141 loc) · 2.53 KB
/
task_1.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::utils::{
gcd::ExtendedGcd,
input::{get_numeric_input, invalid_input, no_solution},
Error,
};
const MIN: i32 = 1;
const MAX: i32 = 100;
#[cfg_attr(doc, katexit::katexit)]
/// # HOMEWORK 3 | TASK 1
///
/// ## Input
///
/// 1. Three numbers (`a`, `b` and `c`).
///
/// ### Constaints
///
/// 2. The input should be in the inclusive range of: `[1;100]`.
///
/// ### Output
///
/// The values of `x` and `y` separated by new lines based on the Bezout's identity formula[1]:
///
/// $$
/// (a \times{x} + b \times{y}) \bmod{c} = 0
/// $$
///
///
/// [1]: https://en.wikipedia.org/wiki/B%C3%A9zout's_identity#Structure_of_solutions
///
/// ### Samples
///
/// #### Input Format
///
/// ```txt
/// 30
/// 18
/// 6
/// ```
///
/// #### Output Format
///
/// ```txt
/// -1
/// 2
/// ```
///
/// #### Input Format
///
/// ```txt
/// 30
/// 18
/// 4
/// ```
///
/// #### Output Format
///
/// ```txt
/// No solution!
/// ```
///
/// #### Explanation
///
/// (30, 18) = 6 => 6 % 4 != 0
///
/// #### Input Format
///
/// ```txt
/// 101
/// 25
/// 9
/// ```
///
/// #### Output Format
///
/// ```txt
/// Invalid input data!
/// ```
///
/// ### Error
///
/// If the input is invalid, return:
///
/// ```rust
/// String::from("Invalid input data!");
/// ```
///
/// If there's no solution, return:
///
/// ```rust
/// String::from("No solution!");
/// ```
///
/// ## Test cases
///
/// ```rust
/// use hackerrank::hw3::task_1::Solution;
///
/// assert_eq!(
/// Solution::main(30, 18, 6),
/// String::from("-1\n2")
/// );
/// assert_eq!(
/// Solution::main(30, 18, 4),
/// String::from("No solution!")
/// );
/// assert_eq!(
/// Solution::main(101, 25, 9),
/// String::from("Invalid input data!")
/// );
/// assert_eq!(
/// Solution::main(100, 50, 100),
/// String::from("0\n2")
/// );
/// assert_eq!(
/// Solution::main(99, 33, 66),
/// String::from("0\n2")
/// );
/// ```
pub struct Solution;
impl Solution {
pub fn main(a: i32, b: i32, c: i32) -> String {
if [a, b, c].iter().any(|item| !(MIN..=MAX).contains(item)) {
return invalid_input();
}
let ((mut x, mut y), d) = a.extended_gcd(b);
if c % d != 0 {
return no_solution();
}
let scale_to_c = c / d;
x *= scale_to_c;
y *= scale_to_c;
format!("{}\n{}", x, y)
}
}
pub fn main() -> Result<(), Error> {
let a = get_numeric_input::<i32>()?;
let b = get_numeric_input::<i32>()?;
let c = get_numeric_input::<i32>()?;
println!("{}", Solution::main(a, b, c));
Ok(())
}