<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
# Permutation Group - 置換群
--------
#### 問題
<p id="i">長度為\(n\)的序列\(s = [x_0, x_1, x_2, \dots, x_{n-1} ]\)上有\(n\)個數字,每個數字各不相同,且任意的數字都滿足\(\forall x_i \in [0, n-1]\)。例如\(s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\)就是這樣一個長度為\(10\)的序列,擁有\(10\)個各不相同的數字,且每個數字都滿足\(i \in [0, 9]\)。 </p>
<p id="i">給出長度相同的序列\(t = [y_0, y_1, y_2, \dots, y_{n-1} ]\),與序列\(s\)滿足相同的條件(有\(n\)個數字,每個數字各不相同,且任意的數字都滿足\(\forall y_i \in [0, n-1]\))。例如
\[t = [3, 4, 2, 6, 1, 7, 0, 5, 9, 8]\]
我們將序列\(t\)作為序列\(s\)的置換準則,置換操作可以令\(s[t[i]] = s[i]\),其中\(i \in [0, n-1]\)。將序列\(s\)中的所有元素按照序列\(t\)中的下標進行一次置換,稱為一次置換操作。例如
\[s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\]
經過序列\(t\)的一次置換后,變成
\[[6, 4, 2, 0, 1, 7, 3, 5, 9, 8]\]
</p>
<p id="i">求長度為\(n\)的序列\(s\)在置換原則t下,經過\(k\)次置換操作后的元素排列情況。 </p>
解法:
<p id="i">暴力\(k\)次循環時間復雜度過高。我們來仔細考察上面例子中的序列\(s\)及其置換準則\(t\):
\[
s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] \\
t = [3, 4, 2, 6, 1, 7, 0, 5, 9, 8]
\]
<p id="i">\((1)\)對于第\(0\)個元素,第\(1\)次置換后\(s[0] = 6\); </p>
\[ s_1 = [6, 4, 2, 0, 1, 7, 3, 5, 9, 8] \]
<p id="i">\((2)\)對于第\(0\)個元素,第\(2\)次置換后\(s[0] = 3\); </p>
\[ s_2 = [3, 1, 2, 6, 4, 5, 0, 7, 8, 9] \]
<p id="i">\((3)\)對于第\(0\)個元素,第\(3\)次置換后\(s[0] = 0\); </p>
\[ s_2 = [0, 4, 2, 3, 1, 7, 6, 5, 9, 8] \]
<p id="i">\((4)\)對于第\(0\)個元素,第\(4\)次置換后\(s[0] = 6\); </p>
\[ s_2 = [6, 1, 2, 0, 4, 5, 3, 7, 8, 9] \]
<p id="i">我們可以看出第\(4\)次置換和第\(1\)置換后\(s[0]\)的結果相同,這說明置換操作是有周期性的,第0個元素\(s[0]\)的周期為3,即\(s[0]_{i+3} = s[0]_{i}\),其中\(i \in [0, 6]\)。不同元素的周期是不同的,比如\(s[2]\)的周期為1(\(s[2]_{i+1} = s[2]_{i}\),其中\(i \in [0, 8]\)),因為\(s[2] \equiv 2)。 </p>
<p id="i">經過觀察發現任意元素\(s[i]\)都擁有一個循環周期,因此只要確定每個元素的周期,就可以避免暴力循環,直接求出k次置換操作后的序列s。該算法的時間復雜度為\(O(n)\)。</p>
</div>
* [Upper Folder - 上一級目錄](../)
* [Source Code - 源碼](https://github.com/zhaochenyou/Way-to-Algorithm/blob/master/src/CombinatorialMathematics/PermutationGroup.hpp)
* [Test Code - 測試](https://github.com/zhaochenyou/Way-to-Algorithm/blob/master/src/CombinatorialMathematics/PermutationGroup.cpp)
--------
- Content 目錄
- Preface 前言
- Chapter-1 Sort 第1章 排序
- InsertSort 插入排序
- BubbleSort 冒泡排序
- QuickSort 快速排序
- MergeSort 歸并排序
- Chapter-2 Search 第2章 搜索
- BinarySearch 二分查找法(折半查找法)
- BruteForce 暴力枚舉
- Recursion 遞歸
- BreadthFirstSearch 廣度優先搜索
- BidirectionalBreadthSearch 雙向廣度搜索
- AStarSearch A*搜索
- DancingLink 舞蹈鏈
- Chapter-3 DataStructure 第3章 數據結構
- DisjointSet 并查集
- PrefixTree(TrieTree) 前綴樹
- LeftistTree(LeftistHeap) 左偏樹(左偏堆)
- SegmentTree 線段樹
- FenwickTree(BinaryIndexedTree) 樹狀數組
- BinarySearchTree 二叉查找樹
- AVLTree AVL平衡樹
- RedBlackTree 紅黑樹
- Chapter-4 DynamicProgramming 第4章 動態規劃
- Chapter-5 GraphTheory 第5章 圖論
- Chapter-6 Calculation 第6章 計算
- LargeNumber 大數字
- Exponentiation 求冪運算
- Chapter-7 CombinatorialMathematics 第7章 組合數學
- FullPermutation 全排列
- UniqueFullPermutation 唯一的全排列
- Combination 組合
- DuplicableCombination (元素)可重復的組合
- Subset 子集
- UniqueSubset 唯一的子集
- Permutation 排列
- PermutationGroup 置換群
- Catalan 卡特蘭數
- Chapter-8 NumberTheory 第8章 數論
- Sieve 篩選算法
- Euclid 歐幾里得
- EuclidExtension 歐幾里得擴展
- ModularLinearEquation 模線性方程
- ChineseRemainerTheorem 中國剩余定理
- ModularExponentiation 模冪運算
- Chapter-9 LinearAlgebra 第9章 線性代數
- Chapter-10 AnalyticGeometry 第10章 解析幾何
- Chapter-11 TextMatch 第11章 文本匹配
- SimpleMatch 簡單匹配
- AhoCorasickAutomata AC自動機
- KnuthMorrisPratt KMP匹配算法
- RabinKarp RabinKarp算法
- BoyerMoore BoyerMoore算法
- Chapter-12 GameTheory 第12章 博弈論
- BashGame 巴什博弈
- WythoffGame 威佐夫博弈
- NimGame 尼姆博弈