MCPcopy Create free account
hub / github.com/SuprDewd/CompetitiveProgramming / nth_permutation

Function nth_permutation

code/other/nth_permutation.cpp:1–8  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1vector<int> nth_permutation(int cnt, int n) {
2 vector<int> idx(cnt), per(cnt), fac(cnt);
3 rep(i,0,cnt) idx[i] = i;
4 rep(i,1,cnt+1) fac[i - 1] = n % i, n /= i;
5 for (int i = cnt - 1; i >= 0; i--)
6 per[cnt - i - 1] = idx[fac[i]],
7 idx.erase(idx.begin() + fac[i]);
8 return per; }
9// vim: cc=60 ts=2 sts=2 sw=2:

Callers 1

testFunction · 0.85

Calls 1

eraseMethod · 0.45

Tested by 1

testFunction · 0.68