生活随笔
收集整理的這篇文章主要介紹了
牛客多校2 - Just Shuffle(置换群的幂)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個置換 B,求出一個置換 A ,使得?,k 是一個大質(zhì)數(shù)
題目分析:等式兩邊同時乘以 t 次冪,變?yōu)?,顯然當 t 為 k 的逆元時,有式子?,所以求一下 B 的 t 次冪就好了
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;int a[N],ans[N];bool vis[N];int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n,k;scanf("%d%d",&n,&k);for(int i=1;i<=n;i++)scanf("%d",a+i);for(int i=1;i<=n;i++){if(vis[i])continue;vector<int>circle;int pos=i;while(!vis[pos]){circle.push_back(pos);vis[pos]=true;pos=a[pos];}int sz=circle.size();int t=0;while(t<sz){if(1LL*t*k%sz==1)break;t++;}for(int i=0;i<sz;i++)ans[circle[i]]=circle[(i+t)%sz];}for(int i=1;i<=n;i++)printf("%d ",ans[i]);return 0;
}
?
總結(jié)
以上是生活随笔為你收集整理的牛客多校2 - Just Shuffle(置换群的幂)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。