Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <set>
- #include <map>
- #define forn(i, n) for (int i = 0; i < int(n); i++)
- #define fore(i, l, r) for (int i = int(l); i < int(r); i++)
- #define all(a) a.begin(), a.end()
- #define mp make_pair
- #define pb(a) push_back(a)
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii;
- //484, Возведите заданную перестановку p в степень k.
- void takeDegree(int m, vector<int> &p, int n)
- {
- vector<bool> used(p.size());
- vector<vector<int>> result;
- fore(i, 0, p.size())
- {
- if (!used[i])
- {
- vector<int> worker;
- int j = i;
- while(!used[j])
- {
- used[j] = true;
- worker.push_back(p[j]);
- j = p[j];
- }
- result.push_back(worker);
- }
- }
- vector<int> ans(n, -1);
- fore(i, 0, result.size())
- {
- fore(j, 0, result[i].size())
- {
- ans[result[i][j]] = result[i][(j + m) % result[i].size()];
- }
- }
- fore(i, 0, n)
- printf("%d ", ans[i]);
- }
- int main()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n, k;
- scanf("%d%d", &n, &k);
- vector<int> v(n);
- forn(i, n)
- {
- int cur;
- scanf("%d", &cur);
- v[i] = cur;
- }
- takeDegree(k, v, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement