hdu-1003 Character Encoding(容斥+组合数)

Character Encoding

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


 

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个数(可重复),使得这m个数的和为k,求共有几种选法;

//思路:如果我们要将k个相同的小球划分成m个数(假设k>=m),每个数都不为0,共有C(k-1,m)种情况

如果我们要将k个相同的小球划分成m个数,划分后可以出现数为0的情况,共有C(k+m-1,m)种情况,给划分后的每个数减一,即为划分后每个数的真实数值;

对于此题,共有ans=C(k+m-1,m),但此时,会出现有的数>=n,所以我们要将减掉  出现1数值>=n,2个数值>=n.....n个数值大于等于n的情况,即出现i(i>=1&&i<=min(k/n,m))个数>=n,f(i)=C(k+m-n*i-1,m)....但减去f(1)时,会多减了f(2),f(3)......,所以要加上f(2),但此时又多加了f(3)....如此往复,发现,当i为奇数时,减去f(i),i为偶数时,加上f(i);

求组合数的时候用逆元。

举个栗子:n=2,m=3,k=3;

1个数>=2:         2个数>=2:         3个数>=2:        
位置: 1 2 3     1 2 3     1 2 3  
1 2       1、2 2 2     1、2、3 2 2 2  
  2 2       2 2 2            
  2   2                      
  2 2 2   1、3 2   2            
            2 2 2            
2   2                        
  2 2     2、3   2 2            
    2 2     2 2 2            
  2 2 2                      
                             
3     2                      
    2 2                      
  2   2                      
  2 2 2                      
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const ll MAX=2e5+5;
const ll mod=998244353;
ll n,m,k;
ll f[MAX],inv[MAX];
ll C(ll n,ll m)
{
    return f[n]*inv[n-m]%mod*inv[m]%mod;
}
ll Qpow(ll a ,ll b)
{
    ll s=1;
    while(b)
    {
        if (b&1)s=s*a%mod;
        a=a*a%mod;
        b=b>>1;
    }
    return s%mod;
}
int main()
{
    int t;
    f[0]=inv[0]=1;
    for (int i=1;i<MAX;i++ )
    {
        f[i]=f[i-1]*i%mod;
        inv[i]=Qpow(f[i],mod-2);
    }
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%lld%lld%lld",&n,&m,&k);
            ll ans=C(k+m-1,m-1);
            ll len=min(k/n,m);
            for(int i=1;i<=len;i++)
            {
                if(i%2==1)ans=(ans-C(k+m-1-i*n,m-1)*C(m,i)%mod+mod)%mod;//ans-C(k+m-1-i*n,m-1)*C(m,i)%mod可能小于0,所以加一个mod;
                else ans=(ans+C(k+m-1-i*n,m-1)*C(m,i)%mod)%mod;
            }
            printf("%lld\n",ans%mod);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37378303/article/details/82054533
今日推荐