Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. fn f(n: usize) -> u128 {
  2. static mut CACHE: Option<Vec<u128>> = None;
  3. unsafe {
  4. if CACHE.is_none() {
  5. let mut v = Vec::with_capacity(100000);
  6. v.push(1);
  7. v.push(1);
  8. v.push(2);
  9. CACHE = Some(v);
  10. }
  11.  
  12. let cache = CACHE.as_mut().unwrap();
  13. for x in cache.len()..=n {
  14. let mut ans = *cache.get_unchecked(x - 1);
  15. ans += cache.get_unchecked(x - 2);
  16. ans += cache.get_unchecked(x - 3);
  17. cache.push(ans)
  18. }
  19. cache[n]
  20. }
  21. }
  22.  
  23. fn main() {
  24. use std::time::Instant;
  25. let t = Instant::now();
  26. let ans = f(100);
  27. println!("f({}) = {}, time: {:?}", 100, ans, t.elapsed());
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement