Advertisement
Jaydeep999997

Funny Gnomes

Mar 30th, 2021
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int M = 500;
  5. const int B = 30;
  6.  
  7. int n, m;
  8.  
  9. class matrix
  10. {
  11. public:
  12.  
  13.     bitset<M> r[M], c[M];
  14.     int dim;
  15.  
  16.     matrix()
  17.     {
  18.  
  19.     }
  20.  
  21.     matrix(int _dim)
  22.     {
  23.         dim = _dim;
  24.     }
  25.  
  26.     void reset()
  27.     {
  28.         for (int i = 0; i < dim; i++)
  29.         {
  30.             r[i].reset();
  31.             c[i].reset();
  32.         }
  33.     }
  34.  
  35.     void makeiden()
  36.     {
  37.         reset();
  38.         for (int i = 0; i < dim; i++)
  39.         {
  40.             r[i][i] = 1;
  41.             c[i][i] = 1;
  42.         }
  43.     }
  44.  
  45.     matrix operator * (const matrix &m) const
  46.     {
  47.         matrix res(dim);
  48.         for (int i = 0; i < dim; i++)
  49.         {
  50.             for (int j = 0; j < dim; j++)
  51.             {
  52.                 res.r[i][j] = ((r[i] & m.c[j]).count() > 0);
  53.                 res.c[j][i] = res.r[i][j];
  54.             }
  55.         }
  56.         return res;
  57.     }
  58. };
  59.  
  60. matrix obj[B];
  61.  
  62. matrix power(matrix a, int b)
  63. {
  64.     matrix res(a.dim);
  65.     res.makeiden();
  66.     while (b > 0)
  67.     {
  68.         if (b & 1)
  69.         {
  70.             res = res * a;
  71.         }
  72.         a = a * a;
  73.         b >>= 1;
  74.     }
  75.     return res;
  76. }
  77.  
  78. void prepare()
  79. {
  80.     for (int i = 1; i < B; i++)
  81.     {
  82.         obj[i] = obj[i - 1] * obj[i - 1];
  83.     }
  84. }
  85.  
  86. bitset<M> fast_power(int x, int k)
  87. {
  88.     bitset<M> res;
  89.     res[x] = 1;
  90.     for (int pos = B - 1; pos >= 0; pos--)
  91.     {
  92.         if (k & (1LL << pos))
  93.         {
  94.             bitset<M> prev = res;
  95.             for (int i = 0; i < n; i++)
  96.             {
  97.                 res[i] = ((prev & obj[pos].c[i]).count() > 0);
  98.             }
  99.         }
  100.     }
  101.     return res;
  102. }
  103.  
  104. signed main()
  105. {
  106.     ios_base::sync_with_stdio(false);
  107.     cin.tie(NULL); cout.tie(NULL);
  108.  
  109. #ifndef ONLINE_JUDGE
  110.     freopen("input.txt", "r", stdin);
  111.     freopen("output.txt", "w", stdout);
  112. #endif
  113.  
  114.     cin >> n;
  115.     for (int i = 0; i < B; i++)
  116.     {
  117.         obj[i] = matrix (n);
  118.     }
  119.     for (int i = 0; i < n; i++)
  120.     {
  121.         for (int j = 0; j < n; j++)
  122.         {
  123.             int x;
  124.             cin >> x;
  125.             obj[0].r[i][j] = obj[0].c[j][i] = x;
  126.         }
  127.     }
  128.     prepare();
  129.     cin >> m;
  130.     for (int i = 0; i < m; i++)
  131.     {
  132.         int k, x;
  133.         cin >> k >> x;
  134.         --x;
  135.         bitset<M> res = fast_power(x, k);
  136.         vector<int> ans;
  137.         for (int j = 0; j < n; j++)
  138.         {
  139.             if (res[j])
  140.             {
  141.                 ans.push_back(j + 1);
  142.             }
  143.         }
  144.         cout << ans.size() << endl;
  145.         if (ans.size() == 0)
  146.         {
  147.             cout << -1 << '\n';
  148.         }
  149.         else
  150.         {
  151.             for (auto &v : ans)
  152.             {
  153.                 cout << v << " ";
  154.             }
  155.             cout << '\n';
  156.         }
  157.     }
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement