链接: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
比赛时一看这道题,我想总的情况是m的n次方,然后减掉重复的,就是题目所求的,我就开始找重复的,然后不是少找了就是找多了,还剩最后几分钟的时候我就想是不是思路错了
看了题解,果不其然,正着比较好想,取个m,n的最小值min,从1枚举到min,意思是n个位置放i个不同的数字,有A(m,i)种排列,对于每一种排列,必须确保这i个数字至少出现一次,可以重复出现,这就用到了隔板法,至于求排列组合不能预处理,只能递推着来求,做这种排列组合的题一定要有清晰的分类标准,才能做到不重不漏
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1000006;
const int MOD = 998244353;
ll inv[MAXN];
int main(void)
{
int T;
ll n,m;
scanf("%d",&T);
inv[1] = inv[0] = 1;
for(int i = 2;i < MAXN; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
int kase = 0;
while(T--) {
kase++;
scanf("%lld %lld",&n,&m);
int Min = min(n,m);
ll ans = 0;
ll preA = m % MOD;
ll preC = 1;
for(int i = 1; i <= Min; i++) {
ans = (ans + (preA * preC) % MOD) % MOD;
preA = (preA % MOD * ((m - i) % MOD)) % MOD;
preC = (preC * ((n - i) % MOD)) % MOD * inv[i] % MOD;
}
printf("Case #%d: %lld\n",kase,ans);
}
return 0;
}