[容斥原理] zoj 3556 How Many Sets I
主题链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- ZOJ 3556
终于做出来了,激动.... 这道题隐藏得深啊,但若推导下来,就变简单了. 首先,一个集合的子集的个数为2^n=s.注意了,题目求的是有序集合组,并且每个集合是可以重复使用的,怎么办呢?这就要想到多重集 ...
- 组合数们&&错排&&容斥原理
最近做了不少的组合数的题这里简单总结一下下 1.n,m很大p很小 且p为素数p要1e7以下的 可以接受On的时间和空间然后预处理阶乘 Lucas定理来做以下是代码 /*Hdu3037 Saving B ...
- How Many Sets I(容斥定理)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...
- ZOJ 3233 Lucky Number --容斥原理
这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数 ...
- ZOJ 3687 The Review Plan I 容斥原理
一道纯粹的容斥原理题!!不过有一个trick,就是会出现重复的,害我WA了几次!! 代码: #include<iostream> #include<cstdio> #inclu ...
随机推荐
- 数据库迁移 - SQLServer->MySQL
SqlServer转换为Mysql的一款工具推荐(mss2sql)
- 利用ant的javac任务来编译程序使用ant的java任务来运行程序
<?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...
- JavaScript DOM省市自适配select菜单
<html> <head> <meta charset="UTF-8"> <meta name="Generator" ...
- 实战 iTextSharp
原文 实战 iTextSharp iTextSharp 是用来生成 PDF 的一个组件,在 1998 年夏天的时候,Bruno Lowagie ,iText 的创作者,参与了学校的一个项目,当时使用 ...
- javascript:设置URL参数的方法,适合多条件查询
适用场景:多条件查询情况,如下图所示: 通过设置URL参数,再结合数据源控件设置的RUL参数,就能进行简单的多条件查询了. javascript函数: <mce:script type=&quo ...
- Libgdx环境搭建及介绍
Libgdx简单介绍: libgdx是一个跨平台的2D/3D的游戏开发框架,它由Java/C/C++语言编写而成.ibgdx兼容大多数微机平台(标准JavaSE实现,能执行在Mac.Linux.Win ...
- 在SharePoint 2013 中使用文档库Scheduling (计划公布功能)
本文讲述在SharePoint2013 中使用文档库Scheduling (计划公布功能)的步骤和注意的事项. 文档库Scheduling (计划公布功能) 用于设定当文档通过审批后特定的时间区间内才 ...
- Android SurfaceView实战 打造抽奖转盘
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41722441 ,本文出自:[张鸿洋的博客] 1.概述 今天给大家带来Surfac ...
- linux脚本: makefile以及链接库
Linux makefile 教程 非常详细,且易懂 http://blog.csdn.net/liang13664759/article/details/1771246 //sort.c #incl ...
- yii Query Builder (yii 查询构造器) 官方指南翻译
/**** Query Builder translated by php攻城师 http://blog.csdn.net/phpgcs Preparing Query Builder 准备 Quer ...