Permutations and Transpositions

Ref: https://codeforces.com/blog/entry/111187 (nor’s blog)

Def: Permutation is a shuffle of range [1, N], in other word, permutation is a bijection on a set.

Inversion

Inversion is pair (i,j), i < j such that p[i] > p[j]. Inversion count of an array if the number of inversion in the array.

Cycle Decomposition

Every permutation is composed of cycles. Proof?

Each permutation can be decomposed into disjoint cycles. E.g. [23154] = (123)(45)

Problems:

  1. CSES Planet Queries I https://cses.fi/problemset/task/1750
  2. CSES Planet Queries II https://cses.fi/problemset/task/1160

Transpositions

Problem: Lucky Permutation https://codeforces.com/contest/1768/problem/D

Hint: Cycle decomposition!

Number of transpositions required to sort an array?

Parity of Transpositions

Inversion

Involution

Power of a permutation

Order of a permutation

Square Root of a permutation

Square Root of a permutation : https://codeforces.com/contest/612/problem/E

Counting number of permutations with inversion count k

This problem can be solved with dynamic programming in N*K time. </br> Consider dp[N][K]. This denotes number of permutations with N distinct elements and K inversion count. </br>

Construction :

	ll N, K; 
	cin >> N >> K;	
	
	dp[1][0] = 1;
	for (ll j=1;j<=K;j++) dp[1][j] = dp[1][j-1]; 
	for (ll i=2;i<=N;i++){
		for (ll inversions = 0; inversions <= K; inversions++){
			ll mn = max (0LL, inversions - i + 1); 
			ll mx = inversions;
			if (mn == 0) dp[i][inversions] = dp[i-1][mx];
			else dp[i][inversions] = (dp[i-1][mx] - dp[i-1][mn-1] + mod) % mod; 
			if (inversions) dp[i][inversions] = (mod + dp[i][inversions] + dp[i][inversions-1]) % mod;
		}
	}
	
	if (!K) cout << 1 << endl;
	else cout << (mod + dp[N][K] - dp[N][K-1]) % mod;