[容斥原理] zoj 3556 How Many Sets I
主题链接:
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的更多相关文章
- 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 ...
随机推荐
- python-Day4-迭代器-yield异步处理--装饰器--斐波那契--递归--二分算法--二维数组旋转90度--正则表达式
本节大纲 迭代器&生成器 装饰器 基本装饰器 多参数装饰器 递归 算法基础:二分查找.二维数组转换 正则表达式 常用模块学习 作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - ...
- mysql入门记录
mysql -h localhost(or ID) -u root -p show databases; create database <数据库名>: drop database < ...
- initialize和init区别
Objective-C很有趣的一个地方是,它非常非常像C.实际上,它就是C语言加上一些其他扩展和一个运行时间(runtime). 有了这个在每个Objective-C程序中都会起作用的附加运行时间,给 ...
- GExpert 1.38 实验版含经典代码格式工具 Berlin 编译版
安装方法:修改install.reg,然后导入.源代码地址:http://blog.dummzeuch.de/experimental-gexperts-version/ http://bbs.2cc ...
- MinGW是什么
MinGW是什么? MinGW是建立在gcc和binutils项目上的,用来编译和连接代码,使之运行在windows系统上: 提供c.c++和fortran编译器和相关工具: MinGW=Minima ...
- mysql服务无法启动
可能是没有data文件夹,可以新建或拷贝以前的data文件夹到安装目录.
- STL--G - For Fans of Statistics(两个推断条件-二分)
G - For Fans of Statistics Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- JNI 详细解释
JNI事实上,Java Native Interface缩写,那是,java本地接口.它提供了许多API实现和Java和其它语言的通信(主要是C&C++). 或许不少人认为Java已经足够强大 ...
- S3C3440看门狗驱动程序
S3C3440看门狗驱动程序 http://www.cnblogs.com/lfsblack/archive/2012/09/13/2684079.html 看门狗是当CPU进入错误状态后,无法恢复的 ...
- MFC 窗口重绘问题
在客户区画直线等图形时, 发现当其最小化或者其他窗口遮挡时,出现窗口重绘,而将原来绘制的图形删除,上网上搜索知道,绘制图形的代码必须放置在Ondraw函数中,才能避免重绘时图形消失(因为一直在响应WM ...