HDU 6397 Character Encoding 容斥 2018杭电多校第八场

5 篇文章 0 订阅
2 篇文章 0 订阅

Character Encoding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 510    Accepted Submission(s): 194

 

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

 

题意:很明确的题意, 就是要求0到n-1中选出m个数, 使其和恰好为k的方案数

 

思路:首先我们先不考虑n, 先考虑m个数, 组成的和为k的组合方式, 我们可以理解成隔板法放小球问题, 将k看作k个1然后将其分成m份, 可以为空, 这样我们就可以看作隔板法放小球了, 答案为C(k + m - 1, m - 1); 现在我们求出来的是不管n的大小的, 显然当k>=n的时候会出现某一份中超过n的组合, 我们可以找出是哪一份, 因为这m份每份都有可能是超出n的那一个, 所以是C(m, i), i表示超出n的份数, 这样我们只是找出了在份数固定的情况下哪几个份中可能出现大于n的情况, 但是组合情况还是没有算, 我们考虑之前的隔板放小球问题, 所以我们直接另k为k-i * n就可以了, 这样C(m, i) * C(k - i * n + m - 1, m - 1)就表示在有i份超出n的情况下的所有组合情况了, 然后我们就可以用容斥去解决了。

 

代码:

#include <iostream>
#include <cstdio>

#define MOD 998244353
const int maxed = 100000 + 10;
typedef unsigned long long ll;

ll A[maxed * 2];
int n, m, k;

int main()
{
    ll slove(ll w, int x);
    A[0] = 1;
    for (int i = 1; i < 2 * maxed; ++i)
        A[i] = A[i - 1] * i % MOD;
    int N;
    scanf("%d", &N);
    while (N--) {
        scanf("%d%d%d", &n, &m, &k);
        ll answer = A[k + m - 1] * slove(A[m - 1] * A[k] % MOD, MOD - 2) % MOD;
        //std::cout << "=====" << answer << std::endl;
        for (int i = 1; ; ++i) {
            if (1LL * i * n > k || i > m)
                break;
            if (i % 2)
                answer = (answer - A[m] * slove(A[i] * A[m - i] % MOD, MOD - 2) % MOD * A[k - i * n + m - 1] % MOD * slove(A[m - 1] * A[k - i * n] % MOD, MOD - 2) % MOD + MOD) % MOD;
            else
                answer = (answer + A[m] * slove(A[i] * A[m - i] % MOD, MOD - 2) % MOD * A[k - i * n + m - 1] % MOD * slove(A[m - 1] * A[k - i * n] % MOD, MOD - 2) % MOD) % MOD;
        }
        printf("%lld\n", answer);
    }
    return 0;
}

ll slove(ll w, int x)
{
    ll a = 1;
    while (x) {
        if (x & 1)
            a = a * w % MOD;
        x >>= 1;
        w = w * w % MOD;
    }
    return a;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值