牛客2018第六场C

链接:https://www.nowcoder.com/acm/contest/144/C
来源:牛客网
 

题目描述

Oak is given N empty and non-repeatable sets which are numbered from 1 to N.

Now Oak is going to do N operations. In the i-th operation, he will insert an integer x between 1 and M to every set indexed between i and N.

Oak wonders how many different results he can make after the N operations. Two results are different if and only if there exists a set in one result different from the set with the same index in another result.

Please help Oak calculate the answer. As the answer can be extremely large, output it modulo 998244353.

输入描述:

The input starts with one line containing exactly one integer T which is the number of test cases. (1 ≤ T ≤ 20)

Each test case contains one line with two integers N and M indicating the number of sets and the range of integers. (1 ≤ N ≤ 1018, 1 ≤ M ≤ 1018, )

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x is the test case number (starting from 1) and y is the number of different results modulo 998244353.

示例1

输入

复制

2
2 2
3 4

输出

复制

Case #1: 4
Case #2: 52

考虑枚举最后有 k 种颜色,那么有 种排列列它们的方法。由于每种操作对⼀一个后缀有影响,区分方案只要考虑第一个被影响的位置即可。n 个位置放 k 种球,每个位置可以放多个,由隔板法方案数为 。所以答案为ans = sigma(C(n-1, i-1)*A(m, i));这个题目我们需要求逆元,如果我们用费马小定理求的话,时间复杂度是o(nlogn),这个复杂度不能通过这个题目,我们可以通过递推的方法来求,递推式为 inv[i] = (M-M/i)*inv[M%i]%M; 求组合数我们也需要用到一个递推式, C(n, i) = (n-i+1)/k*C(n, i-1); 中间用一下逆元取模就可以了。

AC代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#define M 998244353
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1e6 +10;
typedef long long LL;
LL c[maxn], A[maxn], inv[maxn];
LL Pow(LL a, LL b)
{
    LL res = 1;
    while(b)
    {
        if(b&1)
        {
            res = res*a%M;
        }
        a = a*a%M;
        b >>= 2;
    }
    return res%M;
}
void init()
{
    inv[1] = 1;
    for(int i = 2; i <= maxn; i++)
    {
        inv[i] = (M-M/i)*inv[M%i]%M;
    }
}
int main()
{
    LL n, m;
    int T, cas = 0;
    ios::sync_with_stdio(false);
    scanf("%d", &T);
    init();
    while(T--)
    {
        scanf("%lld %lld", &n, &m);
        LL x = min(m, n);
        A[1] = m%M;
        for(LL i = 2; i <= x; i++)
        {
            A[i] = A[i-1]*((m-i+1)%M)%M;
        }
        c[0] = 1;
        n--;
        c[1] = n%M;
        for(LL i = 2; i <= x; i++)
        {
            c[i] = ((n-i+1)%M)*inv[i]%M*c[i-1]%M;
        }
        LL ans = 0;
        for(LL i = 1; i <= x; i++)
        {
            ans = ans+(c[i-1]*A[i])%M;
        }
        ans %= M;
        printf("Case #%d: %lld\n", ++cas, ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值