luogu P5151 HKE與他的小朋友
阿新 • • 發佈:2018-12-26
嘟嘟嘟
看到\(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; }