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 ...
随机推荐
- nightwatch.js - scroll until element is visible
.getLocationInView() Determine an element's location on the screen once it has been scrolled into vi ...
- MQTT 测试工具介绍
eclipse paho 下载地址为: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org ...
- 读取配置文件(configparser,.ini文件)
使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [baseconf]host=127.0.0.1port=3306user=rootpassw ...
- Laravel建站02--配置Laravel
Laravel项目的根目录下有.env文件,如果没有可以把.env.example改名为.env 这个文件是配置文件,可以把app_key.数据库.redis缓存等配置信息写在这个文件里. 目前5.4 ...
- RabbitMQ的工作模式
简单模式: # #########################基于简单模式的 生产者 ######################### #!/usr/bin/env python import ...
- 关于error:Cannot assign to 'self' outside of a method in the init family
有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写.在这种方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息例如以下:error:C ...
- JS 怎么把数组类型的参数传递到后台,后台怎么获取
说明:开发环境 vs2012 asp.net mvc4 c# 1.HTML前端代码 <%@ Page Language="C#" AutoEventWireup=" ...
- js 怎么传递参数
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- Flow 的工作方式 类型检查
Vue.js 技术揭秘 | Vue.js 技术揭秘 https://ustbhuangyi.github.io/vue-analysis/ Vue技术内幕 http://hcysun.me/vue-d ...
- Tomcat设置虚拟文件夹
需求 在做B/S的应用时.常常会遇到一个问题,站点上传的一些图片不是保存在应用server以下.而是保存在别的文件夹,可是页面中又需要能訪问到这些图片.这时,应用server的"虚拟文件夹& ...