HDU 6397 Character Encoding 容斥原理

Character Encoding

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


 

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

一道容斥原理的好题,但是当时没有A出来,心情不好了。。


题意:

     我就简略的说吧,有k个相同的小球,有m个不同的箱子,每个箱子里装的球最少0个,最多n-1个。问有多少种不同的放法。

做法:

     我们知道,如果每个箱子里可以装的小球数量没有上限的话,答案就是C_{k+m-1}^{m-1},为什么请转向度娘。但是这道题有上限,那我们就要减去所有不合法的情况,设S_{i}为有至少有i个箱子不合法的情况(最多会有k/n个箱子不合法),那我们的答案就是

    C_{k+m-1}^{m-1}+\sum_{i=1}^{k/n}(-1)^{i}*S_{i}S_{i}=C_{m}^{i}*C_{n-i*k+m-1}^{m-1},然后就是板子套套的事情,居然就没几行代码的事情。。心塞了


代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int maxn=200005;
ll fac[maxn],inv_fac[maxn],n,m,k;

ll quick(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b/=2;
    }
    return ans;
}
void init(){
    inv_fac[0]=fac[0]=1,fac[1]=inv_fac[1]=1;
    for(int i=2;i<maxn;i++)
        fac[i]=fac[i-1]*i%mod;
    inv_fac[maxn-1]=quick(fac[maxn-1],mod-2);
    for(int i=maxn-2;i>=0;i--)
        inv_fac[i]=inv_fac[i+1]*(i+1)%mod;
}

ll C(ll n,ll m){
    if(n<0||m<0||m>n) return 0;
    if(n==m||m==0) return 1;
    return fac[n]*inv_fac[n-m]%mod*inv_fac[m]%mod;
}
int main(){
    init();
    int t;
    cin>>t;
    while(t--){
        scanf("%lld%lld%lld",&k,&m,&n);
        if((k-1)*m<n){
            printf("0\n");
            continue;
        }
        else if((k-1)*m==n){
            printf("1\n");
            continue;
        }
        ll ans=C(n+m-1,m-1)%mod;
        int up=n/k,tmp=-1;
        for(int i=1;i<=up;i++){
            ans=(ans+tmp*C(m,i)*C(n-i*k+m-1,m-1)%mod+mod)%mod;
            tmp=-tmp;
        }
        printf("%lld\n",ans%mod);
    }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81710624