-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2320: Count Number of Ways to Place Houses
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/problem_2320_count_number_of_ways_to_place_houses/matrix_multiplication.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/problem_2320_count_number_of_ways_to_place_houses/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |