SHARE
TWEET

Untitled

a guest Oct 9th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top