Dang_Quan_10_Tin

TASKSELECT ( Đề thi) Automata Aproach

Sep 7th, 2021
1,297
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 <cstdio>
  3. #include <cstring>
  4. #define task "TASKSELECT"
  5.  
  6. using namespace std;
  7. using ll = long long;
  8.  
  9. const int N = 122 + 2;
  10. const ll mod = 1e9 + 7;
  11. int k, n;
  12. struct Matrix
  13. {
  14.     ll a[N][N];
  15.     Matrix()
  16.     {
  17.         memset(a, 0, sizeof a);
  18.     }
  19.     Matrix operator * (const Matrix &x)
  20.     {
  21.         Matrix ans;
  22.         for(int i = 0; i <= (k + 1) * (k + 1); ++i)
  23.             for(int j = 0; j <= (k + 1) * (k + 1); ++j)
  24.                 for(int h = 0; h <= (k + 1) * (k + 1); ++h)
  25.                     (ans.a[i][h] += a[i][j] * x.a[j][h]) %= mod;
  26.         return ans;
  27.     }
  28. };
  29.  
  30. Matrix Pow(Matrix &a, int b)
  31. {
  32.     Matrix ans;
  33.     for(int i = 0; i < N; ++i)
  34.         ans.a[i][i] = 1;
  35.     for(; b > 0; b >>= 1)
  36.     {
  37.         if(b & 1)
  38.             ans = ans * a;
  39.         a = a * a;
  40.     }
  41.     return ans;
  42. }
  43.  
  44.  
  45. void Read()
  46. {
  47.     cin >> k >> n;
  48. }
  49.  
  50. void Solve()
  51. {
  52.     int l = k;
  53.     Matrix a;
  54.     a.a[0][1] = 1;
  55.     for(int i = 1; i <= k; ++i)
  56.     {
  57.         for(int j = 1; j < i; ++j)
  58.         {
  59.             ++l;
  60.             if(j == 1)
  61.                 a.a[i][l] = 1;
  62.             else
  63.                 a.a[l - 1][l] = 1;
  64.         }
  65.         if(i != 1)
  66.             a.a[l][i] = 1;
  67.         else
  68.             a.a[i][i] = 1;
  69.         if(i < k)
  70.         {
  71.             for(int j = 1; j <= i; ++j)
  72.             {
  73.                 ++l;
  74.                 if(j == 1)
  75.                     a.a[i][l] = 1;
  76.                 else
  77.                     a.a[l - 1][l] = 1;
  78.             }
  79.             a.a[l][i + 1] = 1;
  80.         }
  81.     }
  82.     a = Pow(a, n);
  83.     cout << a.a[0][k] << "\n";
  84. }
  85.  
  86. int32_t main()
  87. {
  88.     if(fopen(task".INP", "r"))
  89.     {
  90.         freopen(task".INP", "r", stdin);
  91.         freopen(task".OUT", "w", stdout);
  92.     }
  93.     ios_base::sync_with_stdio(0);
  94.     cin.tie(0);
  95.     cout.tie(0);
  96.     int t;
  97.     cin >> t;
  98.     while(t--)
  99.     {
  100.         Read();
  101.         Solve();
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment