Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. template<typename T, typename Mul>
  8. constexpr T fast_pow(T x, T identity, int y, Mul mul) {
  9.     T p = identity;
  10.     while (y) {
  11.         if (y & 1) {
  12.             p = mul(p, x);
  13.         }
  14.         y >>= 1;
  15.         x = mul(x, x);
  16.     }
  17.     return p;
  18. }
  19.  
  20. int main()
  21. {
  22.     constexpr int mod = 1e9 + 7;
  23.     ifstream fin("bani.in");
  24.     ofstream fout("bani.out");
  25.     int n;
  26.     int type;
  27.     fin >> type;
  28.     fin >> n;
  29.     auto mul_numbers = [mod = mod](int a, int b) -> int {
  30.         return 1ll * a * b % mod;
  31.     };
  32.     auto mul_matrices = [mod = mod](const vector<vector<int>>& a,
  33.         const vector<vector<int>>& b) {
  34.             int n = a.size();
  35.             vector<vector<int>> r(n, vector<int>(n, 0));
  36.             for (int i = 0; i < n; ++i) {
  37.                 for (int j = 0; j < n; ++j) {
  38.                     for (int k = 0; k < n; ++k) {
  39.                         r[i][j] = (1ll * r[i][j] +
  40.                             1ll * a[i][k] * b[k][j] % mod) % mod;
  41.                     }
  42.                 }
  43.             }
  44.             return r;
  45.     };
  46.     if (type == 1) {
  47.         fout << 5ll * fast_pow(2, 1, n - 1, mul_numbers) % mod;
  48.     }
  49.     else {
  50.         int r = 0;
  51.         vector<vector<int>> m = {
  52.             {0, 1, 1, 0, 0},
  53.             {1, 0, 0, 1, 0},
  54.             {1, 0, 1, 0, 0},
  55.             {0, 1, 1, 0, 1},
  56.             {1, 0, 0, 1, 0}
  57.         };
  58.         vector<vector<int>> identity = {
  59.             {1, 0, 0, 0, 0},
  60.             {0, 1, 0, 0, 0},
  61.             {0, 0, 1, 0, 0},
  62.             {0, 0, 0, 1, 0},
  63.             {0, 0, 0, 0, 1}
  64.         };
  65.  
  66.         m = fast_pow(m, identity, n - 1, mul_matrices);
  67.         for (int i = 0; i < m.size(); ++i) {
  68.             for (int j = 0; j < m.size(); ++j) {
  69.                 r = (1ll * r + m[i][j]) % mod;
  70.             }
  71.         }
  72.         fout << r;
  73.     }
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement