Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 30, 2023
1 parent 9f3a131 commit d25c38f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
32 changes: 32 additions & 0 deletions src/problem_1622_fancy_sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions src/problem_1622_fancy_sequence/mod_inverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _,
);
}

Expand All @@ -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 _
})
}
Expand Down
31 changes: 15 additions & 16 deletions src/problem_1622_fancy_sequence/mod_inverse_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

struct Fancy {
values: Vec<u32>,
mul: u32,
inc: u32,
mul: u64,
inc: u64,
}

impl Fancy {
Expand Down Expand Up @@ -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 _
})
}
}
Expand Down

0 comments on commit d25c38f

Please sign in to comment.