Character Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1431 Accepted Submission(s): 531
Problem Description
In computer science, a character is a letter, a digit, a punctuation mark or some other similar symbol. Since computers can only process numbers, number codes are used to represent characters, which is known as character encoding. A character encoding system establishes a bijection between the elements of an alphabet of a certain size n and integers from 0 to n−1. Some well known character encoding systems include American Standard Code for Information Interchange (ASCII), which has an alphabet size 128, and the extended ASCII, which has an alphabet size 256.
For example, in ASCII encoding system, the word wdy is encoded as [119, 100, 121], while jsw is encoded as [106, 115, 119]. It can be noticed that both 119+100+121=340 and 106+115+119=340, thus the sum of the encoded numbers of the two words are equal. In fact, there are in all 903 such words of length 3 in an encoding system of alphabet size 128 (in this example, ASCII). The problem is as follows: given an encoding system of alphabet size n where each character is encoded as a number between 0 and n−1 inclusive, how many different words of length m are there, such that the sum of the encoded numbers of all characters is equal to k?
Since the answer may be large, you only need to output it modulo 998244353.
Input
The first line of input is a single integer T (1≤T≤400), the number of test cases.
Each test case includes a line of three integers n,m,k (1≤n,m≤105,0≤k≤105), denoting the size of the alphabet of the encoding system, the length of the word, and the required sum of the encoded numbers of all characters, respectively.
It is guaranteed that the sum of n, the sum of m and the sum of k don't exceed 5×106, respectively.
Output
For each test case, display the answer modulo 998244353 in a single line.
Sample Input
4
2 3 3
2 3 4
3 3 3
128 3 340
Sample Output
1
0
7
903
Source
【思路】
题目相当于求对于0 <= xi <= n - 1,有多少组解满足x1 + x2 + ... + xm = k。
两个思路,一个是容斥原理,一个是生成函数,很好的经典题一定要记录一下!
容斥原理
对于(n - 1) * m < k的情况,很显然只有0种,我们给特判一下。如果没有上界n - 1,把k个物品放在m个盒子里,我们利用隔板法,可以得知放置方案数是C(k + m - 1, m - 1)的,因此我们用这个数减去那些某些盒子物品数大于等于n的放置方案数,就可以得到所求答案了。可能有1到min(k / n, m)个盒子是不满足条件的,在这里无法每一种单独求出,但我们可以求出至少1个盒子不满足条件的放置方案数、至少2个盒子……到至少min(k / n, m)个盒子是不满足条件的放置方案数,然后由容斥原理可得最终结果。求法:假设至少c个盒子放置了大于等于n的物品,那么它的方案数是C(m, c) * C(k + m - 1 - c * n, m - 1),枚举c来容斥。
生成函数
设生成函数g(x) = (1 + x + x^2 + ... + x^(n - 1))^m,那么我们要求的就是x^k的系数。
原式是一个等比数列求和的m次方,也就是:。
分子二项式展开,得:,
分母提上来变成,再二项式展开(其实也可以由泰勒展开),得:。
问题就转换成了求这个式子中x^k的系数。
我们枚举i,则j = k - n * i,再求一下系数和。上面表达式和容斥是一样的,是不是非常神奇?
【代码】
//******************************************************************************
// File Name: 1001.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年08月15日 星期三 13时26分30秒
//******************************************************************************
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5, MOD = 998244353;
int t, n, m, k;
ll fac[MAXN], facinv[MAXN];
void init()
{
fac[0] = fac[1] = 1; facinv[0] = facinv[1] = 1;
for (int i = 2; i < MAXN; i++) {
fac[i] = fac[i - 1] * i % MOD;
facinv[i] = (MOD - MOD / i) * facinv[MOD % i] % MOD;
}
for (int i = 2; i < MAXN; i++) facinv[i] = facinv[i] * facinv[i - 1] % MOD;
}
ll C(int n, int m)
{
return fac[n] * facinv[n - m] % MOD * facinv[m] % MOD;
}
int main()
{
init();
scanf("%d", &t);
while (t--) {
scanf("%d %d %d", &n, &m, &k);
if ((ll)(n - 1) * m < k) {
printf("0\n");
continue;
}
ll ans = C(k + m - 1, m - 1);
for (int i = 1, now = -1; i <= min(k / n, m); i++, now = -now)
ans = ((ans + (ll)now * C(m, i) * C(k + m - 1 - i * n, m - 1) % MOD) % MOD + MOD) % MOD;
printf("%lld\n", ans);
}
return 0;
}