Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <assert.h>
  4. using namespace std;
  5.  
  6. #define MOD (1000000007LL)
  7.  
  8. typedef long long int lint;
  9. lint N;
  10. typedef vector<vector<lint> > MATRIX;
  11.  
  12.  
  13. //bruteforce teszthez
  14. lint stupid(){
  15.     lint ret[5] = {0,1,2,3,5};
  16.     lint sum = 11;
  17.    
  18.     if(N==4)return sum;
  19.  
  20.     for(lint i =0;i<N-4;i++){
  21.         lint n = (((ret[4] + ret[3])%MOD) + ret[1])%MOD;
  22.  
  23.         ret[1] = ret[2];
  24.         ret[2] = ret[3];
  25.         ret[3] = ret[4];
  26.         ret[4] = n;
  27.        
  28.         sum = (sum+n);
  29.         if(sum > 1000000007){sum%=MOD;}
  30.     }
  31.  
  32.     return sum;
  33. }
  34.  
  35. MATRIX getMagicMatrix(){
  36.     MATRIX magic = {
  37.         {1,1,1,0,1,0}, // SUM
  38.         {0,1,1,0,1,0},
  39.         {0,1,0,0,0,0},
  40.         {0,0,1,0,0,0},
  41.         {0,0,0,1,0,0},
  42.         {0,0,0,0,1,0}
  43.     };
  44.     return magic;
  45. }
  46.  
  47. int getNOfMatrix(const MATRIX &m){
  48.     return m.size();
  49. }
  50.  
  51. int getMOfMatrix(const MATRIX &m){
  52.     return m[0].size();
  53. }
  54.  
  55. MATRIX getNMZeroMatrix(const int n,const int m){
  56.     MATRIX result(n);
  57.     for(int i=0;i<n;i++){
  58.         result[i].resize(m);
  59.     }
  60.     return result;
  61. }
  62.  
  63.  
  64. void printm(const MATRIX &m){
  65.     for(auto v : m){
  66.         for(auto elem: v){
  67.             printf("%lld\t",elem);
  68.         }
  69.         printf("\n");
  70.     }
  71. }
  72.  
  73.  
  74. MATRIX matrixmul(const MATRIX &a,const MATRIX &b){
  75.     assert(getMOfMatrix(a) == getNOfMatrix(b));
  76.  
  77.     int rowa = getNOfMatrix(a);
  78.     int cola = getMOfMatrix(a);
  79.  
  80.     MATRIX result = getNMZeroMatrix(rowa,getMOfMatrix(b));
  81.    
  82.     for(int resultcolindex = 0;resultcolindex<getMOfMatrix(b);resultcolindex++){
  83.         for(int rowina = 0;rowina<rowa;rowina++){
  84.             for(int c = 0;c<cola;c++){
  85.                 result[rowina][resultcolindex] = (result[rowina][resultcolindex] +  (( (a[rowina][c]%MOD) * (b[c][resultcolindex]) % MOD) % MOD) )% MOD;
  86.             }
  87.         }
  88.     }
  89.  
  90.     return result;
  91. }
  92.  
  93.  
  94.  
  95. //A^e
  96. MATRIX matrixpow(MATRIX matrix, lint e){
  97.     MATRIX pows[40];
  98.     lint e_ = e;  
  99.  
  100.     MATRIX current = matrix;
  101.    
  102.     bool notbegin = true;
  103.     MATRIX result;
  104.  
  105.     int pos = 0;
  106.  
  107.     while(e_>0){
  108.         if(e_&1){
  109.             if(notbegin){
  110.                 result = current;
  111.                 notbegin = false;
  112.             }else{
  113.                 result = matrixmul(result,current);
  114.             }
  115.         }
  116.        
  117.         e_>>=1;
  118.         current = matrixmul(current,current);
  119.         pos++;
  120.     }
  121.  
  122.     return result;
  123. }
  124.  
  125.  
  126. lint getsumn(const lint n){
  127.     if(n==0x4){return 11;}
  128.     if(n==0x5){return 20;}
  129.  
  130.     const MATRIX magic =matrixpow(getMagicMatrix(),n-4);
  131.  
  132.     const MATRIX b = {
  133.         {11}, // KEZDETI SUM
  134.         {5},
  135.         {3},
  136.         {2},
  137.         {1},
  138.         {0}
  139.     };
  140.  
  141.     const MATRIX &result = matrixmul(magic,b);
  142.     return result[0][0];
  143. }
  144.  
  145.  
  146. int main(){
  147.     int tcase;
  148.     scanf("%d",&tcase);
  149.  
  150.     while(tcase--){
  151.         scanf("%lld",&N);
  152.         printf("%lld\n",getsumn(N));    
  153.     }
  154.  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement