19 组合数学 容斥 Lucas

Character Encoding

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

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种纸币,纸币的面值是0, 1,  2,3 ,4 ..........n-1,这n种

钱,我们选择其中的m张组成s的币值的方案数是多少;转换成上一道题目的思想就是我们有

s朵花,我们要把这n朵花放到m个花瓶中,每一个花瓶中最多放n-1朵,我们的方案数有多少

如果我们将每一个花瓶的不合法跟随便放分别用1 和 0 表示的话那么我们就用下面的过程:

花瓶     :       1      2      3       4       5

情况  0 :       0      0      0       0       0    没有不合法的全是随便放,C(  s+m-1, m - 1 );(加上);

情况  1 :       0      0     0       0       1     只用一个不合法其他全是随便放  C(s-(n-1)-1  + m  -   1  ,  m  -  1  ); (减掉)

情况  2 :       0      0      0       1       0    只用一个不合法其他全是随便放   C(s-(n - 1) - 1 + m  -    1  , m  -   1);(减掉)

情况  3 :       0      0      0       1       1    两个是不合法其他全是随便放   C(s-(n - 1)-1-(n-1)- 1 + m -1 ,m  -  1)(加上)

情况  4  : 

................................................................

情况1<<m-1:  1     1       1        1         1

还有就是我们在这列再提一下为什么是组合,(下边要除以相应的阶乘)因为在我们放板子的时候如果是在板子的空里面在放板子的话我们就是重复就算了,所以是组合不是连乘

可能大家都发现了因为有很多种 情况如果是挨着每一种情况都来进行枚举的话肯定会超时的,但是我们也会发现这道题目有一个

个跟上一道题目不同的地方就是这里的花瓶的最大容量是相同的换一句说就是我们对于所有的花瓶如果是只有一个不合法其他的

全是随便放,那么这一种状况就是会重复C( m  ,  1 )  次, 对于有两个花瓶是不合法的其他的全是随便放的话,会有C(m,2)

种情况,那么这样的话我们就是不用再每一个状态都跑了,就是一个简单的组合就可以了,还有一点就是,我把Lucas重新优化了一下,之前的时候一直TLE,向我们队的峰队请教了一波,然后秒a;

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,s,n) for(ll i=s;i<=n;i++)
#define per(i,n,s) for(ll i=n;i>=s;i--)
const ll mod  =  998244353;
const int Max = 2e5+10;
const int maxn= 2e5+10;
ll inv[Max],fac[Max];
ll qPow(ll base, int n) {ll res = 1; while(n) {if(n & 1) res = res * base % mod; base = base * base % mod; n >>= 1;} return res % mod;}
void init() {
    fac[0] = 1;
    rep(i, 1, maxn - 1) fac[i] = (ll)i * fac[i - 1] % mod;
    inv[1] = 1;
    inv[maxn - 1] = qPow(fac[maxn - 1], mod - 2);
    per(i, maxn - 2, 0) {
        inv[i] = inv[i + 1] * (i + 1) % mod;
    }
}
ll Lucas(ll n, ll r) {
    if(n - r < 0 || n < 0 || r < 0) return 0;
    return fac[n] * inv[n - r] % mod * inv[r] % mod;
}
int main(){
    int t;
    init();
    ll n,m,k;
    scanf("%d",&t);
    while(t--){
        scanf("%lld %lld %lld",&n,&m,&k);
        ll son=m-1;
        ll sum=0;
        for(ll i=0;i<=m;i++){
            ll base=k-i*n+m-1;
            if(base<0){
                continue;
            }
            ll ans=Lucas(base,son);
            ll xi=Lucas(m,i);
            ans=(ans*xi)%mod;
            ans=(ans%mod+mod)%mod;
            if(i%2==0){
                sum=(sum+ans)%mod;
            }
            else {
                sum=(sum-ans)%mod;
            }
            sum=(sum%mod+mod)%mod;
        }
        printf("%lld\n",(sum%mod+mod)%mod);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39792342/article/details/81949433