Dang_Quan_10_Tin

TILE

Aug 2nd, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #define task "TILE"
  5. using namespace std;
  6. using ll = long long;
  7. using ld = long double;
  8.  
  9. const int N = 2 + 2;
  10. const ll mod = 1e9 + 7;
  11. struct Matrix{
  12.     ll a[N][N];
  13.     Matrix() {memset(a, 0, sizeof a);}
  14.     Matrix operator * (const Matrix &m) {
  15.         Matrix ans;
  16.         for(int i = 1; i < 3; ++i)
  17.             for(int j = 1; j < 3; ++j)
  18.                 for(int t = 1; t < 3; ++t)
  19.                     ans.a[i][j] = (ans.a[i][j] + a[i][t] * m.a[t][j] % mod) % mod;
  20.         return ans;
  21.     }
  22. };
  23. ll n;
  24.  
  25. void Read(){
  26.     cin >> n;
  27. }
  28.  
  29. Matrix Pow(Matrix a, ll b){
  30.     Matrix tmp;
  31.     for(int i = 1; i < 3; ++i)
  32.         tmp.a[i][i] = 1;
  33.     for(; b > 0; b >>= 1){
  34.         if(b & 1) tmp = tmp * a;
  35.         a = a * a;
  36.     }
  37.     return tmp;
  38. }
  39.  
  40. void Solve(){
  41.     Matrix ans, cur;
  42.     cur.a[1][2] = cur.a[2][2] = 1;
  43.     cur.a[2][1] = 2;
  44.     ans.a[1][1] = 1;
  45.     ans.a[2][1] = 3;
  46.     if(n == 1){
  47.         cout << 1 << "\n";
  48.         return;
  49.     }
  50.     cur = Pow(cur, n - 1ll);
  51.     cur = cur * ans;
  52.     cout << cur.a[1][1] << "\n";
  53. }
  54.  
  55. signed main(){
  56.     ios::sync_with_stdio(0);
  57.     cin.tie(0);
  58.     cout.tie(0); if(fopen(task".INP", "r"))
  59.     freopen(task".INP", "r", stdin),
  60.     freopen(task".OUT", "w", stdout);
  61.     int t;
  62.     cin >> t;
  63.     while(t--)
  64.         Read(),
  65.         Solve();
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment