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 ...
随机推荐
- rf-demos (request)
*** Settings *** Library RequestsLibrary Library Collections Library XML *** Test Cases *** case1 Cr ...
- Jenkins Robot framework 持续集成环境搭建
为什么我们要引入RF?其实最初我们引入RF是为了能够快速的开展自动化验收测试,为敏捷保驾护航.这其中有个重要的工具Jenkins,同时也是应群里朋友们的要求,这次就来介绍一下RF如何快速便捷的结合Je ...
- 转:scanf的用法
https://blog.csdn.net/u012421456/article/details/18501309 scanf()[通过键盘将数据输入到变量中] 它有两种用法: 用法一: scanf( ...
- 使用Pig对手机上网日志进行分析
在安装成功Pig的基础上.本文将使用Pig对手机上网日志进行分析,详细过程例如以下: 写在前面: 手机上网日志文件phone_log.txt.文件内容 及 字段说明部分截图例如以下 需求分析 显示每一 ...
- C#Soket组播
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- MacBook Pro使用初体验之Mac快捷键汇总(持续更新中)
我于近日购置了一台13寸的MacBook Pro高配,打算開始进行iOS开发的学习.Pro的配置情况例如以下: (1)OS X Yosemite ,版本号10.10.3 (2)Retina显示屏,13 ...
- 【SVN版本回退】
[SVN版本回退]根据想要回退的内容,然后选择revert to this revision或者revert changes from this revision.下面引用过来:譬如有个文件,有十个版 ...
- 在html中显示Flash的代码
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- spark 划分stage Wide vs Narrow Dependencies 窄依赖 宽依赖 解析 作业 job stage 阶段 RDD有向无环图拆分 任务 Task 网络传输和计算开销 任务集 taskset
每个job被划分为多个stage.划分stage的一个主要依据是当前计算因子的输入是否是确定的,如果是则将其分在同一个stage,从而避免多个stage之间的消息传递开销. http://spark. ...