SPOJ VECTAR5 推公式
来源:互联网 发布:php mysqli连接数据库 编辑:程序博客网 时间:2024/06/01 03:57
Count Subsets
You are given a set S = {1, 2, 3, ..., n}. Your task is simple. You have to calculate the number of ways of selecting non empty subsets A and B such that A is not a subset of B and B is not a subset of A. Since answer can be large output the result mod 10^9 + 7.
Input
First line of input contains single integer t denoting number of test cases.
Next t lines contain a single integer n.
Output
For each test case output answer to problem by taking mod with 10^9 + 7.
Constraints
1 <= t <= 100000
1 <= n <= 1000000
Example
SAMPLE INPUT:248SAMPLE OUTPUT:11052670
题意:对于一个集合S = {1, 2, ..., n}, 问选择一个非空集合A,一个非空集合B,
A和B都是S的子集,A不是B的子集,且B不是A的子集的方案数有多少,对答案mod1e9+7。
题解:A的取法有(2^n-1)种 B的取法也有(2^n-1)种
那么所有取法是(2^n-1)*(2^n-1)种
我们先不考虑A==B
那么A是B的子集的情况有(C(n,1)*(2^1-1)+C(n,2)*(2^2-1)+...+C(n,n)*(2^n-1))种
因为AB有对称性 所以再乘个2
因为上面的式子包含了A==B 所以我们再把A==B加上就行了 有(2^n-1)种
现在的问题是上面的式子怎么求和
可以先把-1提出来
C(n,1)*2^1+C(n,2)*2^2+...+C(n,n)*2^n - (C(n,1)+C(n,2)+...+C(n,n))
现在就不难看出来了
二项式的展开
所以左边就等于3^n-1 右边等于 2^n-1
然后求和取模就行了
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const ll mod=1000000007;ll quick(ll a,ll k){ll ans=1;while(k){if(k&1)ans=ans*a%mod;k/=2;a=a*a%mod;}return ans;}int main(){ll t;scanf("%lld",&t);while(t--){ll i,n;scanf("%lld",&n);ll ans=quick(quick(2,n)-1,2);ll t1=(quick(3,n)-quick(2,n))*2%mod;ans=ans-t1+quick(2,n)-1;ans=(ans%mod+mod)%mod;printf("%lld\n",ans);}return 0;}
- SPOJ VECTAR5 推公式
- SPOJ 27268 VECTAR5
- Touch of Venom +SPOJ+推公式题
- 推公式。。
- HDU2552 推公式||用公式
- POJ 3734 推公式
- 推公式+二分--poj1759
- hdu2047-递推公式
- HDU_5047_Sawtooth(推公式+高精度)
- 数学推公式题
- HDU 3490 推公式
- ZJNU 1265 推公式
- HDU 6195 推公式
- HDU 6045 推公式
- HDU 6055 推公式
- HDU 6050 推公式
- 递推公式小结
- 方差递推公式
- PipeWork设置Docker网络
- ModifyStyle函数的用法
- Android Json数据常见的得到数据
- Java中被面试官套路过的值传递和引用传递
- JavaScript拿到多个CheckBox的选中状态以及ajax的用法
- SPOJ VECTAR5 推公式
- 解决 AS Gradle 一直 Buildling 或 refreshing 的问题
- RobotFramework自动化学习笔记01-利用xpath定位元素
- aria2的帮助'#bittorrent'标记
- 设计模式总结笔记<三> 建造者模式前篇
- 1017. A除以B
- MFC代码修改窗口属性
- LeetCode:Two Sum
- iframe中的各种跳转方法