[AtCoder Regular Contest 096 E] Everything on It 解题报告 (第二类斯特林数+容斥原理)
题目链接:https://arc096.contest.atcoder.jp/tasks/arc096_c
Time limit : 4sec / Memory limit : 512MB
Score : 900 points
Problem Statement
In "Takahashi-ya", a ramen restaurant, basically they have one menu: "ramen", but N kinds of toppings are also offered. When a customer orders a bowl of ramen, for each kind of topping, he/she can choose whether to put it on top of his/her ramen or not. There is no limit on the number of toppings, and it is allowed to have all kinds of toppings or no topping at all. That is, considering the combination of the toppings, 2N types of ramen can be ordered.
Akaki entered Takahashi-ya. She is thinking of ordering some bowls of ramen that satisfy both of the following two conditions:
- Do not order multiple bowls of ramen with the exactly same set of toppings.
- Each of the N kinds of toppings is on two or more bowls of ramen ordered.
You are given N and a prime number M. Find the number of the sets of bowls of ramen that satisfy these conditions, disregarding order, modulo M. Since she is in extreme hunger, ordering any number of bowls of ramen is fine.
Constraints
- 2≤N≤3000
- 10^8≤M≤10^9+9
- N is an integer.
- M is a prime number.
Subscores
- 600 points will be awarded for passing the test set satisfying N≤50.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of the sets of bowls of ramen that satisfy the conditions, disregarding order, modulo M.
Sample Input 1
2 1000000007
Sample Output 1
2
Let the two kinds of toppings be A and B. Four types of ramen can be ordered: "no toppings", "with A", "with B" and "with A, B". There are two sets of ramen that satisfy the conditions:
- The following three ramen: "with A", "with B", "with A, B".
- Four ramen, one for each type.
Sample Input 2
3 1000000009
Sample Output 2
118
Let the three kinds of toppings be A, B and C. In addition to the four types of ramen above, four more types of ramen can be ordered, where C is added to the above four. There are 118 sets of ramen that satisfy the conditions, and here are some of them:
- The following three ramen: "with A, B", "with A, C", "with B, C".
- The following five ramen: "no toppings", "with A", "with A, B", "with B, C", "with A, B, C".
- Eight ramen, one for each type.
Note that the set of the following three does not satisfy the condition: "'with A', 'with B', 'with A, B'", because C is not on any of them.
Sample Input 3
50 111111113
Sample Output 3
1456748
Remember to print the number of the sets modulo M. Note that these three sample inputs above are included in the test set for the partial score.
Sample Input 4
3000 123456791
Sample Output 4
16369789
题目大意:有 N 种调味剂, 现在要做一些拉面, 每碗拉面中可以放入任意种 类的调味剂, 但必须满足没有两碗拉面使用的调味剂集合相同, 且每种调味剂至少出现在两碗拉面中.
求方案数模一个质数.N ≤ 3000.
我们发现拉面是随便多少碗的,考虑容斥。定义调味剂不合法为调味剂只出现了1次或没有出现,ans=(0个调味剂不合法,其他任意) - (1个调味剂不合法,其他任意) + (2个调味剂不合法,其他任意)…………
写出来就是,注意不合法的调味剂我们还要乘上组合数
f(i)是指有i个调味剂不合法,其他调味剂任意的方案数.
考虑怎么计算f[i],f[i]=Σ(g[i][j]*2(n−i)j)*2(2^(n-i))
g[i][j]为在j碗面中有i种是不合法调味剂的方案数,然后剩下的n-i种调味剂可以随便放在这j碗面里,也可以放在j碗面之外(j<=i)
考虑放在j碗面里总共有 2(n-i) 种放的状态,一共j碗面,方案数就是2(n−i)j
考虑放在j碗面之外,同样有2(n-i)种放的状态,每一种状态都有可能出现或者没有,方案数就是2(2^(n-i))和上面那个之所以形式上不一样是因为这个不限制个数
预处理出组合数和g数组,g数组的递推式:g[i][j]=g[i-1][j-1]+g[i-1][j]*(j+1)(第二类斯特林数)。这样递推的原因是,当有i-1种坏酱在j-1碗面中时,第i种酱就必定在第j碗面中;或者i-1种酱在j碗面中,那第i种酱可以在任意j碗面中,或者压根就没加入任意j碗面中,所以是乘于j+1
值得注意的是,我们在计算2(2^(n-i)) 的时候,作为指数的(2^(n-i))取模并不是模上mod,而是模上mod-1,也就是mod的欧拉函数值(欧拉定理)
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std; const int maxn=3e3+;
ll n,mod;
ll g[maxn][maxn],c[maxn][maxn];
ll mul(ll a,ll b,ll p)
{
ll r=;
for (;b;b>>=,a=(a+a)%p) if (b&) r=(r+a)%p;
return r;
}
ll qpow(ll a,ll b,ll p)
{
ll r=;
for (;b;b>>=,a=mul(a,a,p)) if (b&) r=mul(r,a,p);
return r;
}
int main()
{
scanf("%lld%lld",&n,&mod);
for (int i=;i<=n;i++)
{
c[i][]=c[i][i]=;
for (int j=;j<i;j++)
c[i][j]=(c[i-][j]+c[i-][j-])%mod;
}
for (int i=;i<=n;i++)
{
g[i][]=;
for (int j=;j<=i;j++)
g[i][j]=(g[i-][j-]+g[i-][j]*(j+)%mod)%mod;
}
ll ans=;
for (int i=;i<=n;i++)
{
ll k=c[n][i];
if (i&) k=(mod-k)%mod;
ll x=qpow(,n-i,mod-);//欧拉定理,注意模数
x=qpow(,x,mod);
ll kind=qpow(,n-i,mod);
ll cnt=,y=;
for (int j=;j<=i;j++)
{
cnt=(cnt+(g[i][j]*y%mod))%mod;
y=kind*y%mod;
}
ans=(ans+(k*cnt%mod*x%mod))%mod;
}
printf("%lld\n",ans);
return ;
}
[AtCoder Regular Contest 096 E] Everything on It 解题报告 (第二类斯特林数+容斥原理)的更多相关文章
- AtCoder Regular Contest 096
AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...
- Atcoder Regular Contest 096 D - Sweet Alchemy(贪心+多重背包)
洛谷题面传送门 & Atcoder 题面传送门 由于再过 1h 就是 NOI 笔试了所以题解写得会略有点简略. 考虑差分,记 \(b_i=c_i-c_{fa_i}\),那么根据题意有 \(b_ ...
- Atcoder Regular Contest 096 C - Everything on It(组合数学)
Atcoder 题面传送门 & 洛谷题面传送门 简单题,由于这场 arc 的 F 是 jxd 作业而我不会做,所以只好来把这场的 E 水掉了. 我们记 \(f(i)\) 为钦定 \(i\) 个 ...
- AtCoder Regular Contest 096 D - Static Sushi(线性dp)
Problem Statement "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only o ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- AtCoder Regular Contest 092
AtCoder Regular Contest 092 C - 2D Plane 2N Points 题意: 二维平面上给了\(2N\)个点,其中\(N\)个是\(A\)类点,\(N\)个是\(B\) ...
- AtCoder Regular Contest 093
AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
随机推荐
- maven小知识点
Maven 使用惯例优于配置的原则 .它要求在没有定制之前,所有的项目都有如下的结构: 一个 maven 项目在默认情况下会产生 JAR 文件,另外 ,编译后 的 classes 会放在 basedi ...
- Ubuntu17.04安装WineQQ7.8及微信
安装qq2012成功,但是提示版本过低,qq登录失败. 安装WineQQ WineQQ7.8下载 安装依赖软件,方法来源网上 32位ubuntu:sudo apt install libgtk-3-0 ...
- vue2 router中的 @ 符号表示src
vue2 router中的 @ 符号表示src 学习了:https://segmentfault.com/q/1010000009549802 这个是webpack起的别名: 在build/webpa ...
- jms及active(jdk api)的实现
在企业中,分布式的消息队列需要实现的问题: 1.不同的业务系统分别处理同一个消息(订阅发布),同一个业务系统负载处理同一类消息(队列模式) 2.消息的一致性问题,在互联网公司中一般不要求强一致性,一般 ...
- Qt Quick 简单介绍
Qt Quick 是 Qt 提供的一种高级用户界面技术.使用它可轻松地为移动和嵌入式设备创建流畅的用户界面. 在 Android 设备上, Qt Quick 应用默认使用 OpenGL ES ,渲染效 ...
- [jzoj 4528] [GDOI2019模拟2019.3.26] 要换换名字 (最大权闭合子图)
题目链接: https://jzoj.net/senior/#contest/show/2683/0 题目: 题解: 不妨枚举一个点,让两颗树都以这个点为根,求联通块要么点数为$0$,要么包括根(即联 ...
- POJ 3268 Dijkstra+priority_queue或SPFA
思路:正向建边,一遍Dijkstra,反向建边,再一遍Dijkstra.ans加在一起输出最大值. (SPFA也行--) // by SiriusRen #include <queue> ...
- MySQL表不能修改、删除等操作,卡死、锁死情况的处理办法。
MySQL如果频繁的修改一个表的数据,那么这么表会被锁死.造成假死现象. 比如用Navicat等连接工具操作,Navicat会直接未响应,只能强制关闭软件,但是重启后依然无效. 解决办法: 首先执行: ...
- (转载)Android引导界面实现
Android引导界面实现 Android 2014-07-10 14:47:36 发布 您的评价: 4.0 收藏 3收藏 ViewPager类提供了多界面切换的新效果,是 ...
- 新疆大学OJ(ACM) 1099: 数列有序!
1099: 数列有序! 时间限制: 1 Sec 内存限制: 128 MB 题目描述 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的 ...