[SWEA] 2817 부분 수열의 합

Time Lapse :21min 37sec

2817.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>

#define gc() getchar_unlocked()
#define pc(x) putchar_unlocked(x)

int A[20] = {0}, K, answer = 0, N;
inline int fRI(){
int N = gc(), r = 0;
for(;0x30>N||N>0x3A;N=gc());
do{
r = (r<<3)+(r<<1)+(N&0b1111); N = gc();
}while(0x30<=N&&N<=0x3A);
return r;
}

inline void fWI(int tc) {
int r = 0, c = 0;
pc('#');
while (!(tc % 10)) { c++; tc /= 10; }
while (tc) { r = (r<<3)+(r<<1)+tc%10; tc /= 10; }
while (r) { pc(r % 10 + 48); r /= 10; }
while (c--) pc(0x30); pc(32);
c = 0;
if(!answer) {pc(48); pc(0x0A); return ;}
while (!(answer % 10)) { c++; answer /= 10; }
while (answer) { r = (r<<3)+(r<<1)+answer%10; answer /= 10; }
while (r) { pc(r % 10 + 48); r /= 10; }
while (c--) pc(0x30); pc(0x0A);
return;
}
inline void DFS(int cur,int start){
if(cur^K){
for(int i = start; i < N; i++){
if(cur+A[i]<=K) DFS(cur+A[i],i+1);
else return ;
}
}
else answer++;
}
inline int compare(const void *A, const void *B){
return *(int*) A > *(int*) B ? 1 : -1;
}
int main(int argc, char** argv){
register int tc = 1, T = fRI();
for(;tc<=T;){
N = fRI(); K = fRI();
for(register int i = 0 ; i < N; i++)
A[i] = fRI();
qsort(A,N,sizeof(int),compare);
DFS(0,0);
fWI(tc++);
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/2817/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.