HDU 6397 Character Encoding [ 容斥原理 + 组合数 ]

14 篇文章 0 订阅
6 篇文章 0 订阅

题目链接:

2018 Multi-University Training Contest 8 Character Encoding HDU - 6397

题意概括:

m 个位置任意填入范围是 0 至 n-1 的整数,使各位数字的和为 k ——问总共有多少种情况。

数据范围:

1\leq T\leq 400 

1\leq n,m\leq 10^{5} 

0\leq k\leq 10^{5}

题解分析:

这里有一个关于经典组合数的知识点,dls也多次强调,很重要。

x_{1}+x_{2}+x_{3}+\cdots +x_{m}=k\, ,\, (x_{i}\geq 0)  的解总数是

C(k+m-1,m-1) 

这个知识点挺重要的,要记牢,具体可以用隔板法证明(好像课内会学到)。

这里可以先找出满足等式的所有解,再减去不满足束条件 x_{i}\leq n-1 的解

所以问题就转化成了求包含 x\geq n-1 解的总个数

因为经典组合数问题里,对 x 只有下限约束,无上限约束

所以当 x > n 时,令 x{}'=x-n ,对每个不满足条件的 x 都减去 n 。这样就满足 x 都大于 0 的约束条件,可以用组合数公式了

故当有 c 个 x > n 时,只需要在等式右边减去 c 个 n 便可:

x_{1}+x_{2}+x_{3}+\cdots +x_{m}=k-c*n\, ,\, (x_{i}\geq 0)

C(k-c*n+m-1,m-1)

其中 c 的取值应该是 1,2,3,\cdots,min(k/n,m)

这里需要求出 c 的所有取值后的解的集合,用容斥原理求并集便可

原来的容斥系数是奇数为加,偶数为减。但这里求的是反面的情况,还需要用总数减去。并且当 c = 0 时,求出的正好是总数。

所以可以直接把容斥系数改为 (-1)^{c} 便可。公式:

(-1)^{c}*C(m,c)*C(k-c*n+m-1,m-1)

c=0,1,2,3,\cdots,min(k/n,m)

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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值