Skip to content

Commit

Permalink
Add problem 2320: Count Number of Ways to Place Houses
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 14, 2024
1 parent 0d01016 commit e4e47d7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,7 @@ pub mod problem_2315_count_asterisks;
pub mod problem_2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph;
pub mod problem_2317_maximum_xor_after_operations;
pub mod problem_2319_check_if_matrix_is_x_matrix;
pub mod problem_2320_count_number_of_ways_to_place_houses;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

type Matrix = (u64, u64);

impl Solution {
const MODULUS: u64 = 1_000_000_007;

fn matrix_multiply(lhs: Matrix, rhs: Matrix) -> Matrix {
(
(lhs.0 * (rhs.0 + rhs.1) + lhs.1 * rhs.0) % Self::MODULUS,
(lhs.0 * rhs.0 + lhs.1 * rhs.1) % Self::MODULUS,
)
}

fn exp_mod(mut exponent: u32) -> Matrix {
let mut base = (1, 0);
let mut result = (0, 1);

loop {
if exponent & 1 != 0 {
result = Self::matrix_multiply(result, base);
}

exponent >>= 1;

if exponent == 0 {
break;
}

base = Self::matrix_multiply(base, base);
}

result
}

pub fn count_house_placements(n: i32) -> i32 {
let matrix = Self::exp_mod(n as _);

((matrix.0 * 2 + matrix.1).pow(2) % Self::MODULUS) as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn count_house_placements(n: i32) -> i32 {
Self::count_house_placements(n)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
18 changes: 18 additions & 0 deletions src/problem_2320_count_number_of_ways_to_place_houses/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod matrix_multiplication;

pub trait Solution {
fn count_house_placements(n: i32) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [(0, 1), (1, 4), (2, 9), (3, 25), (4, 64), (5, 169), (1000, 500_478_595)];

for (n, expected) in test_cases {
assert_eq!(S::count_house_placements(n), expected);
}
}
}

0 comments on commit e4e47d7

Please sign in to comment.