Iamtui1010

fuho.cpp

Feb 4th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<vector>
  4.  
  5. #define long long long
  6. #define nln '\n'
  7. #define matrix vector<vector<long>>
  8.  
  9. using namespace std;
  10.  
  11. const long MOD = 1e9+7;
  12.  
  13. matrix operator*(const matrix &a, const matrix &b)
  14. {
  15.     matrix c(a.size());
  16.     for (long i = 0; i < (long)a.size(); ++i){
  17.         c[i].resize(b[0].size(), 0);
  18.         for (long j = 0; j < (long)b[0].size(); ++j)
  19.             for (long k = 0; k < (long)a[0].size(); ++k)       
  20.                 c[i][j] += a[i][k]*b[k][j], c[i][j] %= MOD;
  21.     }
  22.     return c;
  23. }
  24.  
  25. matrix binpow(const matrix &a, long n)
  26. {
  27.     if (n == 0)
  28.         return (matrix){{1, 0}, {0, 1}};
  29.     if (n == 1)
  30.         return a;
  31.     matrix tem = binpow(a, n/2);
  32.     if (n % 2 == 1)
  33.         return tem*tem*a;
  34.     return tem*tem;
  35. }
  36.  
  37. int main()
  38. {
  39.     cin.tie(0)->sync_with_stdio(0);
  40.     cout.tie(0)->sync_with_stdio(0);
  41.     freopen("fuho.inp", "r", stdin);
  42.     freopen("fuho.out", "w", stdout);
  43.     matrix a{{1, 1}, {2, 0}};
  44.     matrix b{{3}, {2}};
  45.     long n;
  46.     cin >> n;
  47.     while (n--){
  48.         long i;
  49.         cin >> i;
  50.         if (i > 1){
  51.             cout << (binpow(a, i-2)*b)[0][0] << nln;
  52.             continue;
  53.         }
  54.         if (i == 1){
  55.             cout << 1 << nln;
  56.             continue;
  57.         }
  58.         if (i == 0){
  59.             cout << 0 << nln;
  60.             continue;
  61.         }
  62.     }
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment