Count Subsets
题意:
给一集合 $S = \{ 1,2, ... , n \} $,取两个S的子集 A和B,使得A不是B的子集,且B不是A的子集。
解法:
1.牛顿展开
我们采用容斥,显然有
$$ans(n) = (2^n - 1)^2 - 2* \sum_{k=1}^n{C_n^k * (2^k - 2)} - (2^n-1)$$
$$ans(n) = (2^n - 1)(2^n-2) - 2*(\sum_{k=1}^n{C_n^k *2^k} - 2*\sum_{k=1}^n{C_n^k})$$
$$ans(n) = 4^n - 2*3^n + 2^n$$
#include <iostream>
#include <cstdio>
#include <cstring>
#include <complex>
#include <cmath>
#include <ctime> using namespace std; #define N 1000010
#define LL long long
#define P 1000000007LL using namespace std; LL power3[N],power2[N]; int main()
{
power3[]=;
power2[]=;
for(int i=;i<N;i++)
{
power3[i] = power3[i-] * 3LL % P;
power2[i] = power2[i-] * 2LL % P;
}
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
LL ans = power2[n]*(power2[n]+1LL)%P;
ans = (ans+P-2LL*power3[n]%P)%P;
printf("%I64d\n",ans);
}
return ;
}
2.生成函数(待补)
可以构造出
Count Subsets的更多相关文章
- LeetCode----Array
Remove Duplicates from Sorted Array 思路:两个指针,头指针在0,尾指针从1开始寻找,找到第一个不等于头指针值的数,覆盖掉头指针后面那个数,然后尾指针往后移. pub ...
- 【子集或者DFS】部分和问题
题目: 给定整数序列a1,a2,...,an,判断是否可以从中选出若干数,使它们的和恰好为k.1≤n≤20 -108≤ai≤108 -108≤k≤108 输入: n=4 a={1,2,4,7} ...
- nodejs api 中文文档
文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 90. Subsets II(中等,编写代码有难度)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [Swift]LeetCode78. 子集 | Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- [Swift]LeetCode90. 子集 II | Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [Swift]LeetCode698. 划分为k个相等的子集 | Partition to K Equal Sum Subsets
Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
随机推荐
- python(16)- python内置函数
python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:https://docs.python.org/3/library/functions.html Buil ...
- Android对apk源代码的改动--反编译+源代码改动+又一次打包+签名【附HelloWorld的改动实例】
最近遇到了须要改动apk源代码的问题,于是上网查了下相关资料.编写了HelloWorld进行改动看看可行性,经过实验证明此方案可行,而且后来也成功用这种方法对目标apk进行了改动,仅仅只是须要改动的部 ...
- Canvas学习笔记——拖曳与投掷物体
首先用一个例子来演示这个效果: 鼠标可以拖曳和投掷小球 // > 16 & 0xff, g = color >> 8 & 0xff, b = color > ...
- h5的复制功能
js+html5实现复制文字按钮 <div> <input type="text" name="guanfangaddress" id=&qu ...
- [深入浅出Cocoa]iOS程序性能优化
本文转载至 http://blog.csdn.net/kesalin/article/details/8762032 [深入浅出Cocoa]iOS程序性能优化 罗朝辉 (http://blog.csd ...
- EasyDarwin添加自定义的服务模块EasyMyModule
EasyDarwin模块的要求 每个QTSS模块必须实现两个方法函数: 一个Main入口函数,服务器在启动的时候将调用这个方法函数,来对您开发的模块所在的QTSS stub库进行初始化. 一个Disp ...
- Geoffrey E. Hinton
https://www.cs.toronto.edu/~hinton/ Geoffrey E. Hinton I am an Engineering Fellow at Google where I ...
- leeetcode 735. Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- Spring Boot缓存源码分析
前言 项目里面要增加一个应用缓存,原本想着要怎么怎么来整合ehcache和springboot,做好准备配置这个配置那个,结果只需要做三件事: pom依赖 写好一个ehcache的配置文件 在boot ...
- jquery特效(6)—判断复选框是否选中进行答题提示
前面有一段时间思想开了小差,跟着师父学习了一段时间才发现差距很大,看来我要奋起直追~\(≧▽≦)/~啦啦啦. 最近公司在做一个项目,需要根据用户选择的选项给出相应的提示,下面来看我写的测试程序的效果: ...