Pagini recente » Cod sursa (job #60444) | Cod sursa (job #3252760) | Cod sursa (job #2087431) | Cod sursa (job #301197) | Cod sursa (job #3187664)
use std::fs;
fn gcd(a: i64, b: i64) -> (i64, i64) {
if b == 0 {
return (1, 1);
}
let (c, d) = gcd(b, a % b);
// c * b + d * (a - b * (a / b)) = x
// a * d + b * (c - d * (a / b)) = x
// c' = d; d' = c - d * (a / b)
(d, c - d * (a / b))
}
fn solution(tup: (i64, i64, i64)) -> (i64, i64) {
let (c, d) = gcd(tup.0, tup.1);
let x = tup.0 * c + tup.1 * d;
if (tup.2 % x) != 0 { return (0, 0); }
let m = tup.2 / x;
(c * m, d * m)
}
fn main() {
fn read() -> Vec<(i64, i64, i64)> {
let text = fs::read_to_string("euclid3.in").unwrap();
let mut crt_line = text.lines().skip(1);
let mut tups: Vec<(i64, i64, i64)> = Vec::new();
while let Some(line) = crt_line.next() {
let nums: Vec<i64> = line
.split_whitespace()
.map(|x| x.parse::<i64>().unwrap())
.collect();
tups.push((nums[0], nums[1], nums[2]));
}
tups
}
let mut r = String::from("");
let tups = read();
for tup in tups {
let (a, b) = solution(tup);
r.push_str(&(a.to_string() + " " + &b.to_string() + "\n"));
}
fs::write("euclid3.out", r).unwrap();
}