Advertisement
Guest User

Fancy stuff

a guest
May 14th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.27 KB | None | 0 0
  1. #[macro_use] extern crate scan_rules;
  2.  
  3. fn main() {
  4.     loop {
  5.         let_readln!(let num: i64);
  6.         if num == 0 { break; }
  7.  
  8.         let_readln!(let c1: i64, let a: i64);
  9.         let_readln!(let c2: i64, let b: i64);
  10.  
  11.         let (a, b, reverse) = if a < b {(a, b, false)} else {(b, a, true)};
  12.         let ratio_a = a as f64 / c1 as f64;
  13.         let ratio_b = b as f64 / c2 as f64;
  14.  
  15.         if let Some((x, y, gcd)) = eu(a, b, num) {
  16.             let (a, b, x, y, reverse) = {
  17.                 if ratio_a > ratio_b {
  18.                     (a/gcd, b/gcd, x, y, reverse)
  19.                 } else {
  20.                     (b/gcd, a/gcd, y, x, !reverse)
  21.                 }
  22.             };
  23.             let steps = -y/a + if y < 0 {1} else {0}; // y -> 0
  24.             let (x, y) = (gcd*(x - steps * b), gcd*(y + steps * a));
  25.             let (x, y) = if !reverse {(x, y)} else {(y,x)};
  26.  
  27.             println!("{} {}", x, y);
  28.         } else {
  29.             println!("failure");
  30.         }
  31.     }
  32. }
  33.  
  34. fn eu(a: i64, b: i64, c: i64) -> Option<(i64, i64, i64)> {
  35.     if b % a == 0 {
  36.         if c % a == 0 {
  37.             Some(((c % b) / a, c / b, a))
  38.         } else { None }
  39.     } else {
  40.         let (r1, r2, gcd) = eu(b % a, a, c)?;
  41.         Some((r2 - (b / a)*r1, r1, gcd))
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement