How Many Sets
来源:互联网 发布:atom php常用插件 编辑:程序博客网 时间:2024/05/20 14:15
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 of the file.
Output
Output the total number mod 1000000007.
Sample Input
1 1
2 2
Sample Output
1
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 of the file.
Output
Output the total number mod 1000000007.
Sample Input
1 1
2 2
Sample Output
1
9
题意:
容斥原理+快速幂
这个题目输出一开始用%I64d输出PE了两发,然后改成%lld就过了,跟一血就差30秒
个数为n的集合的子集有2^n个,从中选出K个使得他们的交集为空的个数。
由于集合可以重复被选,所以总的数目是2^(kn)
然后选中的集合都包含x这个数的数目是c(n,1)*2^(n-1)k
选中的集合包含x1,x2的数目是c(n,2)*2^(n-2)k
……
所以满足的集合的个数res=2^kn-c(n,1)*2^(n-1)k+c(n,2)*2(n-2)k-……
推出的公式为(2^k-1)^n
注意 容斥原理原理:
两个集合的容斥关系公式:A∪B =|A∪B| = |A|+|B| - |A∩B |(∩:重合的部分)
三个集合的容斥关系公式:|A∪B∪C| = |A|+|B|+|C| - |A∩B| - |B∩C| - |C∩A| + |A∩B∩C|
代码:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;const ll N=1000000007;ll pow(ll x,ll n){ ll b=1; while(n) { if(n&1) b=b*x%N; x=x*x%N; n>>=1; } return b;}int main(){ ll k,n; while(scanf("%lld%lld",&n,&k)!=EOF) { ll ans=pow(2,k)-1; ans=pow(ans,n); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- How Many Sets
- How Many Sets I
- How Many Sets I
- How Many Sets
- Sicily 4428. How Many Sets
- Sicily 4428 How Many Sets
- zoj3556 How Many Sets I-------容斥
- [2014Contest_1I]How Many Sets II
- zoj How Many Sets I(组合计数)
- ZOJ 3557 How Many Sets II
- zju 3557 How Many Sets II(Lucas 定理)
- ZOJ 3556 How Many Sets I 解题报告(数论)
- [容斥原理] zoj 3556 How Many Sets I
- ZOJ 3557 How Many Sets II lucas 定理
- ZOJ 3556 How Many Sets I 二项式+容斥
- ZOJ 3556 How Many Sets I (容斥)
- ZOJ3556 How Many Sets I【容斥原理】
- zoj 3556 How Many Sets I - 容斥原理
- 代码走查工具FindBugs, PMD,CheckStyle
- JLIKN固件修复
- 《剑指offer》给定一颗二叉搜索树,请找出其中的第k大的结点。
- RBAC数据库设计
- 扩展阅读 眼界与可以做的事
- How Many Sets
- (hdu1573)X问题(中国剩余定理+扩展欧几里得算法)
- 探索C++对象模型
- java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.g
- feof()和EOF的用法—— C中文件结尾的判断
- 文章标题nodejs-002nodejs数据类型
- 运行protoc 把.proto文件生成.pb.go文件,遇到的问题以及解决方案
- 教你如何阅读Oracle数据库官方文档
- JS中的数组排序函数sort()和reverse()