Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const ll Mod = 1e9 + 7;
  6. const ll Nax = 110;
  7. const ll Inf = 4e18;
  8. ll N , Start , End;
  9.  
  10. ///For Easy Code
  11. #define rep(i,v) for(ll i=0;i<v.size();++i)
  12. typedef vector < ll > Row;
  13. typedef vector < Row > Matrix;
  14.  
  15. ///Zero Matrix
  16. Matrix Zero(ll n,ll m)
  17. {
  18. return Matrix( n , Row(m,0) );
  19. }
  20.  
  21. ///Identity Matrix
  22. Matrix Identity(ll n)
  23. {
  24. Matrix Res = Zero(n,n);
  25. rep(i,Res) Res[i][i] = 1;
  26. return Res;
  27. }
  28.  
  29. ///Matrix Multiplication
  30. Matrix Muliply(const Matrix &a,const Matrix &b)
  31. {
  32. Matrix Res=Zero( a.size() , b[0].size() );
  33.  
  34. rep(i,a) rep(k,a[0]) if(a[i][k])
  35. rep(j,b[0])
  36. Res[i][j] = ( Res[i][j] + a[i][k] * b[k][j] )%Mod;
  37.  
  38. return Res;
  39. }
  40.  
  41. ///Matrix Power
  42. Matrix Power(const Matrix &a,ll k)
  43. {
  44. if(k==0) return Identity( a.size() );
  45. if(k&1) return Muliply( a , Power(a,k-1) );
  46. return Power( Muliply(a,a) , k>>1LL );
  47. }
  48.  
  49. int main()
  50. {
  51. Matrix Adj = Zero(9, 9);
  52.  
  53. ll n; cin >> n;
  54.  
  55. Adj[0][0] = Adj[0][1] = Adj[0][2] = 1;
  56. Adj[1][3] = Adj[1][4] = Adj[1][5] = 1;
  57. Adj[2][6] = Adj[2][7] = Adj[2][8] = 1;
  58. Adj[3][0] = 1;
  59. Adj[4][3] = Adj[4][4] = 1;
  60. Adj[5][6] = Adj[5][7] = Adj[5][8] = 1;
  61. Adj[6][0] = 1;
  62. Adj[7][3] = 1;
  63. Adj[8][6] = Adj[8][7] = Adj[8][8] = 1;
  64.  
  65. Adj = Power(Adj, n - 2);
  66.  
  67. ll Ans = 0;
  68. for (int i = 0; i < 9; i++)
  69. for (int j = 0; j < 9; j++)
  70. Ans = (Ans%Mod + Adj[i][j]%Mod)%Mod;
  71.  
  72. cout << Ans << '\n';
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement