题目链接:
2018 Multi-University Training Contest 8 Character Encoding HDU - 6397
题意概括:
m 个位置任意填入范围是 0 至 n-1 的整数,使各位数字的和为 k ——问总共有多少种情况。
数据范围:
题解分析:
这里有一个关于经典组合数的知识点,dls也多次强调,很重要。
的解总数是
这个知识点挺重要的,要记牢,具体可以用隔板法证明(好像课内会学到)。
这里可以先找出满足等式的所有解,再减去不满足束条件 的解
所以问题就转化成了求包含 解的总个数
因为经典组合数问题里,对 x 只有下限约束,无上限约束
所以当 x > n 时,令 ,对每个不满足条件的 x 都减去 n 。这样就满足 x 都大于 0 的约束条件,可以用组合数公式了
故当有 c 个 x > n 时,只需要在等式右边减去 c 个 n 便可:
其中 c 的取值应该是
这里需要求出 c 的所有取值后的解的集合,用容斥原理求并集便可
原来的容斥系数是奇数为加,偶数为减。但这里求的是反面的情况,还需要用总数减去。并且当 c = 0 时,求出的正好是总数。
所以可以直接把容斥系数改为 便可。公式:
AC代码:
#include <stdio.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 10;
const int MOD = 998244353;
int fac[MAXN], finv[MAXN];
ll mod_pow(ll x, ll n) {
if (n == 0) return 1;
ll res = mod_pow(x * x % MOD, n / 2);
if (n & 1) res = res * x % MOD;
return res;
}
void init() {
fac[0] = 1;
for(int i = 1; i < MAXN; i ++)
fac[i] = (ll)fac[i - 1] * i % MOD;
finv[MAXN - 1] = (int)mod_pow(fac[MAXN - 1], MOD - 2);
for(int i = MAXN - 1; i > 0; i --)
finv[i - 1] = (ll) finv[i] * i % MOD;
}
ll C(int n, int m) {
if(n < 0 || m < 0 || m > n) return 0;
return (ll)fac[n] * finv[m] % MOD * finv[n - m] % MOD;
}
int main() {
int T;
scanf("%d", &T);
init();
while (T --) {
int n, m, k;
ll ans = 0;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i * n <= k && i <= m; i ++) {
if (i % 2) ans = (ans - C(m, i) * C(k - i * n + m - 1, m - 1)% MOD + MOD) % MOD; // 奇数为减
else ans = (ans + C(m, i) * C(k - i * n + m -1, m - 1)% MOD + MOD) % MOD; // 偶数为加
}
printf("%lld\n", ans);
}
}
Character Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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