-
Notifications
You must be signed in to change notification settings - Fork 14
/
Exponential subsets.cpp
74 lines (59 loc) · 1.43 KB
/
Exponential subsets.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
// BY AYUSH AKASH
#include <iostream>
#include <vector>
using namespace std;
void solve()
{
int no_of_elements;
cin >> no_of_elements;
int sum = 0;
vector <int> A(no_of_elements + 1);
for(int i = 1; i <= no_of_elements; i++)
{
cin >> A[i];
sum += A[i]; //cout << "Sum = " << sum << "\n";
}
vector <int> reachable(sum + 1, false);
vector <int> reachable_before(sum + 1, false);
reachable[0] = true;
for(int i = 1; i <= no_of_elements; i++)
{ //cout << "i = " << i << "\n";
for(int j = 0; j <= sum; j++)
{
reachable_before[j] = reachable[j];
}
for(int s = A[i]; s <= sum; s++)
{
if(reachable_before[s - A[i]])
{
reachable[s] = true;
//cout << s << " is reachable\n";
}
}
}
for(int i = 1; i <= no_of_elements; i++)
{
int possible = false;
for(long long p = A[i]; p <= sum/A[i]; p *= A[i])
{ //cout << "P = " << p << "\n";
if(reachable[p*A[i]])
{
possible = true;
}
if(possible || A[i] == 1)
{
break;
}
}
cout << (possible ? 1 : 0) << " ";
}
cout << "\n";
}
int main()
{
int no_of_test_cases;
cin >> no_of_test_cases;
while(no_of_test_cases--)
solve();
return 0;
}