Character Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 932 Accepted Submission(s): 358
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
题意:
给你N,M,K三个数字,询问你在0~n-1中,选M位数字和为K,一共有多少种方法。
题解:
看了讲解,学习了一波生成函数做法。
我们先不看k,只看n和m
当n=2时
0 1 2 3 4 5 6 7 8 9 10 11... 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 6 1 6 15 20 15 6 1 7 1 7 21 35 35 21 7 1 8 1 8 28 56 70 56 28 8 1 9 1 9 36 84 126 126 84 36 9 1 10 1 10 45 120 210 252 210 120 45 10 1 11 1 11 55 165 330 462 462 330 165 55 11 1 |
很显然就是杨辉三角,直接二项式定理就可以了
当n=3是,也是类似的
0 1 2 3 4 5 6 7 8 9 10 11 12
0: 1
1: 1 1 1
2: 1 2 3 2 1
3: 1 3 6 7 6 3 1
4: 1 4 10 16 19 16 10 4 1
5: 1 5 15 30 45 51 45 30 15 5 1
6: 1 6 21 50 90 126 141 126 90 50 21 6 1
生成公式:
这个公式就是就可以表示第m行的结果,对应的系数就是展开式中x^i的系数,所以我们只需要求生成公式中x^k的系数即可。
那么我们就得对公式化简
根据泰勒展开:
可得
所以
到了这一步,我们就可以直接计算了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 2e5+5;
ll f[maxn],ff[maxn],a[maxn],b[maxn];
int n,m,k;
//ll pow(ll a,ll b){
// ll sum = 1;
// while(b){
// if(b&1){
// sum = (sum * a) % mod;
// }
// a = (a*a)%mod;
// b >>= 1;
// }
// return sum;
//}
ll inv(ll a,ll b) {
if(a == 1)return 1;
return inv(b%a,b)*(b-b/a)%b;
}
ll inline bit(int n,int m) {
return f[n]*ff[m]%mod*ff[n-m]%mod;
}
int main() {
f[0] =1;
ff[0] = 1;
for(int i = 1; i <=200000; i++) {
f[i] = f[i-1]*i%mod;
ff[i]=inv(f[i],mod);
//printf("%d: %d %d\n",i,f[i],ff[i]);
}
int _;
for(cin>>_; _; _--) {
scanf("%d %d %d",&n,&m,&k);
if(k == 0) {
printf("1\n");
continue;
}
if((ll)(n-1)*m<k) {
printf("0\n");
continue;
}
ll ans = 0;
for(int i = 0; i<=k/n; i++) {
ll x = bit(m,i)*bit(m+k-i*n-1,m-1)%mod;
if(i%2==0) {
ans = (ans+x)%mod;
} else {
ans=(ans-x+mod)%mod;
}
}
printf("%lld\n",ans);
}
}