链接: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
题意:
n个集合,m个数,每次往n-k(k=1。。。n)到n的集合中插入随机数(1.。。m),求有几种组合
分析:
排列组合问题,最后答案为
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N =1e6 + 5;
const int mod = 998244353;
ll inv[N];
void inti()//求逆元
{
inv[1] = 1;
for(int i = 2; i < N; i++)
{
inv[i] = 1ll*(mod - mod / i) * inv[mod%i] % mod;
}
}
int main()
{
inti();
int t;
scanf("%d", &t);
for(int ca = 1; ca <= t; ca++)
{
ll n, m;
scanf("%lld %lld", &n, &m);
ll M = min(n, m), ans = 0;
ll A = m % mod, C = 1;
for(int k = 1; k <= M; k++)
{
ans += A * C % mod;
ans %= mod;
A = (m - k) % mod * A % mod;
C = (n - k) % mod * C % mod * inv[k] % mod;
}
printf("Case #%d: %lld\n", ca, ans);
}
return 0;
}