Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define LL long long
  5.  
  6. LL countDer(int n)
  7. {
  8. // Create an array to store
  9. // counts for subproblems
  10. LL der[n + 1];
  11.  
  12. // Base cases
  13. der[0] = 1LL ;
  14. der[1] = 0LL ;
  15. der[2] = 1LL ;
  16.  
  17. // Fill der[0..n] in bottom up manner
  18. // using above recursive formula
  19.  
  20. for (int i = 3; i <= n; ++i)
  21. der[i] = (i - 1) * (der[i - 1] + der[i - 2]);
  22.  
  23.  
  24. // Return result for n
  25. return der[n];
  26. }
  27.  
  28. // Driver code
  29. int main()
  30. {
  31. int n = 100 ;
  32. cout << "Count of Derangements is "
  33. << countDer(n);
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement