【折半枚举】Ural Championship April 30, 2017 Problem G. Glasses with solutions
题意:有n杯盐溶液,给定每杯里面盐的质量以及盐溶液的质量。问你有多少种方案选择一个子集,使得集合里面的盐溶液倒到一个被子里面以后,浓度为A/B。
折半枚举,暴力搜索分界线一侧的答案数,跨越分界线的答案,一侧用map存下来,枚举另一侧去统计。
#include<cstdio> #include<map> using namespace std; typedef long long ll; ll ans; int n,a,b,m[39],t[39]; map<ll,int>ma; void dfs(int op,int cur,int goal,int dep,ll A,ll B){ if(dep!=0){ if(A*(ll)b==B*(ll)a){ ++ans; } if(op==0){ ++ma[(ll)b*A-(ll)a*B]; } else{ ans+=(ll)ma[(ll)a*B-(ll)b*A]; } } for(int i=cur;i<=goal;++i){ dfs(op,i+1,goal,dep+1,A+(ll)m[i],B+(ll)t[i]); } } int main(){ // freopen("g.in","r",stdin); scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;++i){ scanf("%d%d",&m[i],&t[i]); } dfs(0,1,n/2,0,0,0); dfs(1,n/2+1,n,0,0,0); printf("%lld\n",ans); return 0; }
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/