主题链接:

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 1
  2. 2 2

Sample Output

  1. 1
  2. 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了,直接高速幂就可以。

代码:

  1. //#include<CSpreadSheet.h>
  2.  
  3. #include<iostream>
  4. #include<cmath>
  5. #include<cstdio>
  6. #include<sstream>
  7. #include<cstdlib>
  8. #include<string>
  9. #include<string.h>
  10. #include<cstring>
  11. #include<algorithm>
  12. #include<vector>
  13. #include<map>
  14. #include<set>
  15. #include<stack>
  16. #include<list>
  17. #include<queue>
  18. #include<ctime>
  19. #include<bitset>
  20. #include<cmath>
  21. #define eps 1e-6
  22. #define INF 0x3f3f3f3f
  23. #define PI acos(-1.0)
  24. #define ll __int64
  25. #define LL long long
  26. #define lson l,m,(rt<<1)
  27. #define rson m+1,r,(rt<<1)|1
  28. #define M 1000000007
  29. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  30. using namespace std;
  31.  
  32. LL n,k;
  33.  
  34. LL quick(LL a,LL b)
  35. {
  36. LL res=1;
  37.  
  38. while(b)
  39. {
  40. if(b&1)
  41. res=(res*a)%M;
  42. b>>=1;
  43. a=a*a%M;
  44. }
  45. return res;
  46. }
  47.  
  48. int main()
  49. {
  50. //freopen("in.txt","r",stdin);
  51. //freopen("out.txt","w",stdout);
  52. while(~scanf("%lld%lld",&n,&k))
  53. {
  54. LL ans=(quick(2,k)-1+M)%M;
  55.  
  56. ans=quick(ans,n);
  57.  
  58. printf("%lld\n",ans);
  59. }
  60. return 0;
  61. }

[容斥原理] 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. 数据库迁移 - SQLServer->MySQL

    SqlServer转换为Mysql的一款工具推荐(mss2sql)

  2. 利用ant的javac任务来编译程序使用ant的java任务来运行程序

    <?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...

  3. JavaScript DOM省市自适配select菜单

    <html> <head> <meta charset="UTF-8"> <meta name="Generator" ...

  4. 实战 iTextSharp

    原文 实战 iTextSharp iTextSharp 是用来生成  PDF 的一个组件,在 1998 年夏天的时候,Bruno Lowagie ,iText 的创作者,参与了学校的一个项目,当时使用 ...

  5. javascript:设置URL参数的方法,适合多条件查询

    适用场景:多条件查询情况,如下图所示: 通过设置URL参数,再结合数据源控件设置的RUL参数,就能进行简单的多条件查询了. javascript函数: <mce:script type=&quo ...

  6. Libgdx环境搭建及介绍

    Libgdx简单介绍: libgdx是一个跨平台的2D/3D的游戏开发框架,它由Java/C/C++语言编写而成.ibgdx兼容大多数微机平台(标准JavaSE实现,能执行在Mac.Linux.Win ...

  7. 在SharePoint 2013 中使用文档库Scheduling (计划公布功能)

    本文讲述在SharePoint2013 中使用文档库Scheduling (计划公布功能)的步骤和注意的事项. 文档库Scheduling (计划公布功能) 用于设定当文档通过审批后特定的时间区间内才 ...

  8. Android SurfaceView实战 打造抽奖转盘

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41722441 ,本文出自:[张鸿洋的博客] 1.概述 今天给大家带来Surfac ...

  9. linux脚本: makefile以及链接库

    Linux makefile 教程 非常详细,且易懂 http://blog.csdn.net/liang13664759/article/details/1771246 //sort.c #incl ...

  10. yii Query Builder (yii 查询构造器) 官方指南翻译

    /**** Query Builder translated by php攻城师 http://blog.csdn.net/phpgcs Preparing Query Builder 准备 Quer ...