病毒扩散 - 牛客练习赛62(排列组合)

病毒扩散

https://ac.nowcoder.com/acm/contest/5205/B
https://ac.nowcoder.com/discuss/416603
病毒扩散牛客练习赛62
病毒扩散输入输出
备注

思路:
排列组合。完全想不到,这个问题可以这样转换,完全不明白啊……😭(tcl)
答案: C t x × C t − x y C_t^x \times C_{t-x}^{y} Ctx×Ctxy
或者像题解说的这样
病毒扩散B题解
看题解不懂但是求组合数还是会的,就当练习一下

715 m s / 198424 K B 715ms / 198424KB 715ms/198424KB ,预处理 C n m C_n^m Cnm

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5005;
const ll mod = 998244353LL;
ll c[N][N];
void init()
{
    for (int i = 0; i < N; i++)
        c[i][0] = 1LL;
    for (int i = 1; i < N; i++)
        for (int j = 1; j < N; j++)
            c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
int main()
{
#ifdef LOCAL_LIUZHIHAN
    freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
#endif
    int n, x, y, t;
    cin >> n;
    init();
    while (n--)
    {
        cin >> x >> y >> t;
        if (t < x + y)
            cout << 0 << endl;
        else
            cout << (c[t][x] % mod * c[t - x][y] % mod) << endl;
    }
    return 0;
}

228 m s / 1376 K B 228ms/1376KB 228ms/1376KB ,预处理 n ! n! n! 以及它的逆元

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353LL;
const int N = 5005;
ll fac[N], invfac[N];
ll qpow(ll a, ll b)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1)
            ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans % mod;
}
ll get_inv(ll a, ll p)
{
    return qpow(a, p - 2);
}
void init()
{
    fac[0] = invfac[0] = 1;
    for (int i = 1; i < N; i++)
    {
        fac[i] = fac[i - 1] * i % mod;
        invfac[i] = get_inv(fac[i], mod);
    }
}
int main()
{
#ifdef LOCAL_LIUZHIHAN
    freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
#endif
    init();
    int n, x, y, t;
    cin >> n;
    while (n--)
    {
        cin >> x >> y >> t;
        if (t < x + y)
            cout << 0 << endl;
        else
            cout << (fac[t] * invfac[x] % mod * invfac[y] % mod * invfac[t - x - y] % mod) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值