-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1182.java
More file actions
45 lines (39 loc) · 1.13 KB
/
1182.java
File metadata and controls
45 lines (39 loc) · 1.13 KB
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
35
36
37
38
39
40
41
42
43
44
45
import java.io.*;
import java.util.*;
public class Main{
public static int N;
public static int S;
public static int[] set;
public static int cnt;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
S = Integer.parseInt(st.nextToken());
set = new int[N];
cnt = 0;
st = new StringTokenizer(br.readLine());
for(int i=0; i<N;i++){
set[i] = Integer.parseInt(st.nextToken());
}
//비트마스크
int a = (1<<N) - 1;
// 부분집합 전부 순회(공집합 제외)
for(int i = a;i>0;i = ((i-1) & a)){
check(i);
}
System.out.println(cnt);
}
//합을 확인하는 함수
public static void check(int bit){
int sum = 0;
for(int i = 0;i<N;i++){
if((bit & (1<<i)) > 0){
sum += set[i];
}
}
if(sum == S){
cnt += 1;
}
}
}