Advertisement
FreakSkipper

Desarranjos

Dec 11th, 2020
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define vi vector<int>
  6. #define vll vector<ll>
  7. #define ll long long
  8. #define pb push_back
  9. #define eb emplace_back
  10. #define mp make_pair
  11. #define ii pair<int,int>
  12. #define ff first
  13. #define ss second
  14.  
  15. #define PRIMO 1000000007
  16.  
  17. int n;
  18. int tb[1000000];
  19.  
  20. int mymod(int a, int b) {
  21.     if (a == 0 || b == 0) {
  22.         return 0;
  23.     }
  24.     if (a == 1) {
  25.         return b;
  26.     }
  27.     if (b == 1) {
  28.         return a;
  29.     }
  30.  
  31.     int aux = mymod(a, b/2);
  32.    
  33.     if ((b & 1) == 0) {
  34.         return (aux+aux) % PRIMO;
  35.     } else {
  36.         return (aux + (aux+aux) % PRIMO) % PRIMO;
  37.     }
  38. }
  39.  
  40. int dp(int n) {
  41.     if (n == 0) return 1;
  42.     if (n == 1) return 0;
  43.     if (tb[n] != -1) return tb[n];
  44.  
  45.     return tb[n] = mymod( (dp(n-1) + dp(n-2)) % PRIMO, n-1);
  46. }
  47.  
  48. int main() {
  49.     ios_base::sync_with_stdio(false);
  50.     cin.tie(NULL);
  51.     memset(tb, -1, sizeof(tb));
  52.  
  53.     cin >> n;
  54.  
  55.     int ans = dp(n);
  56.  
  57.     cout << ans << endl;
  58.    
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement