#include <bits/stdc++.h>
using namespace std;
int n, res;
int cuk[5005];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> cuk[i];
for (int k = 1; k < (1<<n); k++)
{
bitset <5005> B = k;
vector <int> v;
for (int i = 0; i < n; i++)
if (B[i])
v.push_back(cuk[i]);
set <int> S;
for (int i = 1; i < (1<<v.size()); i++)
{
bitset <5005> B2 = i;
int sum = 0;
for (int j = 0; j < v.size(); j++)
if (B2[j])
sum += v[j];
S.insert(sum);
}
int sum = 0;
for (auto i: v)
sum += i;
if (sum == S.size())
res++;
}
cout << res << '\n';
}
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 | #include <bits/stdc++.h> using namespace std; int n, res; int cuk[5005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> cuk[i]; for (int k = 1; k < (1<<n); k++) { bitset <5005> B = k; vector <int> v; for (int i = 0; i < n; i++) if (B[i]) v.push_back(cuk[i]); set <int> S; for (int i = 1; i < (1<<v.size()); i++) { bitset <5005> B2 = i; int sum = 0; for (int j = 0; j < v.size(); j++) if (B2[j]) sum += v[j]; S.insert(sum); } int sum = 0; for (auto i: v) sum += i; if (sum == S.size()) res++; } cout << res << '\n'; } |
English