Guest User

Untitled

a guest
Jul 15th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. #define forn(i, n) for (int i = 0; i < n; ++i)
  7.  
  8. typedef long long LL;
  9.  
  10. const LL p = 1e+9 + 7;
  11.  
  12. // a *= b
  13. void mxmul(LL **a, LL **b)
  14. {
  15.     LL t[4][4];
  16.  
  17.     forn(i, 4)
  18.         forn(j, 4)
  19.         {
  20.             t[i][j] = 0;
  21.             forn(k, 4)
  22.                 t[i][j] += ( (a[i][k] % p) * (b[k][j] % p) ) % p;
  23.         }
  24.  
  25.     memcpy(a, t, sizeof t);          
  26. }
  27.  
  28. // r = a ^ n
  29. void mxbinpow(LL **r, LL **a, LL n)
  30. {
  31.     forn(i, 4)
  32.         forn(j, 4)
  33.             r[i][j] = i - j ? 0 : 1;
  34.  
  35.     while (n)
  36.     {
  37.         if (n & 1)
  38.             mxmul(r, a);
  39.         mxmul(a, a);
  40.         n >>= 1;
  41.     }
  42. }
  43.  
  44. int main()
  45. {
  46.     LL n; cin >> n;
  47.  
  48.     LL r[4][4], a[4][4];
  49.  
  50.     forn(i, 4)
  51.         forn(j, 4)
  52.             a[i][j] = i - j ? 1 : 0;
  53.  
  54.     mxbinpow(r, a, n);
  55.  
  56.     cout << r[3][3] << endl;
  57.    
  58.        
  59.     return 0;
  60. }
Add Comment
Please, Sign In to add comment