组合数应用(容斥原理)(球盒问题)hdu6397(暑期多校)

                                     Character Encoding

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


 

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

 题解:

求用0~n-1的这n个数字选m个加起来和为k,选法有多少种,(3,1,0)和(3,0,1)是不同种情况,这是经典的球盒问题,球相同盒不同可以为空,隔板法,如果没有0~n-1的限制(每个盒可以装任意个最多k个)那么情况数为C(k+m-1,m-1);

合法的情况 x1+x2+...+xm=k

可能有一个不合法两个不合法。。。最多k/n个不合法我们利用容斥原理奇加偶减,选一个不合法的(+C(m,1))

两个不合法的(-C(m,2))

不合法情况:x1'+x2'+...+xm'=k-c*n(c从1到k/n)(相当于先在c个盒子放n个球,这样剩下的球怎么放都至少有一个盒是不合法的,这样情况会重复所以用到容斥原理奇加偶减)

总公式为:

C(k+m-1,m-1)-∑(-1)^(i+1) (  C(m,i)*C(k-i*n+m-1,m-1)  )(i从1到k/n)

代码:

#include 
#include 
#define maxn 200005
typedef long long ll;
using namespace std;
const ll mod=998244353;
ll fac[maxn],inv[maxn];
ll pow_mod(ll a,ll n)
{
    ll ret =1;
    while(n)
    {
        if(n&1) ret=ret*a%mod;
          a=a*a%mod;
          n>>=1;
    }
    return ret;
}
void init()//这里的阶乘必须预处理,否则超时
{
    fac[0]=1;
    for(int i=1;i

                    

你可能感兴趣的:(数学)