[AGC031D]A Sequence of Permutations
Description
對於兩個長度為n排列p,q,定義f(p,q)為一個排列f,滿足fpi=qi
對於一個排列的序列{an},定義a1=p,a2=q,an=f(an-2,an-1)
求ak
n<=1e5,k<=1e9
Solution
我能說我抽代基礎為0嗎QwQ
考慮兩個置換p,q之間的符合,定義為f=pq,結果為一個置換f,滿足fi=pqi
再定義g=p^-1為p的逆,滿足gpi=i,逆有個性質就是
那麼我們可以知道f(p,q)=qp^-1
考慮把a的前幾項寫出來:
注意這裡a7寫成這樣是為了契合我們找到的規律
設
我們可以發現
那麼我們可以歸納出,
然後我們就只需要求出,這東西可以直接把所有輪換找出來然後位移
複雜度O(n)
Code
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5+5;
typedef vector<int> vec;
int n,k,c[N];
bool vis[N];
vec a[7],A,p,q;
vec inv(vec a) {
vec b;b.resize(n);
fo(i,0,n-1) b[a[i]]=i;
return b;
}
vec mult(vec a,vec b) {
vec c;c.resize(n);
fo(i,0,n-1) c[i]=a[b[i]];
return c;
}
vec pwr(vec a,int t) {
vec b;b.resize(n);
fo(i,0,n-1) vis[i]=0;
fo(i,0,n-1)
if (!vis[i]) {
if (a[i]==i) {b[i]=i;continue;}
int x=i,m=0;
do {vis[c[m++]=x]=1;x=a[x];} while (x!=i);
fo(j,0,m-1) b[c[j]]=c[(j+t)%m];
}
return b;
}
int main() {
scanf("%d%d",&n,&k);
p.resize(n);q.resize(n);
fo(i,0,n-1) scanf("%d",&p[i]),p[i]--;
fo(i,0,n-1) scanf("%d",&q[i]),q[i]--;
a[1]=p;a[2]=q;
if (n<=6) {
fo(i,3,n) a[i]=mult(a[i-1],inv(a[i-2]));
fo(i,0,n-1) printf("%d ",a[n][i]+1);
return 0;
}
fo(i,3,6) a[i]=mult(a[i-1],inv(a[i-2]));
A.resize(n);fo(i,0,n-1) A[i]=i;
A=mult(A,q);A=mult(A,inv(p));A=mult(A,inv(q));A=mult(A,p);
A=pwr(A,(k-1)/6);
vec an=a[(k-1)%6+1];
an=mult(A,an);an=mult(an,inv(A));
fo(i,0,n-1) printf("%d ",an[i]+1);
return 0;
}
相關文章
- Leetcode PermutationsLeetCode
- Leetcode-PermutationsLeetCode
- Permutations leetcode javaLeetCodeJava
- sequence to sequence模型模型
- Leetcode-Permutations IILeetCode
- Permutations II leetcode javaLeetCodeJava
- Baby Ehab Plays with Permutations 題解
- ORACLE SEQUENCEOracle
- Sequence recognition
- ORACLE SEQUENCE用法Oracle
- PostgreSQL 序列(Sequence)SQL
- PostgreSQL sequence (一)SQL
- Oracle - Sequence序列Oracle
- Oracle Sequence NocacheOracle
- Oracle序列sequenceOracle
- codeforces 341C Iahub and Permutations(組合數dp)
- 【轉】MySQL中增加sequence管理功能(模擬建立sequence)MySql
- 論文閱讀:Sequence to sequence learning for joint extraction of entities and relations
- python sequence序列Python
- mysql實現sequenceMySql
- 3. Swift SequenceSwift
- Oracle之Sequence(序列)Oracle
- request gap sequence is FailedAI
- ACM Longest Repeated SequenceACM
- Oracle Sequence Audses$研究Oracle
- oracle sequence語法Oracle
- oracle sequence 試用Oracle
- 詳解序列(sequence)
- E. Block SequenceBloC
- 裁剪序列Cut the Sequence
- F - Two Sequence Queries
- Increasing Sequence with Fixed OR
- Rainbow Bracket SequenceAIRacket
- Codeforces 888D Almost Identity Permutations:錯排公式IDE公式
- oracle的scn及sequenceOracle
- Linux_Arithmetic_SequenceLinux
- DeepLearning – Overview of Sequence modelView
- Oracle中Sequence的使用Oracle