跳到正文

题解

P6561 solution

TankYu约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年9月16日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟
题目
P6561

版本

  1. v1当前

可爱数数题。

可以把 2i+1,2i+22i + 1,2i+2 放一起看。

如果用 1 表示这一个数被选的话,那么对于上述的相邻元素只有 10,01,00 三种情况。

那么题目所要求的是 aa 个 10,bb 个 01,m−a−bm-a-b 个 00 能够拼成多少个不存在 01 在 10 前的串。

00 没啥性质,不能从它开始,我们先考虑放 01。

显然 01 和 00 的相对关系比较好想,等价于一共 m−a−b+bm-a-b+b 个中选 bb 个,即 (m−ab)\binom{m-a}{b}。

然后 10 只能放在 00 或 10 后,一共有 m−a−bm-a-b 个 00,这里由小学数学可以知道贡献是 (m−ba)\binom{m-b}{a}。

答案即 (m−ba)×(m−ab)\binom{m-b}{a} \times \binom{m-a}{b},预处理阶乘及其逆元即可。

C++
#include <iostream>
using namespace std;
#define LL long long
const LL mod = 998244353;
LL f[1000010], inv[1000010];
LL ksm(LL a, LL b)
{
    LL ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans *= a;
            ans %= mod;
        }
        a *= a;
        a %= mod;
        b >>= 1;
    }
    return ans;
}

LL C(LL n, LL m)
{
    return f[n] * inv[m] % mod * inv[n - m] % mod;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    f[0] = inv[0] = 1;
    for (int i = 1; i <= 1000000; i++)
    {
        f[i] = f[i - 1] * i % mod;
        inv[i] = ksm(f[i], mod - 2);
    }
    while (t--)
    {
        long long m, a, b;
        cin >> m >> a >> b;
        cout << C(m - a, b) * C(m - b, a) % mod << '\n';
    }
    return 0;
}
原文
存档评论1 条抓取于