From d25c38f70cbafd86a945765c5ee3de54150a636b Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sun, 1 Oct 2023 00:37:37 +0800 Subject: [PATCH] Refactor --- src/problem_1622_fancy_sequence/mod.rs | 32 +++++++++++++++++++ .../mod_inverse.rs | 15 +++++---- .../mod_inverse_2.rs | 31 +++++++++--------- 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/problem_1622_fancy_sequence/mod.rs b/src/problem_1622_fancy_sequence/mod.rs index 7af1b237..8036add6 100644 --- a/src/problem_1622_fancy_sequence/mod.rs +++ b/src/problem_1622_fancy_sequence/mod.rs @@ -47,6 +47,38 @@ mod tests { Operation::AddAll(9), Operation::GetIndex(0, 20_000_002), ], + &[ + Operation::Append(3), + Operation::MultAll(10), + Operation::Append(3), + Operation::MultAll(2), + Operation::GetIndex(1, 6), + Operation::MultAll(8), + Operation::GetIndex(6, -1), + Operation::MultAll(7), + Operation::Append(3), + Operation::Append(6), + Operation::Append(7), + Operation::MultAll(4), + Operation::GetIndex(3, 24), + Operation::Append(3), + Operation::MultAll(7), + Operation::MultAll(3), + Operation::AddAll(6), + Operation::MultAll(10), + Operation::MultAll(8), + Operation::MultAll(8), + Operation::GetIndex(5, 44160), + Operation::Append(7), + Operation::Append(7), + Operation::AddAll(3), + Operation::GetIndex(4, 380_163), + Operation::GetIndex(0, 180_637_443), + Operation::MultAll(5), + Operation::GetIndex(0, 903_187_215), + Operation::GetIndex(4, 1_900_815), + Operation::GetIndex(7, 50), + ], ]; for operations in test_cases { diff --git a/src/problem_1622_fancy_sequence/mod_inverse.rs b/src/problem_1622_fancy_sequence/mod_inverse.rs index ff203af7..ec5dc05e 100644 --- a/src/problem_1622_fancy_sequence/mod_inverse.rs +++ b/src/problem_1622_fancy_sequence/mod_inverse.rs @@ -38,11 +38,10 @@ impl Fancy { } fn append(&mut self, val: i32) { - let val = val as u32; + let val = u64::from(val as u32); self.values.push( - (u64::from(val + (Self::MODULUS as u32 - self.inc)) * Self::mod_inv(u64::from(self.mul)) % Self::MODULUS) - as _, + ((val + (Self::MODULUS - u64::from(self.inc))) * Self::mod_inv(u64::from(self.mul)) % Self::MODULUS) as _, ); } @@ -58,14 +57,16 @@ impl Fancy { } fn mult_all(&mut self, m: i32) { - let m = m as u32; + let m = u64::from(m as u32); - self.mul = (u64::from(self.mul) * u64::from(m) % Self::MODULUS) as _; - self.inc = (u64::from(self.inc) * u64::from(m) % Self::MODULUS) as _; + self.mul = (u64::from(self.mul) * m % Self::MODULUS) as _; + self.inc = (u64::from(self.inc) * m % Self::MODULUS) as _; } fn get_index(&mut self, idx: i32) -> i32 { - self.values.get(idx as u32 as usize).map_or(-1, |&value| { + let idx = idx as u32 as usize; + + self.values.get(idx).map_or(-1, |&value| { ((u64::from(value) * u64::from(self.mul) + u64::from(self.inc)) % Self::MODULUS) as _ }) } diff --git a/src/problem_1622_fancy_sequence/mod_inverse_2.rs b/src/problem_1622_fancy_sequence/mod_inverse_2.rs index 9a445924..b2e3bc18 100644 --- a/src/problem_1622_fancy_sequence/mod_inverse_2.rs +++ b/src/problem_1622_fancy_sequence/mod_inverse_2.rs @@ -2,8 +2,8 @@ struct Fancy { values: Vec, - mul: u32, - inc: u32, + mul: u64, + inc: u64, } impl Fancy { @@ -40,35 +40,34 @@ impl Fancy { } fn append(&mut self, val: i32) { - let val = val as u32; + let val = u64::from(val as u32); - self.values.push( - (u64::from(val + (Self::MODULUS as u32 - self.inc)) * Self::mod_inv(u64::from(self.mul)) % Self::MODULUS) - as _, - ); + self.values + .push(((val + (Self::MODULUS - self.inc)) * Self::mod_inv(self.mul) % Self::MODULUS) as _); } fn add_all(&mut self, inc: i32) { - let modulus = Self::MODULUS as _; - let inc = inc as u32; + let inc = u64::from(inc as u32); self.inc += inc; - if self.inc >= modulus { - self.inc -= modulus; + if self.inc >= Self::MODULUS { + self.inc -= Self::MODULUS; } } fn mult_all(&mut self, m: i32) { - let m = m as u32; + let m = u64::from(m as u32); - self.mul = (u64::from(self.mul) * u64::from(m) % Self::MODULUS) as _; - self.inc = (u64::from(self.inc) * u64::from(m) % Self::MODULUS) as _; + self.mul = self.mul * m % Self::MODULUS; + self.inc = self.inc * m % Self::MODULUS; } fn get_index(&mut self, idx: i32) -> i32 { - self.values.get(idx as u32 as usize).map_or(-1, |&value| { - ((u64::from(value) * u64::from(self.mul) + u64::from(self.inc)) % Self::MODULUS) as _ + let idx = idx as u32 as usize; + + self.values.get(idx).map_or(-1, |&value| { + ((u64::from(value) * self.mul + self.inc) % Self::MODULUS) as _ }) } }