Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int M = 500;
- const int B = 30;
- int n, m;
- class matrix
- {
- public:
- bitset<M> r[M], c[M];
- int dim;
- matrix()
- {
- }
- matrix(int _dim)
- {
- dim = _dim;
- }
- void reset()
- {
- for (int i = 0; i < dim; i++)
- {
- r[i].reset();
- c[i].reset();
- }
- }
- void makeiden()
- {
- reset();
- for (int i = 0; i < dim; i++)
- {
- r[i][i] = 1;
- c[i][i] = 1;
- }
- }
- matrix operator * (const matrix &m) const
- {
- matrix res(dim);
- for (int i = 0; i < dim; i++)
- {
- for (int j = 0; j < dim; j++)
- {
- res.r[i][j] = ((r[i] & m.c[j]).count() > 0);
- res.c[j][i] = res.r[i][j];
- }
- }
- return res;
- }
- };
- matrix obj[B];
- matrix power(matrix a, int b)
- {
- matrix res(a.dim);
- res.makeiden();
- while (b > 0)
- {
- if (b & 1)
- {
- res = res * a;
- }
- a = a * a;
- b >>= 1;
- }
- return res;
- }
- void prepare()
- {
- for (int i = 1; i < B; i++)
- {
- obj[i] = obj[i - 1] * obj[i - 1];
- }
- }
- bitset<M> fast_power(int x, int k)
- {
- bitset<M> res;
- res[x] = 1;
- for (int pos = B - 1; pos >= 0; pos--)
- {
- if (k & (1LL << pos))
- {
- bitset<M> prev = res;
- for (int i = 0; i < n; i++)
- {
- res[i] = ((prev & obj[pos].c[i]).count() > 0);
- }
- }
- }
- return res;
- }
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- cin >> n;
- for (int i = 0; i < B; i++)
- {
- obj[i] = matrix (n);
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- int x;
- cin >> x;
- obj[0].r[i][j] = obj[0].c[j][i] = x;
- }
- }
- prepare();
- cin >> m;
- for (int i = 0; i < m; i++)
- {
- int k, x;
- cin >> k >> x;
- --x;
- bitset<M> res = fast_power(x, k);
- vector<int> ans;
- for (int j = 0; j < n; j++)
- {
- if (res[j])
- {
- ans.push_back(j + 1);
- }
- }
- cout << ans.size() << endl;
- if (ans.size() == 0)
- {
- cout << -1 << '\n';
- }
- else
- {
- for (auto &v : ans)
- {
- cout << v << " ";
- }
- cout << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement