Advertisement
mickypinata

PROG-T1134: Sequence

Sep 13th, 2021
1,831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5.  
  6. const int MD = 2553;
  7. const int MS = 4;
  8. const int logN = log2(1e18);
  9.  
  10. int bl[logN + 1][MS][MS], ans[2][MS];
  11.  
  12. int main(){
  13.  
  14.     int a, b, c, d, e, f, g, h, Q;
  15.     scanf("%d%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g, &h, &Q);
  16.     vector<vector<int>> base = {{e, f, g, h}, {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}};
  17.     for(int i = 0; i < MS; ++i){
  18.         for(int j = 0; j < MS; ++j){
  19.             bl[0][i][j] = base[i][j];
  20.         }
  21.     }
  22.     for(int e = 1; e <= logN; ++e){
  23.         for(int i = 0; i < MS; ++i){
  24.             for(int j = 0; j < MS; ++j){
  25.                 for(int k = 0; k < MS; ++k){
  26.                     bl[e][i][j] = (bl[e][i][j] + bl[e - 1][i][k] * bl[e - 1][k][j]) % MD;
  27.                 }
  28.             }
  29.         }
  30.     }
  31.     while(Q--){
  32.         lli x;
  33.         scanf("%lld", &x);
  34.         if(x <= 4){
  35.             if(x == 1){
  36.                 cout << a << '\n';
  37.             } else if(x == 2){
  38.                 cout << b << '\n';
  39.             } else if(x == 3){
  40.                 cout << c << '\n';
  41.             } else if(x == 4){
  42.                 cout << d << '\n';
  43.             }
  44.             continue;
  45.         }
  46.         x -= 4;
  47.         ans[0][0] = d;  ans[0][1] = c;
  48.         ans[0][2] = b;  ans[0][3] = a;
  49.         int cur = 1;
  50.         for(int e = logN; e >= 0; --e){
  51.             if(x >= ((lli)1 << e)){
  52.                 int prv = cur ^ 1;
  53.                 for(int i = 0; i < MS; ++i){
  54.                     ans[cur][i] = (ans[prv][0] * bl[e][i][0] + ans[prv][1] * bl[e][i][1] +
  55.                                     ans[prv][2] * bl[e][i][2] + ans[prv][3] * bl[e][i][3]) % MD;
  56.                 }
  57.                 x -= ((lli)1 << e);
  58.                 cur ^= 1;
  59.             }
  60.         }
  61.         cout << ans[cur ^ 1][0] << '\n';
  62.     }
  63.  
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement