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 ...
随机推荐
- CrtmpServer getApplication注册流程
最近在研究 CrtmpServer getApplication注册流程,以备查阅 图1. 加载动态库流程 图2配置application流程
- php自定义错误
function myErrorHandler ( $errno , $errstr , $errfile , $errline ) { if (!( error_reporting () & ...
- rf-demos (request)
*** Settings *** Library RequestsLibrary Library Collections Library XML *** Test Cases *** case1 Cr ...
- Allegro基本操作——PCB布线
转:http://blog.sina.com.cn/s/blog_1538bc9470102vyyq.html http://www.elecfans.com/article/80/110/2010/ ...
- Laravel建站04--建立后台文章管理
路由配置 Route::group(['middleware' => 'auth', 'namespace' => 'Admin', 'prefix' => 'admin'], fu ...
- 2016年最值得新手程序猿阅读的书:《增长project师指南》
这本书的来源于根据我在<Repractise简单介绍篇:Web开发的七天里>中所说的 Web 开发的七个步骤而展开的电子书.当然它也是一个 APP.它一本关于怎样成为增长project师的 ...
- linux命令详解:file命令
前言 file命令可以获取多种文件类型,包括文本文件.脚本文件.源码文件.多媒体文件(音频视频)等.file是通过查看文件的头部内容,来获取文件的类型,而不像Window那样是通过扩展名来确定文件类型 ...
- swift3.0系列完整demo代码库
https://github.com/soapyigu/Swift30Projects 感谢作者
- MySQL 存储过程 (2)
通过存储过程查询数据库返回条数操作 第一步:登录自定义用户建立存储过程需要调用测试用到的student表,具体操作如下 (1) 登录用户
- @P0或@P1附近有语法错误
分析:@P0指的是第一个参数附近有错误;为'@P1'指的是第二个参数附近错误语法有错误.