[BOJ] 1208 부분수열의 합2

Time Lapse :None

1208.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <map>
using namespace std;
int nums[40];
int target, N;
long long ans;
map<int,int> l;
void makesum(int sum, int pos){
if(pos==N/2){
l[sum]++;
return ;
}
else {
makesum(sum + nums[pos], pos + 1);
makesum(sum, pos + 1);
return ;
}
}
void makesum2(int sum, int pos){
if(pos==N){
ans += l[target-sum];
return;
}
makesum2(sum+nums[pos],pos+1);
makesum2(sum,pos+1);
}
int main(int argc, char** argv){
scanf("%d %d",&N, &target);
for(int i = 0; i < N; ++i)
scanf("%d",nums+i);
makesum(0,0);
makesum2(0,N/2);
printf("%lld",target ? ans : --ans);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/1208/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.