Advertisement
fooker

solve

Jan 21st, 2024
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. const ll nmax = 1e9+7;
  6. const ll nmax2 = 998244353;
  7.  
  8. void solve(){
  9.  
  10.     ll a, b, c, d, e;
  11.     std::cin >> a >> b >> c >> d >> e;
  12.  
  13.     ll n;
  14.     std::cin >> n;
  15.  
  16.     if (n == 1){
  17.         std::cout << 0 << '\n';
  18.         return;
  19.     } else if (n == 2){
  20.         std::cout << 1 << '\n';
  21.         return;
  22.     } else if (n == 3){
  23.         std::cout << 2 << '\n';
  24.         return;
  25.     } else if (n == 4){
  26.         std::cout << 3 << '\n';
  27.         return;
  28.     }
  29.  
  30.     std::vector<std::vector<ll>> adj(5, std::vector<ll>(5));
  31.     adj[0][0] = a;
  32.     adj[0][1] = b;
  33.     adj[0][2] = c;
  34.     adj[0][3] = d;
  35.     adj[0][4] = e;
  36.    
  37.     adj[1][0] = 1;
  38.     adj[1][1] = 0;
  39.     adj[1][2] = 0;
  40.     adj[1][3] = 0;
  41.     adj[1][4] = 0;
  42.  
  43.     adj[2][0] = 0;
  44.     adj[2][1] = 1;
  45.     adj[2][2] = 0;
  46.     adj[2][3] = 0;
  47.     adj[2][4] = 0;
  48.  
  49.     adj[3][0] = 0;
  50.     adj[3][1] = 0;
  51.     adj[3][2] = 1;
  52.     adj[3][3] = 0;
  53.     adj[3][4] = 0;
  54.  
  55.     adj[4][0] = 0;
  56.     adj[4][1] = 0;
  57.     adj[4][2] = 0;
  58.     adj[4][3] = 0;
  59.     adj[4][4] = 1;
  60.  
  61.     auto matrix_self = [&] (std::vector<std::vector<ll>> &vect, ll k) -> void {
  62.         std::vector<std::vector<ll>> res(k, std::vector<ll>(k, 0));
  63.         for (ll i = 0; i < k; i++){
  64.             for (ll j = 0; j < k; j++){
  65.                 ll ans = 0;
  66.                 for (ll l = 0; l < k; l++){
  67.                     ans += vect[i][l] * vect[l][j];
  68.                     ans %= nmax;
  69.                 }
  70.                 res[i][j] = ans;
  71.             }
  72.         }
  73.         for (ll i = 0; i < k; i++){
  74.             for (ll j = 0; j < k; j++) vect[i][j] = res[i][j];
  75.         }
  76.     };
  77.    
  78.     auto matrix_mul = [&] (std::vector<std::vector<ll>> vect1, std::vector<std::vector<ll>> vect2) -> std::vector<std::vector<ll>> {
  79.         std::vector<std::vector<ll>> res(5, std::vector<ll>(1, 0));
  80.         res[0][0] = vect1[0][0] * vect2[0][0] + vect1[0][1] * vect2[1][0] + vect1[0][2] * vect2[2][0] + vect1[0][3] * vect2[3][0] + vect1[0][4] * vect2[4][0];
  81.         res[0][0] %= nmax;
  82.         res[1][0] = vect1[1][0] * vect2[0][0] + vect1[1][1] * vect2[1][0] + vect1[1][2] * vect2[2][0] + vect1[1][3] * vect2[3][0] + vect1[1][4] * vect2[4][0];
  83.         res[1][0] %= nmax;
  84.         res[2][0] = vect1[2][0] * vect2[0][0] + vect1[2][1] * vect2[1][0] + vect1[2][2] * vect2[2][0] + vect1[2][3] * vect2[3][0] + vect1[2][4] * vect2[4][0];
  85.         res[2][0] %= nmax;
  86.         res[3][0] = vect1[3][0] * vect2[0][0] + vect1[3][1] * vect2[1][0] + vect1[3][2] * vect2[2][0] + vect1[3][3] * vect2[3][0] + vect1[3][4] * vect2[4][0];
  87.         res[3][0] %= nmax;
  88.         res[4][0] = vect1[4][0] * vect2[0][0] + vect1[4][1] * vect2[1][0] + vect1[4][2] * vect2[2][0] + vect1[4][3] * vect2[3][0] + vect1[4][4] * vect2[4][0];
  89.         res[4][0] %= nmax;
  90.         return res;
  91.     };
  92.    
  93.     auto matrix_exp = [&] (ll m, std::vector<std::vector<ll>> A) -> std::vector<std::vector<ll>> {
  94.         std::vector<std::vector<ll>> fin(5, std::vector<ll>(1, 0));
  95.         fin[0][0] = 3;
  96.         fin[1][0] = 2;
  97.         fin[2][0] = 1;
  98.         fin[3][0] = 0;
  99.         fin[4][0] = 1;
  100.         while (m > 0){
  101.             if (m & 1) fin = matrix_mul(A, fin);
  102.             matrix_self(A, 5);
  103.             m >>= 1;
  104.         }      
  105.         return fin;
  106.     };
  107.  
  108.     auto final_answer = matrix_exp(n - 4, adj);
  109.     std::cout << final_answer[0][0] << '\n';
  110. }
  111.  
  112. int main(){
  113.     std::ios_base::sync_with_stdio(false);
  114.     std::cin.tie(0);
  115.     std::cout.tie(0);
  116.  
  117.     int t;
  118.     std::cin >> t;
  119.     // t = 1;
  120.     while(t--){
  121.         solve();
  122.     }
  123.     return 0;
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement