hdu6397 Character Encoding 隔板法+容斥原理+线性逆元方程
题意:给出n,m,k,用m个0到n-1的数字凑出k,问方案数,mod一个值。
题目思路:
首先如果去掉数字范围的限制,那么就是隔板法,先复习一下隔板法。
①k个相同的小球放入m个不同的盒子,每个盒子不为空的种类数:k-1个空隙中插入m-1个板子,C(k-1, m-1)
②k个相同的小球放入m个不同的盒子,可以允许有的盒子为空种类数:我们再加上m个球,按照①式不为空求解,因为分割完后,每个盒子减去1,就是当前问题的解,即:C(k-1+m, m-1);
而现在有了n这个限制,也就是说之前算出来的答案中有非法的情况,即至少有一个盒子里面有至少n个球,也就是说我需要减去这些非法的方案,当至少有一个盒子里面是至少n个球时,这样的方案数是C(m,1)*C(k-1+m-n,m-1)。(这个式子怎么理解呢,第一步,先从m个盒子中选出一个盒子,放上n个小球,确保这个方案非法,第二步,把剩下的球随机放入m个盒子中,并且有盒子可以不放球,这就是前后两个式子的含义)。但是!如果只是减去这个式子,那么会删的太多,为什么呢?
举个例子,如果我第一步往一号盒子放了n个球,然后随机放的时候往2号盒子放了n个球,这种方案和第一步往2号盒子里放n个球,然后随机放的时候往一号盒子里放n个球,情况是不是一样呢,也就是说,C(m,1)*C(k-1+m-n,m-1)这个式子包含了这两种相同的方案,那我如果减去这个是不是就重复减了呢?重复减怎么办呢,那就加上这个,而三个n的情况是不是又多余了呢,那就减去。于是就得到了一个容斥的式子。
最后的答案就是 C(k-1+m, m-1)+ (-1)^i * C(m,i)* C(k-1+m-i*n,m-1),i从1到m。
计算组合数的地方要用逆元,阶乘要预处理,而逆元的话用费马小定理加减枝似乎也可以过,但为了练习前几天学的线性逆元方程,就使用了这个来做。特别的是,如果inv[i]表示的是i的逆元,那么不可以直接用inv[A[i]]来表示i的阶乘的逆元,因为i的阶乘A[i]可能很大,会爆数组,所以要用jc[i]表示i的阶乘的逆元,jc[i]=jc[i-1]*inv[i]%mod;然后就可以啦。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define eps 1e-9
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=200010;
ll mod=998244353;
int T;
ll n,m,k;
ll A[maxn];
ll inv[maxn],jc[maxn];
void initinv(){
inv[1]=1;
for(int i=2;i<=200010;i++){
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
}
}
ll c(ll m1,ll n1){
if(n1<m1)return 0;
return A[n1]*jc[m1]%mod*jc[n1-m1]%mod;
}
int main(){
initinv();
A[0]=1;
jc[0]=1;
for(int i=1;i<=200000;i++){
A[i]=A[i-1]*i%mod;
jc[i]=jc[i-1]*inv[i]%mod;
}
cin>>T;
while(T--){
scanf("%lld%lld%lld",&n,&m,&k);
if(k==0){//这些是简单的减枝 没有也能过
printf("1\n");
continue;
}
if(k>m*(n-1)){
printf("0\n");
continue;
}
if(k<n){
ll ans=c(k,m+k-1);
printf("%lld\n",ans);
}else{
ll ans=0;
int flag=-1;
ans+=c(m-1,m+k-1);
for(int i=1;i<=m;i++){
ans+=flag*c(i,m)*c(m-1,m+k-i*n-1)%mod;
ans=(ans+mod)%mod;
flag*=-1;
}
printf("%lld\n",ans);
}
}
}
Character Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1774 Accepted Submission(s): 686
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
hdu6397 Character Encoding 隔板法+容斥原理+线性逆元方程的更多相关文章
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- hdu6397 Character Encoding 母函数解约束条件下多重集
http://acm.hdu.edu.cn/showproblem.php?pid=6397 原问题的本质是问m个元素的多重集S,每一种类型的对象至多出现n-1次的S的k组合的个数是多少? 等价于 x ...
- A - Character Encoding HDU - 6397 - 方程整数解-容斥原理
A - Character Encoding HDU - 6397 思路 : 隔板法就是在n个元素间的(n-1)个空中插入k-1个板,可以把n个元素分成k组的方法 普通隔板法 求方程 x+y+z=10 ...
- 逆元 组合A(n,m) C(n,m)递推 隔板法
求逆元 https://blog.csdn.net/baidu_35643793/article/details/75268911 int inv[N]; void init(){ inv[] = ; ...
- hdu 6397 Character Encoding (生成函数)
Problem Description In computer science, a character is a letter, a digit, a punctuation mark or som ...
- 多校 HDU 6397 Character Encoding (容斥)
题意:在0~n-1个数里选m个数和为k,数字可以重复选: 如果是在m个xi>0的情况下就相当于是将k个球分割成m块,那么很明显就是隔板法插空,不能为0的条件限制下一共k-1个位置可以选择插入隔板 ...
- 51Nod 1509 加长棒(隔板法)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1509 思路: 直接去解可行的方法有点麻烦,所以应该用总的方法去减去不可行 ...
- light oj 1102 - Problem Makes Problem组合数学(隔板法)
1102 - Problem Makes Problem As I am fond of making easier problems, I discovered a problem. Actuall ...
- 使用英文版eclipse保存代码,出现some characters cannot be mapped using "Cp1251" character encoding.
some characters cannot be mapped using "Cp1251" character encoding. 解决办法:方案一: eclipse-> ...
随机推荐
- react常见面试题
当你调用 setState 的时候,发生了什么事? 当调用 setState 时,React会做的第一件事情是将传递给 setState 的对象合并到组件的当前状态.这将启动一个称为和解(reconc ...
- python之数据库的操作(课前准备)
数据库(Database)是按照数据结构来组织.存储和管理数据的仓库. 上面的就是数据库的定义. 何为数据库,简单的来说,就是我们的大型数据的存放地点. 而我们学习的呢就是数据库的访问层的制作. 何为 ...
- GBK、GB2312和UTF-8编码区分
GBK包含全部中文字符, GBK的文字编码是双字节来表示的,即不论中.英文字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1. 至于UTF-8编码则是用以解决国际上字符的一种多字节编码,它对 ...
- NULL、0、nullptr
C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #defi ...
- Ubuntu重启关机命令使用
如果你想保持当前用户的登录而且使用root用户执行命令 可以输入:su root,输入密码 可以运行,shutdown 命令 重启命令: 1.reboot 2.shutdown -r now 立刻重启 ...
- spoj14846 Bribe the Prisoners
看来我还是太菜了,这么一道破题做了那么长时间...... 传送门 分析 我首先想到的是用状压dp来转移每一个人是否放走的状态,但是发现复杂度远远不够.于是我们考虑区间dp,dpij表示i到j区间的所有 ...
- JavaWeb_反射
一个类有多个组成部分,例如:成员变量,方法,构造方法等.反射就是加载类,并解剖出类的各个组成部分. 编程时什么情况下才需要加载类,并解剖出累的各个组成部分呢? 反射是用来做框架的. 从配置文件中解读类 ...
- SSH (Struts2+Spring3.0+Hibernate3)框架(一) 理论
典型的J2EE三层结构,分为表现层.中间层(业务逻辑层)和数据服务层.三层体系将业务规则.数据访问及合法性校验等工作放在中间层处理.客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与 ...
- 【Android学习】三种布局<include />、<merge />、<ViewStub />
1.布局重用<include /> <include />标签能够重用布局文件,使用起来非常简单 <include layout="@layout/layout ...
- SpringMVC 配置式开发-HandlerMapping的执行流程(八)
具体看这两块是怎么执行的 下图是实现了DispatcherServlet从HandleMapping获得处理器执行链的逻辑的源代码 下面是DispatcherServlet从HandleAdaptor ...