-
Notifications
You must be signed in to change notification settings - Fork 1
/
KC024.cpp
executable file
·88 lines (63 loc) · 1.42 KB
/
KC024.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <algorithm>
#include <vector>
#define FOR(i,a,b) for(int i = a ; i < b ; ++i )
using namespace std;
int binSearch( vector< int >& tab, int start, int stop , int number ){
if( start >= stop )
return start;
int middle = ( start + stop )/2;
if( tab[ middle ] == number )
return middle;
else if( tab[ middle ] < number )
return binSearch( tab, middle+1, stop , number );
else if( tab[ middle ] > number )
return binSearch( tab, start, middle -1 , number );
}
bool forbidden[1000];
bool checkSum( vector< int >& tab, int start, int stop , int sum ){
int newSum = 0 ;
for ( int i = stop ; i >= 0 ; i-- ){
if( forbidden[ i ] == false ){
forbidden[ i ] = true;
newSum = sum - tab[ i ];
if( newSum == 0 )
return true;
else if( newSum > 0 ){
int pos = binSearch( tab, start, stop , newSum );
if ( checkSum( tab, start, pos, newSum ) ){
return true;
}
}
forbidden[ i ] = false;
}
}
return false;
}
int main(){
int t;
int n;
int temp;
int sum;
vector<int> tab(1000);
ios_base::sync_with_stdio(false);
while( true ){
cin >> sum;
if( cin.eof() ) break;
cin >> n;
FOR( i , 0 , n ){
cin >> tab[ i ];
forbidden[ i ] = false;
}
if( sum == 0 )
cout << "1\n";
else{
sort( tab.begin(), tab.begin()+n );
if ( checkSum( tab, 0, n-1 , sum ) )
cout << "1\n";
else
cout << "0\n";
}
}
return 0 ;
}