hdu6397 Character Encoding 多校第8场1001
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.
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.
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
排斥定理:sum( (-1)^c * C(m , c) * C(m-1+k-n*c , m-1) );
1 #include <bits/stdc++.h> 2 #define ll long long 3 #define INF 0x3f3f3f3f 4 using namespace std; 5 const ll mod = 998244353; 6 const int N = 2e5+10; 7 ll fac[N], inv[N]; 8 ll pow_mod(ll x, ll n){ 9 ll res=1; 10 while(n>0){ 11 if(n&1)res=res*x%mod; 12 x=x*x%mod; 13 n>>=1; 14 } 15 return res; 16 } 17 ll C(int a, int b) { 18 if(a < 0 || b < 0 || a < b) return 0; 19 return fac[a] * inv[b] % mod * inv[a-b] % mod; 20 } 21 int main() { 22 int mx = N; 23 fac[0] = 1;for(int i = 1; i <= mx; i ++) fac[i] = fac[i-1] * i %mod; 24 inv[mx] = pow_mod(fac[mx], mod-2); for(int i = mx-1; i >= 0; i --) inv[i] = inv[i+1] *(i+1) % mod; 25 int t, n, m, k; 26 cin >> t; 27 while(t--) { 28 cin >> n >> m >> k; 29 ll ans = 0; 30 for(int i = 0; i*n <= k; i ++) { 31 if(i&1) ans = (ans - C(m,i)*C(m-1+k-n*i, m-1)%mod+mod) % mod; 32 else ans = (ans + C(m,i)*C(m-1+k-n*i, m-1)%mod+mod) % mod; 33 } 34 cout << ans%mod << endl; 35 } 36 return 0; 37 }