题目大意
求序列 满足以下要求的方案数:
- 序列长度为 。
- 。
- ()。
- ()。
解题思路
整理可得:。
即 。
即 。
此时转化成了一个经典的组合模型: 的方案数为 。
简要解析
,这意味着顺序是强制固定的,只能从小到大排。
- 如果我从集合 中任意选出 个不同的数字(不考虑顺序),比如选出了 。
- 选出后,因为数字各不相同,必须把它们按从小到大排序,变成 ,这就唯一对应了题目中的一个合法序列。
- 反过来,题目中任何一个严格递增的序列,去掉角标后就是一个确定的子集。
因为“选数字”这个动作不考虑顺序,方案数就是组合数 。
所以从 中选出任意 个不重复元素按从大到小的顺序放入 即可。
即 。
AC_Code(阶乘 + 逆元)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
int qmi(int a, int b, int p)
{
int res = 1 % p;
while (b)
{
if (b & 1)
res = (LL)res * a % p;
a = (LL)a * a % p;
b >>= 1;
}
return res;
}
int fact(int x)
{
int res = 1;
for (int i = 1; i <= x; ++ i )
res = (LL)res * i % MOD;
return res;
}
int infact(int x)
{
return qmi(fact(x), MOD - 2, MOD);
}
int C(int a, int b)
{
if (a < b)
return 0;
return (LL)fact(a) * infact(b) % MOD * infact(a - b) % MOD;
}
int main()
{
int a, b;
cin >> a >> b;
cout << C(b - 1, a - 1) << endl;
return 0;
}AC_Code(Lucas)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
int qmi(LL a, LL b, int p)
{
int res = 1 % p;
while (b)
{
if (b & 1)
res = (LL)res * a % p;
a = (LL)a * a % p;
b >>= 1;
}
return res;
}
int C(LL a, LL b, int p)
{
if (b > a)
return 0;
if (a - b < b)
b = a - b;
int res = 1 % p;
for (int i = 1, j = a; i <= b; ++ i, -- j )
{
res = (LL)res * j % p;
res = (LL)res * qmi(i, p - 2, p) % p;
}
return res;
}
int lucas(LL a, LL b, int p)
{
if (a < p && b < p)
return C(a, b, p);
return (LL)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}
int main()
{
LL a, b;
cin >> a >> b;
cout << lucas(b - 1, a - 1, MOD) << endl;
return 0;
}