[容斥原理] zoj 3556 How Many Sets I
2015-06-10 21:30
435 查看
主题链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?
problemId=4535
How Many Sets I
Time Limit: 2 Seconds Memory Limit: 65536 KB
Give a set S, |S| = n, then how many ordered set group (S1, S2, ..., Sk) satisfies S1 ∩ S2 ∩ ... ∩ Sk =
∅. (Si is a subset of S, (1 <= i <= k))
the file.
Author: QU, Zhe
Contest: ZOJ Monthly, October 2011
Submit Status
题目意思:
已知|S|=n。给定k,求S1 ∩ S2 ∩
... ∩ Sk = ∅,当中Si是S的子集(i<=k)的种数。
n,k<=2^31-1
解题思路:
容斥原理
反向考虑。如果S1 ∩ S2 ∩
... ∩ Sk 不等于 ∅。则至少存在一个元素S1。S2。...,Sk都包括。
枚举都包括的元素.总的种数为(2^n)^k=2^(nk)
假设至少都包括一个元素,则种数为C(n,1)*(2^(n-1))^k=C(n,1)*2^((n-1)k)
假设至少都包括两个元素,则种数为C(n,2)*(2^(n-2))^k=C(n,2)*2^((n-2)k)
假设至少都包括i个元素,则种数为C(n,i)*(2^(n-i))^k=C(n,i)*2^((n-i)k)
减去包括一个的加上包括两个的减去包括3个的,如此类推。能够得出一下公式:
2^(nk)+C(n,1)*2^((n-1)k)-C(n,2)*2^((n-2)k)+...(-1)^i*C(n,i)*2^((n-i)k)+.....=(2^k-1)^n
(通过二项式公式)
所以答案转化为求(2^k-1)^n了,直接高速幂就可以。
代码:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?
problemId=4535
How Many Sets I
Time Limit: 2 Seconds Memory Limit: 65536 KB
Give a set S, |S| = n, then how many ordered set group (S1, S2, ..., Sk) satisfies S1 ∩ S2 ∩ ... ∩ Sk =
∅. (Si is a subset of S, (1 <= i <= k))
Input
The input contains multiple cases, each case have 2 integers in one line represent n and k(1 <= k <= n <= 231-1), proceed to the end ofthe file.
Output
Output the total number mod 1000000007.Sample Input
1 1 2 2
Sample Output
1 9
Author: QU, Zhe
Contest: ZOJ Monthly, October 2011
Submit Status
题目意思:
已知|S|=n。给定k,求S1 ∩ S2 ∩
... ∩ Sk = ∅,当中Si是S的子集(i<=k)的种数。
n,k<=2^31-1
解题思路:
容斥原理
反向考虑。如果S1 ∩ S2 ∩
... ∩ Sk 不等于 ∅。则至少存在一个元素S1。S2。...,Sk都包括。
枚举都包括的元素.总的种数为(2^n)^k=2^(nk)
假设至少都包括一个元素,则种数为C(n,1)*(2^(n-1))^k=C(n,1)*2^((n-1)k)
假设至少都包括两个元素,则种数为C(n,2)*(2^(n-2))^k=C(n,2)*2^((n-2)k)
假设至少都包括i个元素,则种数为C(n,i)*(2^(n-i))^k=C(n,i)*2^((n-i)k)
减去包括一个的加上包括两个的减去包括3个的,如此类推。能够得出一下公式:
2^(nk)+C(n,1)*2^((n-1)k)-C(n,2)*2^((n-2)k)+...(-1)^i*C(n,i)*2^((n-i)k)+.....=(2^k-1)^n
(通过二项式公式)
所以答案转化为求(2^k-1)^n了,直接高速幂就可以。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; LL n,k; LL quick(LL a,LL b) { LL res=1; while(b) { if(b&1) res=(res*a)%M; b>>=1; a=a*a%M; } return res; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%lld%lld",&n,&k)) { LL ans=(quick(2,k)-1+M)%M; ans=quick(ans,n); printf("%lld\n",ans); } return 0; }
相关文章推荐
- Hadoop日志以及日志的格式和命名组成
- 【PHP学习】PHP 简介
- 清除缓存功能的实现代码
- C++常见错误总结(不定时更新版~)
- 实现一个通讯录并将信息保存到文本中
- Adobe Flash Player ActiveX 不能安装的解决方法
- 【MATLAB 从零到进阶】day2 矩阵 数组
- WEBSHELL跳板REDUH使用说明
- uitableview持续练习
- 关于druid数据连接池抛出的 connection holder is null 异常
- Linux(CentOS)SSH无密码验证登陆
- Action的执行
- 在手机上使用Win7系统-桌面虚拟化单机POC环境(一)
- construct
- hadoop的编程实例
- 与和或的优先级问题
- 移动端使用的WebKit私有属性(转)
- IOS 开发,调用打电话,发短信,打开网址
- Subsets
- 如何通过反编译工具与插件 查看java *.class 文件源码