比赛 NOIP水题争霸赛 评测结果 AAAAAATTTT
题目名称 博士的密码 最终得分 60
用户昵称 Tony 运行时间 4.284 s
代码语言 C++ 内存使用 0.28 MiB
提交时间 2018-02-11 21:42:41
显示代码纯文本
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#define ll long long
using namespace std;
ll RD(){
	ll flag = 1,out = 0;char c = getchar();
	while(c < '0' || c > '9'){if(c == '-')flag = -1;c = getchar();}
	while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
	return flag * out;
	}
ll num,key;
ll ori[66];
ll cnt;
ll sum[66];
ll half1[10010100],half2[10010100];
ll p1,p2;
void DFS(ll index,ll sum){
	if(index == num + 1){
		if(sum == key)cnt++;
		return ;
		}
	for(int i = 0;i <= 1;i++){
		//cout<<i<<" "<<sum + i * ori[index]<<endl;
		DFS(index + 1,sum + i * ori[index]);
		}
	}
void DFS1(ll index,ll sum){
	if(index == num / 2 + 1){
		half1[++p1] = sum;
		return ;
		}
	for(int i = 0;i <= 1;i++){
		//cout<<i<<" "<<sum + i * ori[index]<<endl;
		DFS1(index + 1,sum + i * ori[index]);
		}
	}
void DFS2(ll index,ll sum){
	if(index == num + 1){
		half2[++p2] = sum;
		return ;
		}
	for(int i = 0;i <= 1;i++){
		//cout<<i<<" "<<sum + i * ori[index]<<endl;
		DFS2(index + 1,sum + i * ori[index]);
		}
	}
int main(){
	freopen("password1.in","r",stdin);
	freopen("password1.out","w",stdout);
	num = RD();key = RD();
	for(int i = 1;i <= num;i++){
		ori[i] = RD();
		}
	if(num <= 25){
		DFS(1,0);
		
		}
	else{
		DFS1(1,0);
		DFS2(num / 2 + 1,0);
		for(int i = 1;i <= p1;i++){
			for(int j = 1;j <= p2;j++){
				if(half1[i] + half2[j] == key)cnt++;
				}
			}
		}
	cout<<cnt<<endl;
	fclose(stdin);
	fclose(stdout);
	return 0;
	}