hdu 6397 Character Encoding

hdu 6397 Character Encoding

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

 

Source
 

題意:

  共有m個取值範圍爲[0,n-1]的數字,求使得總和爲k的方案數,即求php

的整數解的組數c++

 

 

思路:

     咱們能夠把k當作k個1,經過m-1個隔板來分割成m個數字。可是這樣作會有問題,就是數字可能爲0,可是隔板法不容許這種狀況存在,因此咱們能夠作一個等價處理,即將取值範圍+1,即[1,n],那麼相應的總和也要加上m,即k+m,則問題轉化爲 「共有m個取值範圍爲[1,n]的數字,求使得總和爲m+k的方案數」, 根據隔板法能夠得出無限制的狀況下方案數爲 C(m+k-1, m-1)。git

    而題目的限制是 Xi<=n,假設咱們先從總和(m+k)取出n,而後把剩下的部分(m+k-n)分紅m份,那麼此時再把預先取出的n放到這m份中的任意一份中,都會使得它大於n,即超出了限制,因此此次種狀況至少會使得一個數超出限制。那假設咱們預先取出了2*n呢?同理,這種狀況至少會使得兩個數超出限制,以此類推直到c不知足c*n<=k的條件。那麼根據容斥原理,答案就是:ui

 

注意要預處理出階乘,以及階乘的逆元,這樣就能夠O(1)獲得組合數,否則會超時this

代碼:

 

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;

typedef long long ll;
const int MAXN = 2e5+40;
const ll mod = 998244353;
ll fac[MAXN],ifac[MAXN],inv[MAXN];


void init()
{
    int n = MAXN;
    inv[1]=1;
    for(int i=2;i<n;i++)///預處理逆元
    {
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    }
    fac[0]=1;
    ifac[0]=1;
    for(int i=1;i<n;i++)///預處理階乘和階乘的逆
    {
        fac[i]=fac[i-1]*i%mod;
        ifac[i]=ifac[i-1]*inv[i]%mod;
    }

}


ll C(int n,int m )
{
    if(n<0||m<0||n<m) return 0;              ///沒有這句致使 wa了
    return fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}

int main()
{

    int t,n,m,k;
    init();
    scanf("%d", &t);
    while(t--){
        scanf("%d%d%d", &n, &m, &k);
        ll  ans = 0;
        for(int c=0;c*n<=k;c++){
            if(c&1){
                ans = (ans-C(m,c)*C(m-1+k-c*n,m-1)%mod+mod)%mod;
            }else{
                ans = (ans+C(m,c)*C(m-1+k-c*n,m-1)%mod)%mod;
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}
相關文章
相關標籤/搜索