[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;
}
相關文章
- CF1093E [Intersection of Permutations]
- Baby Ehab Plays with Permutations 題解
- sequence to sequence模型模型
- Sequence recognition
- uvm的sequence
- PostgreSQL 序列(Sequence)SQL
- ORACLE SEQUENCE用法Oracle
- python sequence序列Python
- Rainbow Bracket SequenceAIRacket
- Increasing Sequence with Fixed OR
- PostgreSQL sequence (一)SQL
- 論文閱讀:Sequence to sequence learning for joint extraction of entities and relations
- FSM:Sequence 1101 recognizer
- F - Two Sequence Queries
- 裁剪序列Cut the Sequence
- DeepLearning – Overview of Sequence modelView
- E. Block SequenceBloC
- mysql實現sequenceMySql
- LeetCode 444 sequence reconstructionLeetCodeStruct
- [LeetCode]60. Permutation SequenceLeetCode
- HDU 1711 Number Sequence(KMP)KMP
- A Proof of Golden Section of Fibonacci SequenceGo
- oracle的scn及sequenceOracle
- 演算法題 - Pop Sequence演算法
- HDU 6047 Maximum Sequence (貪心)
- Least Cost Bracket Sequence(貪心)ASTRacket
- 從SEQUENCE跳號說起
- [ABC234G] Divide a SequenceIDE
- abc134E - Sequence Decomposing
- [20220321]探究oracle sequence.txtOracle
- Keras版Sequence2Sequence對對聯實戰——自然語言處理技術Keras自然語言處理
- HDU 6299-Balanced Sequence(貪心)
- Leetcode 298 Binary Tree Longest Consecutive SequenceLeetCode
- 不同於Oracle:SEQUENCE的區別Oracle
- [題解]CF13C Sequence
- [題解]P4597 序列 sequence
- CCPC Final 2023 B. Periodic Sequence
- [20220322]探究oracle sequence 2.txtOracle