1. 程式人生 > >「NOIP模擬」化學【狀態壓縮】

「NOIP模擬」化學【狀態壓縮】

在這裡插入圖片描述

分成兩組狀壓。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define db double
#define sg string
#define ll long long
#define rel(i,x,y) for(ll i=(x);i<(y);i++)
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
#define red(i,x,y) for(ll i=(x);i>=(y);i--)
#define res(i,x) for(ll i=head[x];i;i=nxt[i])
using namespace std;

const ll N=20;
const ll Inf=1e18;
const ll Mod=1e9+7;

ll n,m,la,lb,ans,alla,allb;
ll a[N+5],ka,ra[1<<N],suma[1<<N];
ll b[N+5],kb,rb[1<<N],sumb[1<<N];

#define lowbit(x) (x&(-x))

inline ll read() {
	ll x=0;char ch=getchar();bool f=0;
	while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return f?-x:x;
}

void File() {
	freopen("chemistry.in","r",stdin);
	freopen("chemistry.out","w",stdout);
}

ll find(ll x) {
	ll l=1,r=kb,ans=0;
	
	while(l<=r) {
		ll mid=l+r>>1;
		if(rb[mid]>x) r=mid-1;
		else l=mid+1,ans=mid;
	}
	
	return ans;
}

int main() {
//	File();
	
	n=read(),m=read();
	
	la=n>>1;alla=(1<<la)-1;
	
	lb=n-la;allb=(1<<lb)-1;
	
	rep(i,1,la) a[i]=read();
	
	rep(i,1,lb) b[i]=read();
	
	rep(i,1,alla) {
		suma[i]=suma[i^lowbit(i)]+a[__builtin_ctz(lowbit(i))+1];
		if(suma[i]<=m) ans++,ra[++ka]=suma[i];
	}
	
	rep(i,1,allb) {
		sumb[i]=sumb[i^lowbit(i)]+b[__builtin_ctz(lowbit(i))+1];
		if(sumb[i]<=m) ans++,rb[++kb]=sumb[i];
	}
	
	sort(rb+1,rb+1+kb);
	
	rep(i,1,ka) ans+=find(m-ra[i]);
	
	printf("%lld",ans+1);

	return 0;
}