luogu P5151 HKE与他的小朋友

时间:2022-05-25 00:03:47

嘟嘟嘟



看到\(i\)变成了\(A_i\),我突然想起了置换这个东西。于是马上到网上学了一遍轮换乘法。

手模后发现轮换乘法满足结合律,但不满足交换律。

于是就可以快速幂啦。

需要注意的是每一次相乘是\(O(n)\)的,因此总复杂度为\(O(n \log n)\)。

代码一看就懂

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, k, a[maxn]; int tp[maxn], ret[maxn];
In void mul(int* ret, int* a)
{
for(int i = 1; i <= n; ++i) tp[i] = ret[a[i]];
for(int i = 1; i <= n; ++i) ret[i] = tp[i];
}
In void quickpow(int* a, int b)
{
for(int i = 1; i <= n; ++i) ret[i] = i;
for(; b; b >>= 1, mul(a, a))
if(b & 1) mul(ret, a);
} int main()
{
n = read(); k = read();
for(int i = 1; i <= n; ++i) a[i] = read();
quickpow(a, k);
for(int i = 1; i <= n; ++i) tp[ret[i]] = i;
for(int i = 1; i <= n; ++i) write(tp[i]), space; enter;
return 0;
}