题目传送门

https://atcoder.jp/contests/arc096/tasks/arc096_c

题解

考虑容斥,问题转化为求至少有 \(i\) 个数出现不高于 \(1\) 次。

那么我们令这 \(i\) 个数被划分到 \(j\) 个集合中。但是由于限制是不多于一次,意味着可能存在一些数没有出现过。那么,我们计算的时候可以将这种情况看成新增一个数 \(0\),然后将这 \(i+1\) 个数划分到 \(j+1\) 个集合中,与 \(0\) 在同一个集合的表示没有出现过。于是将 \(i\) 个数中的一些数划分到 \(j\) 个集合的方案数为 \(\begin{Bmatrix} i + 1 \\ j + 1 \end{Bmatrix}\)。

然后考虑剩下来的 \(n - i\) 个数可以形成 \(2^{n-i}\) 个集合。我们可以枚举这些集合有没有出现,那么就是 \(2^{2^{n-i}}\)。最后剩下的 \(n-i\) 个数还可以往之前的 \(j\) 个集合里面贴,所以再乘上 \((2^{n-i})^j\)。

于是最后的答案为:

\[\sum_{i=0}^n (-1)^i \binom ni \sum_{j=0}^i \begin{Bmatrix} i + 1 \\ j + 1 \end{Bmatrix} \cdot 2^{2^{n-i}} \cdot (2^{n-i})^j
\]

下面是代码,由于乘方都可以被预处理,所以时间复杂度为 \(O(n^2)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I>
inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 3000 + 7; int n, P;
int S[N][N], C[N][N]; inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y, const int &P = ::P) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
} inline void ycl() {
S[0][0] = C[0][0] = 1;
for (int i = 1; i <= n + 1; ++i) {
C[i][0] = 1;
for (int j = 1; j <= i; ++j) S[i][j] = (S[i - 1][j - 1] + (ll)S[i - 1][j] * j) % P, C[i][j] = smod(C[i - 1][j - 1] + C[i - 1][j]);
}
} inline void work() {
ycl();
int ans = 0;
for (int i = 0; i <= n; ++i) {
int cnt = 0, ni22 = fpow(2, fpow(2, n - i, P - 1)), fn1 = fpow(2, n - i), fn = 1;
for (int j = 0; j <= i; ++j) sadd(cnt, (ll)S[i + 1][j + 1] * ni22 % P * fn % P), fn = (ll)fn * fn1 % P;
// dbg("i = %d, ni22 = %d, fn1 = %d, cnt = %d\n", i, ni22, fn1, cnt);
if (i & 1) sadd(ans, P - (ll)cnt * C[n][i] % P);
else sadd(ans, (ll)cnt * C[n][i] % P);
}
printf("%d\n", ans);
} inline void init() {
read(n), read(P);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

ARC096E Everything on It 容斥原理的更多相关文章

  1. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  2. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  3. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  4. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  5. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  6. HDU5838 Mountain(状压DP + 容斥原理)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...

  7. 【BZOJ-2669】局部极小值 状压DP + 容斥原理

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 561  Solved: 293[Submit][Status ...

  8. HDU 2204Eddy's爱好(容斥原理)

    Eddy's爱好 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  9. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

随机推荐

  1. 5G即将到来,你还会购买4G手机吗?

    科技在不断进步,通信技术也是如此,5G网络将于明年下半年开始测试部署,4G手机是否值得更换呢?三星上周发布了Galaxy Note 9智能手机,这也给消费者带来了一个难题:到底是现在花上1000美元将 ...

  2. 【CF1255A】Changing Volume【思维】

    题意:每次可以-5/-2/-1/+1/+2/+5,问是否存在方案使得A变成B 题解:首先我们可以设A<B,若A>B,则交换AB,因为A到B和B到A是互逆的过程,所以可以交换 其次将B-=A ...

  3. Oracle数据库字符集问题

    Oracle数据库的字符集问题,也涉及作为服务器操作系统的CentOS或者Windows的字符集与Oracle字符集之间的关联关系Oracle的字符集,这个问题的提出是因为两个原因:一是遇到一个DMP ...

  4. cordova+vue做的app解决引入cordova-plugin-splashscreen后启动先显示黑屏在显示启动页

    先上项目目录结构cordova项目结构 android platform 结构 图中用红框框起来的为主要修改文件 这篇主要的讲cordova项目引用了cordova-plugin-splashscre ...

  5. IP地址的分类及各类IP的最大网络数、网络号范围和最大主机数

    总结自谢希仁老师的<计算机网络>第五版 每一类网络地址都由两部分组成:网络号net-id+主机号host-id.IP地址的分类可以参看下图: 可以看到各个类别的区别,同时,所有的类别都是3 ...

  6. isset、empty 误区

    isset()  常用来判定变量是否存在  但是当变量赋值为NUll时候,返回值一样是FALSE  而实际上变量是存在的,与实际情况不符,判定错误. empty() 用来判定变量或者类的属性值是否为等 ...

  7. 使div弹窗可拖拽指令

    在项目开发过程中,有些情况dialog弹窗,直接使用div模拟弹窗效果,并需要支持div可拖拽. div模拟弹窗效果: (1)在用于存放指令的文件夹内,新建js文件,命名为:drag.js.具体代码如 ...

  8. loj#2391 「JOISC 2017 Day 1」港口设施

    分析 https://yhx-12243.github.io/OI-transit/records/uoj356%3Bloj2391%3Bac2534.html 代码 #include<bits ...

  9. p4899 [IOI2018] werewolf 狼人

    分析 我用的主席树维护qwq 代码 #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  10. grpc应用于微服务的分析,基于python

    grpc应用于微服务的分析 gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计,目前提供 C.Java 和 Go 语言版本,分别是:grpc, grpc-java, g ...