一.题目链接:
Carny Magician
二.题目大意:
大小为 n 的排列,求其中有 m 个元素在下标对应位置,其他元素错排的字典序第 k 大的排列.
三.分析:
贴一个大佬链接~~
比赛时搞了半天也没推出来状态转移方程...
状态表示:
c[i][j]: 从 i 个元素中取 j 个的方案数
fac[i]: i 的阶乘
f[i]: 大小为 i 的排列的错排方案数
g[i][j]: 大小为 i 的排列,其中有 j 个元素不存在下标对应位置,的错排方案数
dp[i][j][k]: 大小为 i 的排列,其中有 j 个元素放在了其下标对应位置上,有 k 个元素不存在下标对应位置,的方案数
状态转移方程:
c[i][j] = c[i - 1][j - 1] + c[i - 1][j]
fac[i] = fac[i - 1] * i
f[i] = (f[i - 1] + f[i - 2]) * (i - 1)
g[i][j] = c[j][k] * c[i - j][k] * fac[k] * c[k][l] * c[i - j - k][k - l] * fac[j] * g[i - j - k][k - l]
dp[i][j][k] = c[i - k][j] * g[i - j][k]
唯一不好想的就是 g[i, j] 的状态转移方程,解释如下:
首先符号化解释:设 A 为当前不存在下标对应的元素集合,B 为当前存在下标对应的元素集合.
1. 从集合 A 中选择 k 个元素,从集合 B 中选择 k 个元素的相应位置,再把从集合 A 中选择的 k 个元素放到从集合 B 中选择的 k 个元素的对应位置上去,共有 c[j, k] * c[i - j, k] * fac[k] 选法.
2. 由于第一步集合 A 中的 k 个元素放到了集合 B 中 k 个元素的位置,那么就需要再从集合 B 中选择 k 个元素放到集合 A 中对应的位置(注意,不是集合 A 中元素对应的位置,因为集合 A 中的元素无对应位置),还有一点就是集合 B 中有 k 个元素的位置已经被集合 A 中的元素占据,所以集合 B 分为两部分:B1(对应位置已被占据),B2(对应位置未被占据).
不妨假设在 B1 中选择 l 个元素,那么在 B2 中选择 k - l 个元素,将这在集合 B 中总共选取的 k 个元素与集合 A 中未使用的 j - k 个元素放到集合 A 对应的位置并全排列,共有 c[k, l] * c[i - j - k, k - l] * fac[j].
3. 经过以上操作后,排列剩余 i - j - k 个元素和位置未被使用,其中有 k - l 个元素不存在其下标的对应位置,方案数为 g[i - j - k, k - l]
综上所得:
四.代码实现:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//大数
struct BigInteger
{
static const int BASE = 100000000; //和WIDTH保持一致
static const int WIDTH = 8; //八位一存储,如修改记得修改输出中的%08d
bool sign; //符号, 0表示负数
size_t length; //位数
vector<int> num; //反序存
//构造函数
BigInteger(long long x = 0) { *this = x; }
BigInteger(const string &x) { *this = x; }
BigInteger(const BigInteger &x) { *this = x; }
//剪掉前导0,并且求一下数的位数
void cutLeadingZero()
{
while (num.back() == 0 && num.size() != 1)
{
num.pop_back();
}
int tmp = num.back();
if (tmp == 0)
{
length = 1;
}
else
{
length = (num.size() - 1) * WIDTH;
while (tmp > 0)
{
length++;
tmp /= 10;
}
}
}
//赋值运算符
BigInteger &operator=(long long x)
{
num.clear();
if (x >= 0)
{
sign = true;
}
else
{
sign = false;
x = -x;
}
do
{
num.push_back(x % BASE);
x /= BASE;
} while (x > 0);
cutLeadingZero();
return *this;
}
BigInteger &operator=(const string &str)
{
num.clear();
sign = (str[0] != '-'); //设置符号
int x, len = (str.size() - 1 - (!sign)) / WIDTH + 1;
for (int i = 0; i < len; i++)
{
int End = str.size() - i * WIDTH;
int start = max((int)(!sign), End - WIDTH); //防止越界
sscanf(str.substr(start, End - start).c_str(), "%d", &x);
num.push_back(x);
}
cutLeadingZero();
return *this;
}
BigInteger &operator=(const BigInteger &tmp)
{
num = tmp.num;
sign = tmp.sign;
length = tmp.length;
return *this;
}
//绝对值
BigInteger abs() const
{
BigInteger ans(*this);
ans.sign = true;
return ans;
}
//正号
const BigInteger &operator+() const { return *this; }
//负号
BigInteger operator-() const
{
BigInteger ans(*this);
if (ans != 0)
ans.sign = !ans.sign;
return ans;
}
// + 运算符
BigInteger operator+(const BigInteger &b) const
{
if (!b.sign)
{
return *this - (-b);
}
if (!sign)
{
return b - (-*this);
}
BigInteger ans;
ans.num.clear();
for (int i = 0, g = 0;; i++)
{
if (g == 0 && i >= num.size() && i >= b.num.size())
break;
int x = g;
if (i < num.size())
x += num[i];
if (i < b.num.size())
x += b.num[i];
ans.num.push_back(x % BASE);
g = x / BASE;
}
ans.cutLeadingZero();
return ans;
}
// - 运算符
BigInteger operator-(const BigInteger &b) const
{
if (!b.sign)
{
return *this + (-b);
}
if (!sign)
{
return -((-*this) + b);
}
if (*this < b)
{
return -(b - *this);
}
BigInteger ans;
ans.num.clear();
for (int i = 0, g = 0;; i++)
{
if (g == 0 && i >= num.size() && i >= b.num.size())
break;
int x = g;
g = 0;
if (i < num.size())
x += num[i];
if (i < b.num.size())
x -= b.num[i];
if (x < 0)
{
x += BASE;
g = -1;
}
ans.num.push_back(x);
}
ans.cutLeadingZero();
return ans;
}
// * 运算符
BigInteger operator*(const BigInteger &b) const
{
int lena = num.size(), lenb = b.num.size();
BigInteger ans;
for (int i = 0; i < lena + lenb; i++)
ans.num.push_back(0);
for (int i = 0, g = 0; i < lena; i++)
{
g = 0;
for (int j = 0; j < lenb; j++)
{
long long x = ans.num[i + j];
x += (long long)num[i] * (long long)b.num[j];
ans.num[i + j] = x % BASE;
g = x / BASE;
ans.num[i + j + 1] += g;
}
}
ans.cutLeadingZero();
ans.sign = (ans.length == 1 && ans.num[0] == 0) || (sign == b.sign);
return ans;
}
//*10^n 大数除大数中用到
BigInteger e(size_t n) const
{
int tmp = n % WIDTH;
BigInteger ans;
ans.length = n + 1;
n /= WIDTH;
while (ans.num.size() <= n)
ans.num.push_back(0);
ans.num[n] = 1;
while (tmp--)
ans.num[n] *= 10;
return ans * (*this);
}
// /运算符 (大数除大数)
BigInteger operator/(const BigInteger &b) const
{
BigInteger aa((*this).abs());
BigInteger bb(b.abs());
if (aa < bb)
return 0;
char *str = new char[aa.length + 1];
memset(str, 0, sizeof(char) * (aa.length + 1));
BigInteger tmp;
int lena = aa.length, lenb = bb.length;
for (int i = 0; i <= lena - lenb; i++)
{
tmp = bb.e(lena - lenb - i);
while (aa >= tmp)
{
str[i]++;
aa = aa - tmp;
}
str[i] += '0';
}
BigInteger ans(str);
delete[] str;
ans.sign = (ans == 0 || sign == b.sign);
return ans;
}
// %运算符
BigInteger operator%(const BigInteger &b) const
{
return *this - *this / b * b;
}
// ++ 运算符
BigInteger &operator++()
{
*this = *this + 1;
return *this;
}
// -- 运算符
BigInteger &operator--()
{
*this = *this - 1;
return *this;
}
// += 运算符
BigInteger &operator+=(const BigInteger &b)
{
*this = *this + b;
return *this;
}
// -= 运算符
BigInteger &operator-=(const BigInteger &b)
{
*this = *this - b;
return *this;
}
// *=运算符
BigInteger &operator*=(const BigInteger &b)
{
*this = *this * b;
return *this;
}
// /= 运算符
BigInteger &operator/=(const BigInteger &b)
{
*this = *this / b;
return *this;
}
// %=运算符
BigInteger &operator%=(const BigInteger &b)
{
*this = *this % b;
return *this;
}
// < 运算符
bool operator<(const BigInteger &b) const
{
if (sign != b.sign) //正负,负正
{
return !sign;
}
else if (!sign && !b.sign) //负负
{
return -b < -*this;
}
//正正
if (num.size() != b.num.size())
return num.size() < b.num.size();
for (int i = num.size() - 1; i >= 0; i--)
if (num[i] != b.num[i])
return num[i] < b.num[i];
return false;
}
bool operator>(const BigInteger &b) const { return b < *this; } // > 运算符
bool operator<=(const BigInteger &b) const { return !(b < *this); } // <= 运算符
bool operator>=(const BigInteger &b) const { return !(*this < b); } // >= 运算符
bool operator!=(const BigInteger &b) const { return b < *this || *this < b; } // != 运算符
bool operator==(const BigInteger &b) const { return !(b < *this) && !(*this < b); } //==运算符
// 逻辑运算符
bool operator||(const BigInteger &b) const { return *this != 0 || b != 0; } // || 运算符
bool operator&&(const BigInteger &b) const { return *this != 0 && b != 0; } // && 运算符
bool operator!() { return (bool)(*this == 0); } // ! 运算符
//重载<<使得可以直接输出大数
friend ostream &operator<<(ostream &out, const BigInteger &x)
{
if (!x.sign)
out << '-';
out << x.num.back();
for (int i = x.num.size() - 2; i >= 0; i--)
{
char buf[10];
//如WIDTH和BASR有变化,此处要修改为%0(WIDTH)d
sprintf(buf, "%08d", x.num[i]);
for (int j = 0; j < strlen(buf); j++)
out << buf[j];
}
return out;
}
//重载>>使得可以直接输入大数
friend istream &operator>>(istream &in, BigInteger &x)
{
string str;
in >> str;
size_t len = str.size();
int start = 0;
if (str[0] == '-')
start = 1;
if (str[start] == '\0')
return in;
for (int i = start; i < len; i++)
{
if (str[i] < '0' || str[i] > '9')
return in;
}
x.sign = !start;
x = str.c_str();
return in;
}
};
BigInteger c[55][55];
BigInteger fac[55];
BigInteger f[55];
BigInteger g[55][55];
BigInteger dp[55][55][55];
bool vis[55];
int ans[55];
void init_c()
{
c[0][0] = 1;
for(int i = 1; i <= 50; ++i)
{
c[i][0] = 1;
for(int j = 1; j <= i; ++j) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
void init_fac()
{
fac[0] = 1; for(int i = 1; i <= 50; ++i) fac[i] = fac[i - 1] * i;
}
void init_f()
{
f[0] = 1; f[1] = 0;
for(int i = 2; i <= 50; ++i) f[i] = (f[i - 1] + f[i - 2]) * (i - 1);
}
void init_g()
{
for(int i = 0; i <= 50; ++i)
{
for(int j = 0; j <= i; ++j)
{
if(j == 0) {g[i][j] = f[i]; continue;}
for(int k = 0; k <= min(j, i - j); ++k)
{
for(int l = max(0, 2 * k + j - i); l <= k; ++l)
{
g[i][j] += c[j][k] * c[i - j][k] * fac[k] * c[k][l] * c[i - j - k][k - l] * fac[j] * g[i - j - k][k - l];
}
}
}
}
}
void init_dp()
{
for(int i = 0; i <= 50; ++i)
{
for(int j = 0; j <= i; ++j)
{
for(int k = 0; k <= i - j; ++k)
{
dp[i][j][k] = c[i - k][j] * g[i - j][k];
}
}
}
}
void init()
{
init_c();
init_fac();
init_f();
init_g();
init_dp();
}
void work()
{
int n, m; BigInteger k;
cin >> n >> m >> k;
int x = 0;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
if(vis[j]) continue;
if(i == j && !m) continue;
int tmp_m = m - (i == j);
int tmp_x = x + (i != j && !vis[i]) - (j < i);
if(k > dp[n - i][tmp_m][tmp_x]) k -= dp[n - i][tmp_m][tmp_x];
else
{
m = tmp_m;
x = tmp_x;
ans[i] = j;
vis[j] = 1;
break;
}
}
}
bool f = 1; for(int i = 1; i <= n; ++i) f &= !!ans[i];
if(f) for(int i = 1; i <= n; ++i) cout << ans[i] << (i == n ? '\n' : ' ');
else cout << -1 << endl;
}
int main()
{
// freopen("input.txt", "r", stdin);
init();
work();
cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
return 0;
}