链接:https://www.nowcoder.com/acm/contest/144/C
来源:牛客网
Generation I
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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
隔板法相当于是把n个位置分为k组
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 5;
const int mod = 998244353;
LL n,m;
LL inv[N];
//线性处理逆元
void init()
{
inv[0] = 1;inv[1] = 1;
for(int i = 2;i < N;++i)
{
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
}
int main()
{
int t;
scanf("%d",&t);
init();
int p = 0;
while(t--)
{
scanf("%lld %lld",&n,&m);
p++;
LL x = min(n,m);
LL prem = m % mod;
LL prec = 1;
LL ans = prem * prec;
for(LL i = 2;i <= x;++i)
{
prem = prem * (m % mod - i + 1) % mod;
prec = prec * (n % mod - i + 1) % mod * inv[i - 1] % mod;
ans = (ans + prec * prem % mod) % mod;
}
printf("Case #%d: %lld\n",p,ans);
}
return 0;
}