Character Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 898 Accepted Submission(s): 349
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
2018 Multi-University Training Contest 8
题意:输入 n m k,表示可以选择 m 个 【 0 , n-1 】 的数字和为 k,问有多少种方案
解法:
这题的容斥公式为
C( n + m - 1 ,m - 1 )* C( m ,0 ) - C( n + m - 1 - n ,m - 1 )* C( m ,1 ) +
C( n + m - 1 - 2 * n ,m - 1 )* C( m ,2 )- C( n + m - 1 - 3 * n ,m - 1 )* C( m ,3 ) ...
直到 n + m - 1 - i * n <= 0
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
const ll mod = 998244353;
const int maxn = 2 * (1e5 + 5);
ll inv[maxn], fac[maxn];
ll Pow(ll a,ll b) {
ll ans = 1;
while(b) {
if(b & 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll Comb(ll n,ll m) { //求 C(n,m) = n! / ((n - m)! * m!)
if(n < 0 || m < 0 || n < m){
return 0;
}
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
void init() {
// 预处理阶乘
fac[0] = fac[1] = 1;
for(ll i = 2; i < maxn; i++) {
fac[i] = i * fac[i - 1] % mod;
}
//求阶乘的逆元
inv[maxn - 1] = Pow(fac[maxn - 1],mod - 2);
for(ll i = maxn - 2; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
int main() {
init();
int T;
ll n,m,k;
scanf("%d",&T);
while(T--) {
scanf("%lld %lld %lld",&n,&m,&k);
ll M = m - 1;
ll K = k + M;
ll ans = 0;
for(ll i = 0; i * n <= k; i++){
if(i & 1){
ans = (ans - Comb(K,M) * Comb(m,i) % mod + mod) % mod;
}else{
ans = (ans + Comb(K,M) * Comb(m,i) % mod) % mod;
}
K -= n;
}
printf("%lld\n",ans);
}
return 0;
}

本文探讨了计算机科学中字符编码的概念,重点介绍了ASCII编码,并通过一个具体的数学问题深入讲解了如何计算特定条件下不同长度字符串的可能数量。文章还提供了一段C++代码,展示了如何使用组合数学原理和容斥原理解决这一问题。
1238

被折叠的 条评论
为什么被折叠?



