思路:折半搜索,每个数的状态只有三种:不选、选入集合A、选入集合B,然后就暴搜出其中一半,插入hash表,然后再暴搜另一半,在hash表里查找就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 25 int n;
int a[maxn],t[maxn];
bool v[1025][1025];
int ans; struct hash_table{
static const int mod=242354;
int tot,now[mod],pre[1000000],son[1000000],val[1000000];
void insert(int t,int x){
int pos=(x%mod+mod)%mod;
for (int p=now[pos];p;p=pre[p]) if (son[p]==t&&val[p]==x) return;
son[++tot]=t,val[tot]=x,pre[tot]=now[pos],now[pos]=tot;
}
int find(int t,int x){
int pos=(x%mod+mod)%mod,ans=0;
for (int p=now[pos];p;p=pre[p])
if (val[p]==x&&!v[son[p]][t]){v[son[p]][t]=1;ans++;}
return ans;
}
}H; void dfs1(int x,int val){
if (x>n/2){
int tmp=0;for (int i=1;i<=n/2;i++) tmp=tmp<<1|t[i];
H.insert(tmp,val);return;
}
t[x]=1;dfs1(x+1,val+a[x]);
t[x]=0;dfs1(x+1,val);
t[x]=1;dfs1(x+1,val-a[x]);
} void dfs2(int x,int val){
if (x>n){
int tmp=0;for (int i=n/2+1;i<=n;i++) tmp=tmp<<1|t[i];
ans+=H.find(tmp,val);
return;
}
t[x]=1;dfs2(x+1,val+a[x]);
t[x]=0;dfs2(x+1,val);
t[x]=1;dfs2(x+1,val-a[x]);
} int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
dfs1(1,0),dfs2(n/2+1,0);
printf("%d\n",ans-1);
return 0;
}

bzoj2679:[Usaco2012 Open]Balanced Cow Subsets的更多相关文章

  1. bzoj2679: [Usaco2012 Open]Balanced Cow Subsets(折半搜索)

    2679: [Usaco2012 Open]Balanced Cow Subsets Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 462  Solv ...

  2. BZOJ_2679_[Usaco2012 Open]Balanced Cow Subsets _meet in middle+双指针

    BZOJ_2679_[Usaco2012 Open]Balanced Cow Subsets _meet in middle+双指针 Description Farmer John's owns N ...

  3. 【BZOJ 2679】[Usaco2012 Open]Balanced Cow Subsets(折半搜索+双指针)

    [Usaco2012 Open]Balanced Cow Subsets 题目描述 给出\(N(1≤N≤20)\)个数\(M(i) (1 <= M(i) <= 100,000,000)\) ...

  4. 折半搜索+Hash表+状态压缩 | [Usaco2012 Open]Balanced Cow Subsets | BZOJ 2679 | Luogu SP11469

    题面:SP11469 SUBSET - Balanced Cow Subsets 题解: 对于任意一个数,它要么属于集合A,要么属于集合B,要么不选它.对应以上三种情况设置三个系数1.-1.0,于是将 ...

  5. BZOJ2679 : [Usaco2012 Open]Balanced Cow Subsets

    考虑折半搜索,每个数的系数只能是-1,0,1之中的一个,因此可以先通过$O(3^\frac{n}{2})$的搜索分别搜索出两边每个状态的和以及数字的选择情况. 然后将后一半的状态按照和排序,$O(2^ ...

  6. [Usaco2012 Open]Balanced Cow Subsets

    Description Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk ...

  7. 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets

    [算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...

  8. 「题解」:毛一琛/$cow$ $subsets$

    问题 A: 毛一琛/$cow$ $subsets$ 时间限制: 1 Sec  内存限制: 512 MB 题面 题面谢绝公开. 题解 题名貌似是个大神??看起来像是签到题然后就死了. 首先$O(3^n) ...

  9. BZOJ.2679.Balanced Cow Subsets(meet in the middle)

    BZOJ 洛谷 \(Description\) 给定\(n\)个数\(A_i\).求它有多少个子集,满足能被划分为两个和相等的集合. \(n\leq 20,1\leq A_i\leq10^8\). \ ...

随机推荐

  1. Centos kvm桥接

    新建一个ifcfg-br0文件: DEVICE=br0TYPE=BridgeBOOTPROTO=staticBROADCAST=10.1.255.255IPADDR=10.1.29.3NETMASK= ...

  2. 在C#中生成唯一的字符串和数字【GUID】转

    转自:http://www.cnblogs.com/lcwzj/archive/2009/04/16/1436992.html 当我们想要获得一个唯一的key的时候,通常会想到GUID.这个key非常 ...

  3. Spark RDD概念学习系列之RDD的容错机制(十七)

    RDD的容错机制 RDD实现了基于Lineage的容错机制.RDD的转换关系,构成了compute chain,可以把这个compute chain认为是RDD之间演化的Lineage.在部分计算结果 ...

  4. Spark shell里的语句探索

    获得垃圾链接数据集的命令如下: wget http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/spam.data scala> v ...

  5. js基础一

    1.声明提升:变量的声明提升,函数的声明提升,但函数赋值表达式不会提升: foo(); // 正常运行,因为foo在代码运行前已经被创建 function foo() {} foo(); // 出错: ...

  6. "用wow64exts调试64位任务管理器抓取的32位程序的dump"

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:"用wow64exts调试64位任务管理器抓取的32位程序的dump".

  7. get-random生成电话号码

    "138"+((0..9|Get-Random -count 10) -join $null) From:http://blog.csdn.net/shrekz/article/d ...

  8. PowerShell随笔2_分支 选择 循环 特殊变量

    PowerShell特殊变量: PowerShell的特殊变量由系统自动创建.用户自定义的变量名称应该不和特殊变量相同. $^ :前一命令行的第一个标记 $$ :前一命令行的最后一个标记 $_ :表示 ...

  9. DirectoryExists

    判断文件夹是否存在 关键点 GetFileAttributes The GetFileAttributes function retrieves attributes for a specified  ...

  10. 混合模式程序集是针对“v1.1.4322”版的执行时生成的,在没有配置其它信息的情况下,无法在 4.0 执行时中载入该程序集。

    看到一个kinect大牛编写的一个水果忍者的体感游戏版本号,让我为自己一直以来仅仅用现有的网页游戏来模拟kinect体感游戏控制感到羞愧,没办法.我还是菜鸟.学习一段后自己模仿星际大战这个游戏.自己写 ...