1. 程式人生 > >航電多校第八場-A-Character Encoding

航電多校第八場-A-Character Encoding

form odin for in res sim class 假設 ascii mes

題目描述

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 then 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.

輸入

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.

輸出

For each test case, display the answer modulo 998244353 in a single line.

樣例輸入

4
2 3 3
2 3 4
3 3 3
128 3 340

樣例輸出

1
0
7
903

題意是從0-n-1這n個數中選m個,和為k的方案數,可以重復選擇
也就是將k分成m份,每份為0-n-1
也就是將k+m分成m份,每份為1-n
假設沒有上限n,答案就是隔板法 C(k+m-1,m-1)
這裏面肯定計算了有大於n的情況
假設我們從這k+m裏面拿出一個n,然後將剩下的分成非空的m份,再把這個n加到其中的一份當中,那麽至少有一份是大於n的,同樣的道理,如果拿出c個n,那麽至少有c份是大於n的,因為這裏是至少,而不是一定有c份大於n,這其中有包含關系,所以需要容斥一下
技術分享圖片
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int p=998244353;
const int N=1e6+10;
int T,n,m,k;
ll fac[N],inv[N];
ll poww(ll x,int y)
{
    ll ret=1;
    while(y)
    {
        if (y&1) ret=ret*x%p;
        x=x*x%p;
        y>>=1;
    }
    return ret;
}
void pre()
{
    fac[0]=1;
    for (int i=1;i<N;i++) fac[i]=fac[i-1]*i%p;
    inv[N-1]=poww(fac[N-1],p-2);
    for (int i=N-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%p;
}
ll C(int a,int b)
{
    return fac[a]*inv[b]%p*inv[a-b]%p;
}
ll solve(int n,int m,int k)
{
    if(1ll*(n-1)*m<k)  return 0;
     
    ll ans=C(k+m-1,m-1);
    int nn=k/n;
    for (int i=1;i<=nn;i++)
    {
        ll tmp=C(k-i*n+m-1,m-1)*C(m,i)%p;

        if (i&1) ans=(ans-tmp+p)%p;
        else ans=(ans+tmp)%p;
    }
    return ans;
}
int main()
{
    pre();
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        printf("%lld\n",solve(n,m,k));
    }
    return 0;
}
View Code

航電多校第八場-A-Character Encoding