Luogu3067 平衡的奶牛群 Meet in the middle
题意:给出$N$个范围在$[1,10^8]$内的整数,问有多少种取数方案使得取出来的数能够分成两个和相等的集合。$N \leq 20$
发现爆搜是$O(3^N)$的,所以考虑双向搜索。
先把前$3^\frac{N}{2}$搜完,然后每一次搜出后$3^\frac{N}{2}$的时候,枚举前面的$2^\frac{N}{2}$,每一个对应一下看有没有和为$0$的方案即可。复杂度为$O(6^\frac{N}{2})$,虽然不开O2过不去qwq
 #include<bits/stdc++.h>
 using namespace std;
 inline int read(){
     ;
     char c = getchar();
     while(!isdigit(c))
     c = getchar();
     while(isdigit(c)){
     a = (a << ) + (a << ) + (c ^ ');
     c = getchar();
     }
     return a;
 }
 struct HashTable{
 #define MOD 103
     struct node{
         int num;
         node* nxt;
     }*begin[MOD] , *last[MOD];
     void insert(int num){
         int t = num % MOD;
         )
             t += MOD;
         if(last[t] == NULL){
             begin[t] = new node;
             begin[t]->num = num;
             begin[t]->nxt = NULL;
             last[t] = begin[t];
         }
         else{
             node* now = new node;
             now->num = num;
             now->nxt = NULL;
             last[t]->nxt = now;
             last[t] = now;
         }
     }
     bool count(int num){
         int t = num % MOD;
         )
             t += MOD;
         for(node* i = begin[t] ; i != NULL ; i = i->nxt)
             if(i->num == num)
                 ;
         ;
     }
 }zt[ << ];
 ] , N , ans;
  << ][ << ];
 void init(int now , int end , int cnt , int sum){
     if(now > end){
         zt[cnt].insert(sum);
         return;
     }
     init(now +  , end , cnt , sum);
     init(now +  , end , cnt | ( << now) , sum + M[now]);
     init(now +  , end , cnt | ( << now) , sum - M[now]);
 }
 void getAns(int now , int end , int cnt , int sum){
     if(now > end){
      ; i <  << (N >> ) ; i++)
         if(!is[cnt][i] && (zt[i].count(sum) || zt[i].count(-sum))){
             ;
             ans++;
         }
     return;
     }
     getAns(now +  , end , cnt , sum);
     getAns(now +  , end , cnt | ( << now - (N >> )) , sum + M[now]);
     getAns(now +  , end , cnt | ( << now - (N >> )) , sum - M[now]);
 }
 int main(){
     N = read();
      ; i < N ; i++)
     M[i] = read();
     init( , (N >> ) -  ,  , );
     getAns(N >>  , N -  ,  , );
     cout << ans - ;
     ;
 }
再放一个复杂度似乎不对但是很快的方法
 #include<bits/stdc++.h>
 using namespace std;
 struct node{
     int zt , sum;
 }num1[] , num2[];
 ];
 ];
 void dfs(node* num , int& cnt , int now , int end , int sum , int zt){
     if(now > end){
         num[++cnt].sum = sum;
         num[cnt].zt = zt;
         return;
     }
     dfs(num , cnt , now +  , end , sum , zt);
     dfs(num , cnt , now +  , end , sum + M[now] , zt | ( << now));
     dfs(num , cnt , now +  , end , sum - M[now] , zt | ( << now));
 }
 bool cmp(node a , node b){
     return a.sum < b.sum;
 }
 bool operator == (node a , node b){
     return a.zt == b.zt && a.sum == b.sum;
 }
 int main(){
     cin >> N;
      ; i < N ; i++)
         cin >> M[i];
     dfs(num1 , cnt1 ,  , (N - ) >>  ,  , );
     dfs(num2 , cnt2 , N >>  , N -  ,  , );
     sort(num1 +  , num1 + cnt1 +  , cmp);
     sort(num2 +  , num2 + cnt2 +  , cmp);
     cnt1 = unique(num1 +  , num1 + cnt1 + ) - num1 - ;
     cnt2 = unique(num2 +  , num2 + cnt2 + ) - num2 - ;
     int p1 = cnt2 , p2 = cnt2;
      ; i <= cnt1 ; i++){
         )
             p1--;
         p2 = p1;
         )
             p2--;
         while(++p2 <= p1)
             vis[num1[i].zt | num2[p2].zt] = ;
     }
     ;
      ; i <  << N ; i++)
         ans += vis[i];
     cout << ans;
     ;
 }
Luogu3067 平衡的奶牛群 Meet in the middle的更多相关文章
- 洛谷 P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S…
		
P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S… 题目描述 Farmer John's owns N cows (2 <= N <= 20), where ...
 - [luogu3067 USACO12OPEN] 平衡的奶牛群
		
传送门 Solution 折半搜索模板题 考虑枚举每个点在左集合和右集合或者不在集合中,然后排序合并即可 Code //By Menteur_Hxy #include <cmath> #i ...
 - 折半搜索+状态压缩【P3067】 [USACO12OPEN]平衡的奶牛群Balanced Cow S…
		
Description 给n个数,从中任意选出一些数,使这些数能分成和相等的两组. 求有多少种选数的方案. Input 第\(1\)行:一个整数\(N\) 第\(2\)到\(N+1\)行,包含一个整数 ...
 - Meet in the middle
		
搜索是\(OI\)中一个十分基础也十分重要的部分,近年来搜索题目越来越少,逐渐淡出人们的视野.但一些对搜索的优化,例如\(A\)*,迭代加深依旧会不时出现.本文讨论另一种搜索--折半搜索\((meet ...
 - Meet in the middle学习笔记
		
Meet in the middle(MITM) Tags:搜索 作业部落 评论地址 PPT中会讲的很详细 当搜索的各项互不影响(如共\(n\)个物品前\(n/2\)个物品选不选和后\(n/2\)个物 ...
 - SPOJ4580 ABCDEF(meet in the middle)
		
题意 题目链接 Sol 发现abcdef是互不相关的 那么meet in the middle一下.先算出abc的,再算def的 注意d = 0的时候不合法(害我wa了两发..) #include&l ...
 - codevs1735 方程的解数(meet in the middle)
		
题意 题目链接 Sol 把前一半放在左边,后一半放在右边 meet in the middle一波 统计答案的时候开始想的是hash,然而MLE了两个点 实际上只要排序之后双指针扫一遍就行了 #inc ...
 - 【BZOJ4800】[Ceoi2015]Ice Hockey World Championship (meet in the middle)
		
[BZOJ4800][Ceoi2015]Ice Hockey World Championship (meet in the middle) 题面 BZOJ 洛谷 题解 裸题吧,顺手写一下... #i ...
 - 【CF888E】Maximum Subsequence(meet in the middle)
		
[CF888E]Maximum Subsequence(meet in the middle) 题面 CF 洛谷 题解 把所有数分一下,然后\(meet\ in\ the\ middle\)做就好了. ...
 
随机推荐
- 【机器学习】激活函数(Activation Function)
			
https://blog.csdn.net/ChenVast/article/details/81382795 激活函数是模型整个结构中的非线性扭曲力 神经网络的每层都会有一个激活函数 1.逻辑函数( ...
 - SD从零开始65 框架协议(Outline Agreement)
			
SD从零开始65 框架协议(Outline Agreement) 合同-销售凭证类型Contracts-Sales Document Types 框架协议在几乎所有的业务处理中都扮演重要的角色:客户和 ...
 - Ansible--inventory
			
简介 Inventory 是 Ansible 管理主机信息的配置文件,相当于系统 HOSTS 文件的功能,默认存放在 /etc/ansible/hosts.为方便批量管理主机,便捷使用其中的主机分组, ...
 - 自定义控件详解(五):onMeasure()、onLayout()
			
前言: 自定义控件的三大方法: 测量: onMeasure(): 测量自己的大小,为正式布局提供建议 布局: onLayout(): 使用layout()函数对所有子控件布局 绘制: onDraw() ...
 - 安卓开发_计时器(Chronometer)的简单使用
			
计时器控件(Chronometer)是一个可以显示从某个起始时间开始一共过去多长时间的本文. 继承自TextView,以文本的形式显示时间内容 该组件有五个方法 1.setBase(): \\用于设置 ...
 - 非对称加密与GPG/PGP
			
最近浏览博客的时候,经常会看到博主展示出自己的公钥,于是对 GPG/PGP 产生兴趣.下面简单记录相关文章的链接,方便以后了解. 简介: 1991年,程序员Phil Zimmermann为了避开政府的 ...
 - macOS平台安装metasploit
			
1 在Github上克隆Metasploit git clone https://github.com/rapid7/metasploit-framework.git /usr/local/shar ...
 - iframe无刷新跨域上传文件并获得返回值
			
原文:http://geeksun.iteye.com/blog/1070607 需求:从S平台上传文件到R平台,上传成功后R平台返回给S平台一个值,S平台是在一个页面弹出的浮窗里上传文件,所以不能用 ...
 - Appium1.9 之 Chromedriver安装方式
			
1.在 appium 官网上下载安装后,下载的是1.7.1的版本,安装之后是1.9.1最新版本. 2.appium安装之后,会发现涉及到 浏览器相关的业务时(我使用的是chrome)会提示 “No C ...
 - poi对excel的基本读写操作
			
最近简单的弄了下poi对excel的应用,为方便自己以后的使用就把一些基本操作记录下来,其他更复杂的操作可以等以后有需求的时候再来深入了解一番! 写操作: /** * * 层次结构就是workbook ...