use crate::Solution;
problem!(Problem0078, 78, "Coin Partitions");
impl Solution for Problem0078 {
fn solve(&self) -> String {
const DIVISOR: u32 = 1_000_000;
let mut cache = vec![1, 1];
while cache[cache.len() - 1] != 0 {
let n = cache.len();
let mut next_val = 0;
for k in 1..(n + 1) {
let left_value = match n.checked_sub((k * (3 * k - 1)) >> 1) {
Some(ind) => cache[ind],
None => break, };
let right_value = match n.checked_sub((k * (3 * k + 1)) >> 1) {
Some(ind) => cache[ind],
None => 0,
};
let value = (left_value + right_value) % DIVISOR;
if k % 2 == 0 {
next_val = (next_val + (DIVISOR - value)) % DIVISOR; } else {
next_val = (next_val + value) % DIVISOR;
}
}
cache.push(next_val);
}
(cache.len() - 1).to_string()
}
}