Skip to content

Commit a95199e

Browse files
committed
Add problem 3179: Find the N-th Value After K Seconds
1 parent 65fcc64 commit a95199e

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,7 @@ pub mod problem_3170_lexicographically_minimum_string_after_removing_stars;
21232123
pub mod problem_3174_clear_digits;
21242124
pub mod problem_3175_find_the_first_player_to_win_k_games_in_a_row;
21252125
pub mod problem_3178_find_the_child_who_has_the_ball_after_k_seconds;
2126+
pub mod problem_3179_find_the_n_th_value_after_k_seconds;
21262127
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21272128

21282129
#[cfg(test)]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
const MODULUS: u64 = 1_000_000_007;
7+
8+
fn exp_mod(mut base: u64, mut exponent: u64) -> u64 {
9+
let mut result = 1;
10+
11+
loop {
12+
if exponent & 1 != 0 {
13+
result = (result * base) % Self::MODULUS;
14+
15+
if exponent == 1 {
16+
break;
17+
}
18+
}
19+
20+
exponent >>= 1;
21+
base = (base * base) % Self::MODULUS;
22+
}
23+
24+
result
25+
}
26+
27+
// See <https://en.wikipedia.org/wiki/Modular_multiplicative_inverse#Using_Euler's_theorem>.
28+
fn mod_inv(x: u64) -> u64 {
29+
Self::exp_mod(x, Self::MODULUS - 2)
30+
}
31+
32+
fn binomial(n: u64, k: u64) -> u64 {
33+
let n_minus_k = n - k;
34+
let (k, n_minus_k) = if n_minus_k < k { (n_minus_k, k) } else { (k, n_minus_k) };
35+
let mut factorial = 1;
36+
37+
for i in 2..=k {
38+
factorial = (factorial * i) % Self::MODULUS;
39+
}
40+
41+
let factorial_k = factorial;
42+
43+
for i in k + 1..=n_minus_k {
44+
factorial = (factorial * i) % Self::MODULUS;
45+
}
46+
47+
let factorial_n_minus_k = factorial;
48+
49+
for i in n_minus_k + 1..=n {
50+
factorial = (factorial * i) % Self::MODULUS;
51+
}
52+
53+
let factorial_n = factorial;
54+
55+
(factorial_n * Self::mod_inv((factorial_k * factorial_n_minus_k) % Self::MODULUS)) % Self::MODULUS
56+
}
57+
58+
pub fn value_after_k_seconds(n: i32, k: i32) -> i32 {
59+
let n = u64::from(n.cast_unsigned());
60+
let k = u64::from(k.cast_unsigned());
61+
62+
Self::binomial(n + k - 1, n - 1) as _
63+
}
64+
}
65+
66+
// ------------------------------------------------------ snip ------------------------------------------------------ //
67+
68+
impl super::Solution for Solution {
69+
fn value_after_k_seconds(n: i32, k: i32) -> i32 {
70+
Self::value_after_k_seconds(n, k)
71+
}
72+
}
73+
74+
#[cfg(test)]
75+
mod tests {
76+
#[test]
77+
fn test_solution() {
78+
super::super::tests::run::<super::Solution>();
79+
}
80+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod mathematical;
2+
3+
pub trait Solution {
4+
fn value_after_k_seconds(n: i32, k: i32) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [((4, 5), 56), ((5, 3), 35)];
13+
14+
for ((n, k), expected) in test_cases {
15+
assert_eq!(S::value_after_k_seconds(n, k), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)