SPOJ VECTAR5 推公式

探讨了如何计算从集合S={1,2,...,n}

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:
2
4
8

SAMPLE OUTPUT:
110
52670

题意:对于一个集合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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值