Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long ll;
  5. const int maxn=(1<<8)+1;
  6. const ll MOD=1000000000;
  7. struct parameter{int c, r;};
  8. struct Matrix{
  9. ll matrix[maxn][maxn];
  10. parameter DIM;
  11. Matrix(){}
  12. Matrix(int c, int r){
  13. DIM={c, r};
  14. memset(matrix, 0, sizeof(matrix));
  15. }
  16. Matrix operator * (Matrix& A){
  17. Matrix C(DIM.c, A.DIM.r);
  18. memset(C.matrix, 0, sizeof(C.matrix));
  19. for(int i=0; i<DIM.c; i++)
  20. for(int j=0; j<A.DIM.r; j++)
  21. for(int k=0; k<DIM.r; k++)
  22. C.matrix[i][j]=(C.matrix[i][j]+A.matrix[i][k]*matrix[k][j])%MOD;
  23. return C;
  24. }
  25. void print(){
  26. for(int i=0; i<DIM.c; i++){
  27. for(int j=0; j<DIM.r; j++)
  28. cout<<matrix[i][j]<<'\t';
  29. cout<<endl;
  30. }
  31. }
  32. };
  33. Matrix BigMatrixExpo(Matrix& A, ll n){
  34. //cout<<n<<endl;
  35. Matrix B=A;
  36. Matrix C(A.DIM.c, A.DIM.r);
  37. for(int i=0; i<C.DIM.c; i++)
  38. for(int j=0; j<C.DIM.r; j++)
  39. C.matrix[i][j]=(i==j);
  40. C.print();
  41. while(n){
  42. cout<<n<<endl;
  43. if(n&1){
  44. C=C*B;
  45. C.print();
  46. }
  47. B=B*B;
  48. B.print();
  49. n>>=1;
  50. }
  51. cout<<"------------------------"<<endl;
  52. C.print();
  53. return C;
  54. }
  55. ll N, M, ts;
  56. int main(){
  57. cin>>N>>M;
  58. ts=1<<N;
  59. Matrix dp(ts, ts);
  60. for(int i=0; i<ts; i++){
  61. for(int j=0; j<ts; j++){
  62. if(!j){
  63. dp.matrix[i][j]=1;
  64. continue;
  65. }
  66. if(!(j&1)){
  67. dp.matrix[i][j]=dp.matrix[i>>1][j>>1];
  68. continue;
  69. }
  70. if((j&3)==1){
  71. if(i&1) dp.matrix[i][j]=0;
  72. else dp.matrix[i][j]=dp.matrix[i>>2][j>>2];
  73. continue;
  74. }
  75. if(i&1) dp.matrix[i][j]=dp.matrix[i>>2][j>>2];
  76. else dp.matrix[i][j]=dp.matrix[i>>2][j>>2]+dp.matrix[i>>1][j>>1];
  77. }
  78. }
  79. dp.print();
  80. cout<<"----------------------"<<endl;
  81. dp=BigMatrixExpo(dp, M);
  82.  
  83. cout<<dp.matrix[0][0]<<endl;
  84. return 0;
  85. }
  86. /*
  87. Last login: Mon Dec 11 15:12:21 on ttys000
  88. Chens-MacBook-Pro:~ CCS$ /Users/ccs/Desktop/C ; exit;
  89. 2
  90. 2
  91. 1 1 1 2
  92. 1 0 1 1
  93. 1 1 0 1
  94. 1 0 0 1
  95. ----------------------
  96. 1 0 0 0
  97. 0 1 0 0
  98. 0 0 1 0
  99. 0 0 0 1
  100. 2
  101. 5 2 2 6
  102. 3 2 1 4
  103. 3 1 2 4
  104. 2 1 1 3
  105. 1
  106. 49 22 22 64
  107. 32 15 14 42
  108. 32 14 15 42
  109. 22 10 10 29
  110. 49 22 22 64
  111. 32 15 14 42
  112. 32 14 15 42
  113. 22 10 10 29
  114. ------------------------
  115. 49 22 22 64
  116. 32 15 14 42
  117. 32 14 15 42
  118. 22 10 10 29
  119. 49
  120. logout
  121. Saving session...
  122. ...copying shared history...
  123. ...saving history...truncating history files...
  124. ...completed.
  125.  
  126. [Process completed]
  127.  
  128.  
  129. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement