hdu 6397 charactor encoding

Character Encoding

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


c++

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 n1. 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 n1 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 (1T400), the number of test cases.

Each test case includes a line of three integers n,m,k (1n,m105,0k105), 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
 
 
 
題意:
x1+x2+x3+...xm=k, 0<=xi<=n-1,已知n,m,k,求全部可能的狀況數。
 
解法:
若是這個題xi沒有上限,那麼就是一個把k個1(小球)放到m個盒子裏,容許空盒的情形,採用隔板法,先將每一個盒子裏先放上一個球,那麼如今共有m+k個球,造成了m+k-1個間隙,在這些間隙中選出m-1個間隙放上隔板,便可將這些小球(1)分紅m份,也就是讓這些1造成了m個數。
如今xi存在上限
假設咱們先從總和m+k中選出n個球放在一邊,而後把剩餘的m+k-n個球分到m個盒子中,接着再把預先選出的n個球放到這m個盒子中的任意一箇中,此時這個被選中的盒子裏的球數確定是大於等於n的,也就是說這種狀況 至少有1個盒子裏的球是超出限制的。
同理咱們選出2*n個球放一邊...這種狀況至少有2個盒子裏的球是超出限制的。
......
咱們最多選到(k/n)*n個球放一邊,此時至少有k/n個盒子超出限制。
由於每一個事件存在重疊的部分,因此用到 容斥原理
總的狀況數爲

注意要預處理階乘以及階乘的逆元git

AC代碼:ui

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int mod=998244353;
 5 const int maxn=2e5+10;
 6 ll  fac[maxn],invfac[maxn],inv[maxn];  //fac[i]表明i的階乘,invfac[i]表明i階乘的逆元
 7 void init()
 8 {
 9     inv[1]=1;
10     int i;
11     for(i=2;i<maxn;i++)
12     {
13         inv[i]=(mod-mod/i)*inv[mod%i]%mod;//求逆元
14     }
15     fac[0]=1;
16     invfac[0]=1;
17     for(i=1;i<maxn;i++)
18     {
19         fac[i]=fac[i-1]*i%mod;//求階乘
20         invfac[i]=invfac[i-1]*inv[i]%mod;//求階乘的逆元
21     }
22 }
23 ll C(int n,int m)//求組合數C(n,m)
24 {
25     if(n<0||m<0||m>n)return 0;
26     return fac[n]*invfac[m]%mod*invfac[n-m]%mod;
27 }
28 int main()
29 {
30     int t;
31     cin>>t;
32     init();
33     while(t--)
34     {
35         int n,m,k;
36         scanf("%d%d%d",&n,&m,&k);
37         ll ans=0;
38         for(int c=0;c<=k/n;c++)
39         {
40             if(c&1)
41             {
42                 ans=(ans-C(m,c)*C(k+m-c*n-1,m-1)%mod+mod)%mod;
43             }
44             else ans=(ans+C(m,c)*C(k+m-c*n-1,m-1)%mod)%mod;
45         }
46         printf("%I64d\n",ans);
47     }
48     return 0;
49 }
相關文章
相關標籤/搜索