主题链接:

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 of
the 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    

problemId=4535" style="color:blue; text-decoration:none">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;
}

[容斥原理] zoj 3556 How Many Sets I的更多相关文章

  1. zoj——3556 How Many Sets I

    How Many Sets I Time Limit: 2 Seconds      Memory Limit: 65536 KB Give a set S, |S| = n, then how ma ...

  2. ZOJ 3556 How Many Sets I

    How Many Sets I Time Limit: 2 Seconds      Memory Limit: 65536 KB Give a set S, |S| = n, then how ma ...

  3. zoj 3557 How Many Sets II

    How Many Sets II Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...

  4. zoj——3557 How Many Sets II

    How Many Sets II Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...

  5. ZOJ 3556

    终于做出来了,激动.... 这道题隐藏得深啊,但若推导下来,就变简单了. 首先,一个集合的子集的个数为2^n=s.注意了,题目求的是有序集合组,并且每个集合是可以重复使用的,怎么办呢?这就要想到多重集 ...

  6. 组合数们&&错排&&容斥原理

    最近做了不少的组合数的题这里简单总结一下下 1.n,m很大p很小 且p为素数p要1e7以下的 可以接受On的时间和空间然后预处理阶乘 Lucas定理来做以下是代码 /*Hdu3037 Saving B ...

  7. How Many Sets I(容斥定理)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...

  8. ZOJ 3233 Lucky Number --容斥原理

    这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数 ...

  9. ZOJ 3687 The Review Plan I 容斥原理

    一道纯粹的容斥原理题!!不过有一个trick,就是会出现重复的,害我WA了几次!! 代码: #include<iostream> #include<cstdio> #inclu ...

随机推荐

  1. CSS - ResetCss

    /* KISSY CSS Reset 理念:清除和重置是紧密不可分的 特色:1.适应中文 2.基于最新主流浏览器 */ /* 清除内外边距 */ body, h1, h2, h3, h4, h5, h ...

  2. GCD其他实用场景

    GCD线程间通信 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);     ...

  3. [转]VMware 出现下述错误: Application failure. hr=0x80040101:Failed to initialize virtual machine.

    VMware 出现下述错误:Application failure. hr=0x80040101:Failed to initialize virtual machine. 解决方法:1.重新注册这三 ...

  4. Linux下动态库使用

    1. 静态库和动态库的基本概念 静态库,是在可执行程序连接时就已经加入到执行码中,在物理上成为执行程序的一部分:使用静态库编译的程序运行时无需该库文件支持,哪里都可以用, 但是生成的可执行文件较大.动 ...

  5. (读书笔记).NET大局观-.NET框架类库概观

    .NET框架类库概况 构建在.NET框架上所有的软件,都会用到通用语言进行时,即使基于最简单的CLR程序,也需要用到一部分.NET框架类库,更精致复杂的软件则使用这个类库提供的更多服务. .NET框架 ...

  6. 【leetcode】Single Number II

    int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...

  7. win7 虚拟机 ios开发环境搭建

    http://www.cnblogs.com/xiaoyaoju/archive/2013/05/21/3091171.html

  8. ModelConvertHelper(将DataTable转换成List<model>)

      public class ModelConvertHelper<T> where T : new() {      public static IList<T> Conve ...

  9. 基于集合成工控机Ubuntu系统安装分区详解

    基于集合成工控机Ubuntu系统安装分区详解 硬件描述:双核的CPU,128G的固态硬盘 软件描述:使用Ubuntu12.04系统,内核3.8.0-29版本,QT4.8.1版本 1.新建分区表 /de ...

  10. 4部门明确软件IC产业企业所得税优惠政策

    中国证券网讯 据财政部5月9日消息,财政部.国家税务总局.发展改革委.工业和信息化部联合发布关于软件和集成电路产业企业所得税优惠政策有关问题的通知.该通知自2015年1月1日起执行. 通知指出,按照& ...