hdu6397(组合+容斥原理)

Character Encoding

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

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

题意:

  共有m个取值范围为[0,n-1]的数字,求使得总和为k的方案数,即求

的整数解的组数

 

解析:

我们可以把k看成k个1,通过m-1个隔板来分割成m个数字。但是这样做会有问题,就是数字可能为0,但是隔板法不允许这种情况存在,所以我们可以做一个等价处理,即将取值范围+1,即[1,n],那么相应的总和也要加上m,即k+m,则问题转化为 “共有m个取值范围为[1,n]的数字,求使得总和为m+k的方案数”, 根据隔板法可以得出无限制的情况下方案数为 C(m+k-1, m-1)。

对于此题,共有ans=C(k+m-1,m),但此时,会出现有的数>=n,所以我们要将减掉  出现1数值>=n,(有C(m,1)种选法)2个数值>=n(有C(m,2)种选法).....(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) 

这道题的k,就相当于有k个小球,放到m个盒子里,如果不考虑Xi的约束条件,答案就是。我们考虑,这些答案里会有某些,我们应该把这些情况去掉。当有一个的时候,有种Xi。此时就相当于k个小球中,已经有n个小球放到了某个盒子里,接下来把剩下的k-n个小球放到m个盒子里就是有这么多种情况。所以我们用,这样的话,多减去了有两个的情况,我们又要加回来。考虑到这里,就不难发现,这可以用容斥原理解决。         最后得出:      

ans=C_{m+k-1}^{m-1}+\sum_{i=1}^{m}C_{m}^{i}*C_{m+k-i*n-1}^{m-1}*(-1)^{i}

#include<bits/stdc++.h>
using namespace std;

#define e exp(1)
#define pi acos(-1)
#define mod 998244353
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int maxn=2e5+5;
ll n,m,k;
ll f[maxn],inv[maxn];
ll qpow(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans%mod;
}
void init()
{
	f[0]=inv[0]=1;
	for(int i=1; i<maxn; i++)
	{
		f[i]=f[i-1]*i%mod;
		inv[i]=qpow(f[i],mod-2);
	}
}

ll C(ll n,ll m)
{
	return f[n]*inv[n-m]%mod*inv[m]%mod;
}
int main()
{
	init();
	int T;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&1)ans=(ans-C(m,i)*C(k+m-1-i*n,m-1)%mod+mod)%mod;
			else ans=(ans+C(m,i)*C(k+m-1-i*n,m-1)%mod)%mod;
		}
		printf("%lld\n",ans%mod);
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值