use std::iter::repeat;
fn main() {
let ans = people_aware_of_secret(4, 1, 3);
println!("ans = {}", ans);
}
fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
let mod0: i64 = 1000000007;
// dpKnow[i], 第i天知道秘密的人
let mut dp_know: Vec<i64> = repeat(0 as i64).take((n + 1) as usize).collect();
// dpForget[i], 第i天将要忘记秘密的人
let mut dp_forget: Vec<i64> = repeat(0 as i64).take((n + 1) as usize).collect();
// dpShare[i], 第i天可以分享秘密的人
let mut dp_share: Vec<i64> = repeat(0 as i64).take((n + 1) as usize).collect();
// 第1天的时候,知道秘密的人1个,A
// 第1天的时候,将要忘记秘密的人0个
// 第1天的时候,可以分享秘密的人0个
dp_know[1] = 1;
if 1 + forget <= n {
dp_forget[(1 + forget) as usize] = 1;
}
if 1 + delay <= n {
dp_share[(1 + delay) as usize] = 1;
}
// 从第2天开始!i
for i in 2..=n {
// 第i天
// dpKnow[i - 1] - dpForget[i] + dpShare[i]
dp_know[i as usize] = (mod0 + dp_know[(i - 1) as usize] - dp_forget[i as usize]
+ dp_share[i as usize])
% mod0;
if i + forget <= n {
// dpShare[i] 是第i天,刚知道秘密的人!
// 这批人,会在i + forget天,都忘了!
dp_forget[(i + forget) as usize] = dp_share[i as usize];
}
if i + delay <= n {
// dpShare[i + delay - 1] + dpShare[i] - dpForget[i + delay]
// i + delay 天 , 100天后,会分享秘密的人
// 第i天,有一些新人,i + delay天分享,一部分, dpShare[i]
// 第二部分呢?i + delay - 1天,知道秘密并且会散播的人,- dpForget[i + delay]
dp_share[(i + delay) as usize] = (mod0 + dp_share[(i + delay - 1) as usize]
- dp_forget[(i + delay) as usize]
+ dp_share[i as usize])
% mod0;
}
}
return dp_know[n as usize] as i32;
}
评论